Setting label properties

Cássio Marques committed Apr 14, 2013
commit 40f0292098b1c1cf8a3e5b4d17c5203afd491000
Showing 2 changed files with 106 additions and 14 deletions
zebra/epl/label.rb b/lib/zebra/epl/label.rb +33 -6
@@ @@ -2,29 +2,50 @@
module Zebra
module Epl
class Label
+ class InvalidPrintSpeedError < StandardError; end
+ class InvalidPrintDensityError < StandardError; end
+ class PrintSpeedNotInformedError < StandardError; end
+
attr_reader :elements
+ attr_accessor :width, :length, :gap, :print_speed, :print_density
- def initialize
+ def initialize(options = {})
+ options.each_pair { |key, value| self.__send__("#{key}=", value) if self.respond_to?("#{key}=") }
@elements = []
end
+ def length_and_gap=(length_and_gap)
+ self.length = length_and_gap[0]
+ self.gap = length_and_gap[1]
+ end
+
+ def print_speed=(s)
+ raise InvalidPrintSpeedError unless (0..6).include?(s)
+ @print_speed = s
+ end
+
+ def print_density=(d)
+ raise InvalidPrintDensityError unless (0..15).include?(d)
+ @print_density = d
+ end
+
def <<(element)
elements << element
end
def dump_contents(io = STDOUT)
+ check_required_configurations
# Start options
io << "O\n"
# Q<label height in dots>,<space between labels in dots>
- io << "Q240,24\n"
+ io << "Q#{length},#{gap}\n" if length && gap
# q<label width in dots>
- io << "q432\n"
+ io << "q#{width}\n" if width
# Print Speed (S command)
- io << "S2\n"
+ io << "S#{print_speed}\n"
# Density (D command)
- io << "D5\n"
+ io << "D#{print_density}\n" if print_density
# ZT = Printing from top of image buffer.
- io << "ZT\n"
io << "\n"
# Start new label
@@ @@ -36,6 +57,12 @@ module Zebra
io << "P0\n"
end
+
+ private
+
+ def check_required_configurations
+ raise PrintSpeedNotInformedError unless print_speed
+ end
end
end
end
spec/zebra/epl/label_spec.rb +73 -8
@@ @@ -3,7 +3,46 @@
require 'spec_helper'
describe Zebra::Epl::Label do
- subject(:label) { described_class.new }
+ subject(:label) { described_class.new :print_speed => 2 }
+
+ describe "#new" do
+ it "sets the label width" do
+ label = described_class.new :width => 300
+ label.width.should == 300
+ end
+
+ it "sets the label length/gap" do
+ label = described_class.new :length_and_gap => [400, 24]
+ label.length.should == 400
+ label.gap.should == 24
+ end
+
+ it "sets the printing speed" do
+ label = described_class.new :print_speed => 2
+ label.print_speed.should == 2
+ end
+
+ it "validates the printing speed" do
+ [-1, 8, "a"].each do |s|
+ expect {
+ described_class.new :print_speed => s
+ }.to raise_error(Zebra::Epl::Label::InvalidPrintSpeedError)
+ end
+ end
+
+ it "sets the print density" do
+ label = described_class.new :print_density => 10
+ label.print_density.should == 10
+ end
+
+ it "validates the print density" do
+ [-1, 16, "a"].each do |d|
+ expect {
+ described_class.new :print_density => d
+ }.to raise_error(Zebra::Epl::Label::InvalidPrintDensityError)
+ end
+ end
+ end
describe "#<<" do
it "adds an item to the list of label elements" do
@@ @@ -11,16 +50,42 @@ describe Zebra::Epl::Label do
label << stub
}.to change { label.elements.count }.by 1
end
+ end
+
+ describe "#dump_contents" do
+ let(:io) { "" }
- describe "#dump_contents" do
- let(:io) { "" }
+ it "dumps its contents to the received IO" do
+ label << stub(:to_s => "foobar")
+ label << stub(:to_s => "blabla")
+ label.width = 100
+ label.length_and_gap = [200, 24]
+ label.print_speed = 3
+ label.print_density = 10
+ label.dump_contents(io)
+ io.should == "O\nQ200,24\nq100\nS3\nD10\n\nN\nfoobar\nblabla\nP0\n"
+ end
+
+ it "does not try to set the label width when it's not informed (falls back to autosense)" do
+ label.dump_contents(io)
+ io.should_not =~ /q/
+ end
- it "dumps its contents to the received IO" do
- label << stub(:to_s => "foobar")
- label << stub(:to_s => "blabla")
+ it "does not try to set the length/gap when they were not informed (falls back to autosense)" do
+ label.dump_contents(io)
+ io.should_not =~ /Q/
+ end
+
+ it "does not try to set the print density when it's not informed (falls back to the default value)" do
+ label.dump_contents(io)
+ io.should_not =~ /D/
+ end
+
+ it "raises an error if the print speed was not informed" do
+ label = described_class.new
+ expect {
label.dump_contents(io)
- io.should == "O\nQ240,24\nq432\nS2\nD5\nZT\n\nN\nfoobar\nblabla\nP0\n"
- end
+ }.to raise_error(Zebra::Epl::Label::PrintSpeedNotInformedError)
end
end
end