Adds boxes

Cássio Marques committed Nov 24, 2013
commit 04bb56c932bf3c6f31b9d8cb7866f8b331e3532b
Showing 4 changed files with 117 additions and 1 deletions
zebra/epl.rb b/lib/zebra/epl.rb +1 -0
@@ @@ -5,6 +5,7 @@ require "zebra/epl/rotation"
require "zebra/epl/multipliers"
require "zebra/epl/print_mode"
require "zebra/epl/font"
+ require "zebra/epl/box"
require "zebra/epl/label"
require "zebra/epl/text"
require "zebra/epl/barcode"
zebra/epl/box.rb b/lib/zebra/epl/box.rb +40 -0
@@ @@ -0,0 +1,40 @@
+ require "zebra/epl/printable"
+
+ module Zebra
+ module Epl
+ class Box
+ include Printable
+
+ class InvalidLineThickness < StandardError; end
+
+ attr_reader :line_thickness, :end_position, :end_x, :end_y
+
+ def line_thickness=(thickness)
+ raise InvalidLineThickness unless thickness.nil? || thickness.to_i.to_s == thickness.to_s
+ @line_thickness = thickness
+ end
+
+ def end_position=(coords)
+ @end_position, @end_x, @end_y = coords, coords[0], coords[1]
+ end
+
+ def to_epl
+ check_attributes
+ ["X#{x}", y, line_thickness, end_x, end_y].join(",")
+ end
+
+ private
+
+ def has_data?
+ false
+ end
+
+ def check_attributes
+ super
+ raise MissingAttributeError.new("the line thickness is not given") unless line_thickness
+ raise MissingAttributeError.new("the horizontal end position (X) is not given") unless end_x
+ raise MissingAttributeError.new("the vertical end position (Y) is not given") unless end_y
+ end
+ end
+ end
+ end
zebra/epl/printable.rb b/lib/zebra/epl/printable.rb +5 -1
@@ @@ -29,10 +29,14 @@ module Zebra
private
+ def has_data?
+ true
+ end
+
def check_attributes
raise MissingAttributeError.new("the X value is not given") unless @x
raise MissingAttributeError.new("the Y value is not given") unless @y
- raise MissingAttributeError.new("the data to be printed is not given") unless @data
+ raise MissingAttributeError.new("the data to be printed is not given") unless @data || !has_data?
end
end
end
spec/zebra/epl/box_spec.rb +71 -0
@@ @@ -0,0 +1,71 @@
+ # encoding: utf-8
+ require 'spec_helper'
+
+ describe Zebra::Epl::Box do
+ it "can be initialized with initial position" do
+ box = described_class.new :position => [20, 40]
+ box.position.should == [20, 40]
+ box.x.should == 20
+ box.y.should == 40
+ end
+
+ it "can be initialized with the end position" do
+ box = described_class.new :end_position => [20, 40]
+ box.end_position.should == [20, 40]
+ box.end_x.should == 20
+ box.end_y.should == 40
+ end
+
+ it "can be initialized with the line thckness " do
+ box = described_class.new :line_thickness => 3
+ box.line_thickness.should == 3
+ end
+
+ describe "#to_epl" do
+ subject(:box) { described_class.new attributes }
+ let(:attributes) { { :position => [20,40], :end_position => [60, 100], :line_thickness => 3 } }
+
+ it "raises an error if the X position was not informed" do
+ box = described_class.new attributes.merge :position => [nil, 40]
+ expect {
+ box.to_epl
+ }.to raise_error(Zebra::Epl::Printable::MissingAttributeError, "Can't print if the X value is not given")
+ end
+
+ it "raises an error if the Y position was not informed" do
+ box = described_class.new attributes.merge :position => [20, nil]
+ expect {
+ box.to_epl
+ }.to raise_error(Zebra::Epl::Printable::MissingAttributeError, "Can't print if the Y value is not given")
+ end
+
+ it "raises an error if the end X position was not informed" do
+ box = described_class.new attributes.merge :end_position => [nil, 40]
+ expect {
+ box.to_epl
+ }.to raise_error(Zebra::Epl::Printable::MissingAttributeError, "Can't print if the horizontal end position (X) is not given")
+ end
+
+ it "raises an error if the end Y position was not informed" do
+ box = described_class.new attributes.merge :end_position => [20, nil]
+ expect {
+ box.to_epl
+ }.to raise_error(Zebra::Epl::Printable::MissingAttributeError, "Can't print if the vertical end position (Y) is not given")
+ end
+
+ it "raises an error if the line thickness was not informed" do
+ box = described_class.new attributes.merge :line_thickness => nil
+ expect {
+ box.to_epl
+ }.to raise_error(Zebra::Epl::Printable::MissingAttributeError, "Can't print if the line thickness is not given")
+ end
+
+ it "begins with the 'X' command" do
+ box.to_epl.should =~ /\AX/
+ end
+
+ it "contains the attributes in correct order" do
+ box.to_epl.should == "X20,40,3,60,100"
+ end
+ end
+ end