diff --git a/lib/zebra/zpl.rb b/lib/zebra/zpl.rb index e7a65a0..eee9678 100644 --- a/lib/zebra/zpl.rb +++ b/lib/zebra/zpl.rb @@ -8,8 +8,6 @@ require 'zebra/zpl/print_mode' require 'zebra/zpl/font' require 'zebra/zpl/box' -require 'zebra/zpl/diagonal' -require 'zebra/zpl/circle' require 'zebra/zpl/label' require 'zebra/zpl/text' require 'zebra/zpl/barcode' @@ -19,4 +17,5 @@ require 'zebra/zpl/pdf417' require 'zebra/zpl/justification' require 'zebra/zpl/raw' +require 'zebra/zpl/graphic' require 'zebra/zpl/datamatrix' diff --git a/lib/zebra/zpl/box.rb b/lib/zebra/zpl/box.rb index d3bdb06..a274804 100644 --- a/lib/zebra/zpl/box.rb +++ b/lib/zebra/zpl/box.rb @@ -41,6 +41,7 @@ def color=(value) def to_zpl check_attributes + puts "The Box class is deprecated. Please switch to the Graphic class (graphic_type = box)." "^FO#{x},#{y}^GB#{box_width},#{box_height},#{line_thickness},#{color},#{rounding_degree}^FS" end diff --git a/lib/zebra/zpl/circle.rb b/lib/zebra/zpl/circle.rb deleted file mode 100644 index 765b6fe..0000000 --- a/lib/zebra/zpl/circle.rb +++ /dev/null @@ -1,44 +0,0 @@ -require "zebra/zpl/printable" - -module Zebra - module Zpl - class Circle - include Printable - - class InvalidLineThickness < StandardError; end - class InvalidColorError < StandardError; end - - attr_reader :line_thickness, :diameter, :color - - def line_thickness=(thickness) - raise InvalidLineThickness unless thickness.nil? || thickness.to_i.to_s == thickness.to_s - @line_thickness = thickness - end - - def diameter=(value) - @diameter = value - end - - def color=(value) - raise InvalidColorError unless %w[B W].include?(value&.upcase) - @color = value - end - - def to_zpl - check_attributes - "^FO#{x},#{y}^GC#{diameter},#{line_thickness},#{color}^FS" - end - - private - - def has_data? - false - end - - def check_attributes - super - raise MissingAttributeError.new("the circle diameter is not given") unless diameter - end - end - end -end diff --git a/lib/zebra/zpl/diagonal.rb b/lib/zebra/zpl/diagonal.rb deleted file mode 100644 index 383e78b..0000000 --- a/lib/zebra/zpl/diagonal.rb +++ /dev/null @@ -1,55 +0,0 @@ -require "zebra/zpl/printable" - -module Zebra - module Zpl - class Diagonal - include Printable - - class InvalidLineThickness < StandardError; end - class InvalidColorError < StandardError; end - class InvalidOrientationError < StandardError; end - - attr_reader :line_thickness, :box_width, :box_height, :color, :orientation - - def line_thickness=(thickness) - raise InvalidLineThickness unless thickness.nil? || thickness.to_i.to_s == thickness.to_s - @line_thickness = thickness - end - - def box_width=(width) - @box_width = width - end - - def box_height=(height) - @box_height = height - end - - def color=(value) - raise InvalidColorError unless %w[B W].include?(value&.upcase) - @color = value - end - - def orientation=(value) - raise InvalidOrientationError unless %w[R L].include?(value&.upcase) - @orientation = value - end - - def to_zpl - check_attributes - "^FO#{x},#{y}^GD#{box_width},#{box_height},#{line_thickness},#{color},#{orientation}^FS" - end - - private - - def has_data? - false - end - - def check_attributes - super - raise MissingAttributeError.new("the box_width is not given") unless box_width - raise MissingAttributeError.new("the box_height is not given") unless box_height - end - end - end -end diff --git a/lib/zebra/zpl/graphic.rb b/lib/zebra/zpl/graphic.rb new file mode 100644 index 0000000..2a2a5f2 --- /dev/null +++ b/lib/zebra/zpl/graphic.rb @@ -0,0 +1,79 @@ +require "zebra/zpl/printable" + +module Zebra + module Zpl + class Graphic + include Printable + + class InvalidLineThickness < StandardError; end + class InvalidColorError < StandardError; end + class InvalidOrientationError < StandardError; end + class InvalidGraphicType < StandardError; end + + attr_reader :line_thickness, :graphic_width, :graphic_height, :color, :orientation, :rounding_degree, :graphic_type + attr_writer :rounding_degree + + ELLIPSE = "E" + BOX = "B" + DIAGONAL = "D" + CIRCLE = "C" + SYMBOL = "S" + + def graphic_type=(type) + raise InvalidGraphicType unless %w(E B D C S).include? type + @graphic_type = type + end + + def line_thickness=(thickness) + raise InvalidLineThickness unless thickness.nil? || thickness.to_i.to_s == thickness.to_s + @line_thickness = thickness + end + + def graphic_width=(width) + @graphic_width = width + end + + def graphic_height=(height) + @graphic_height = height + end + + def color=(value) + raise InvalidColorError unless %w[B W].include?(value&.upcase) + @color = value + end + + def orientation=(value) + raise InvalidOrientationError unless %w[R L].include?(value&.upcase) + @orientation = value + end + + def to_zpl + check_attributes + zpl = case graphic_type + when "B" + "B#{graphic_width},#{graphic_height},#{line_thickness},#{color},#{orientation}" + when "E" + "E#{graphic_width},#{graphic_height},#{line_thickness},#{color}" + when "C" + "C#{graphic_width},#{line_thickness},#{color}" + when "D" + "D#{graphic_width},#{graphic_height},#{line_thickness},#{color},#{orientation}" + when "S" + "S#{orientation},#{graphic_height},#{graphic_width}" + end + "^FO#{x},#{y}^G#{zpl}^FS" + end + + private + + def has_data? + false + end + + def check_attributes + super + raise InvalidGraphicType if @graphic_type.nil? + end + end + end +end diff --git a/spec/zebra/zpl/graphics_spec.rb b/spec/zebra/zpl/graphics_spec.rb new file mode 100644 index 0000000..c842dd0 --- /dev/null +++ b/spec/zebra/zpl/graphics_spec.rb @@ -0,0 +1,225 @@ +require 'spec_helper' + +describe Zebra::Zpl::Graphic do + it "can be initialized with graphic type" do + graphic = described_class.new graphic_type: Zebra::Zpl::Graphic::ELLIPSE + expect(graphic.graphic_type).to eq "E" + end + + it "can be initialized with a graphic width" do + graphic = described_class.new graphic_width: 30 + expect(graphic.graphic_width).to eq 30 + end + + it "can be initialized with a graphic height" do + graphic = described_class.new graphic_height: 30 + expect(graphic.graphic_height).to eq 30 + end + + it "can be initialized with a line_thickness" do + graphic = described_class.new line_thickness: 3 + expect(graphic.line_thickness).to eq 3 + end + + it "can be initialized with a color" do + graphic = described_class.new color: "W" + expect(graphic.color).to eq "W" + end + + it "can be initialized with an orientation" do + graphic = described_class.new orientation: "R" + expect(graphic.orientation).to eq "R" + end + + it "can be initialized with a rounding degree" do + graphic = described_class.new rounding_degree: 2 + expect(graphic.rounding_degree).to eq 2 + end + + + + describe "#orientation" do + it "raises an error if the orientation not in [N L]" do + expect { described_class.new orientation: 'A' }.to raise_error(Zebra::Zpl::Graphic::InvalidOrientationError) + end + end + + describe "#color" do + it "raises an error if the color not in [B W]" do + expect { described_class.new color: 'A' }.to raise_error(Zebra::Zpl::Graphic::InvalidColorError) + end + end + + describe "#line_thickness" do + it "raises an error if line thickness is not a number" do + expect { described_class.new line_thickness: 'A' }.to raise_error(Zebra::Zpl::Graphic::InvalidLineThickness) + end + end + + describe "#graphic_type" do + it "raises an error if the graphic type not in [E B D C S]" do + expect { described_class.new graphic_type: 'A' }.to raise_error(Zebra::Zpl::Graphic::InvalidGraphicType) + end + end + + describe "#to_zpl" do + let(:valid_attributes) { { + position: [50, 50], + graphic_width: 200, + graphic_height: 300, + line_thickness: 2, + color: "B", + orientation: "L" + }} + let(:graphic_ellipse) { described_class.new valid_attributes.merge({graphic_type: Zebra::Zpl::Graphic::ELLIPSE}) } + let(:graphic_diagonal) { described_class.new valid_attributes.merge({graphic_type: Zebra::Zpl::Graphic::DIAGONAL})} + let(:graphic_box) { described_class.new valid_attributes.merge({graphic_type: Zebra::Zpl::Graphic::BOX}) } + let(:graphic_symbol) { described_class.new valid_attributes.merge({graphic_type: Zebra::Zpl::Graphic::SYMBOL}) } + let(:graphic_circle) { described_class.new valid_attributes.merge({graphic_type: Zebra::Zpl::Graphic::CIRCLE}) } + + let(:tokens_ellipse) { graphic_ellipse.to_zpl.split(/(\^[A-Z]+|\,)/).reject{ |e| ['', ',', nil].include?(e) } } + let(:tokens_diagonal) { graphic_diagonal.to_zpl.split(/(\^[A-Z]+|\,)/).reject{ |e| ['', ',', nil].include?(e) } } + let(:tokens_box) { graphic_box.to_zpl.split(/(\^[A-Z]+|\,)/).reject{ |e| ['', ',', nil].include?(e) } } + let(:tokens_symbol) { graphic_symbol.to_zpl.split(/(\^[A-Z]+|\,)/).reject{ |e| ['', ',', nil].include?(e) } } + let(:tokens_circle) { graphic_circle.to_zpl.split(/(\^[A-Z]+|\,)/).reject{ |e| ['', ',', nil].include?(e) } } + + it "raises an error if the X position is not given" do + graphic = described_class.new position: [nil, 50], graphic_type: described_class::ELLIPSE + expect { + graphic.to_zpl + }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the X value is not given") + end + + it "raises an error if the Y position is not given" do + graphic = described_class.new position: [50, nil], graphic_type: described_class::ELLIPSE + expect { + graphic.to_zpl + }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the Y value is not given") + end + + it "raises an error if Graphic Type is not given" do + graphic = described_class.new position: [50, 50] + expect { + graphic.to_zpl + }.to raise_error(Zebra::Zpl::Graphic::InvalidGraphicType) + end + + it "contains the X position" do + expect(tokens_ellipse[2]).to eq "50" + end + + it "contains the Y position" do + expect(tokens_ellipse[1]).to eq "50" + end + + #Elipse Attributes + + it "ellipse contains the ellipse graphic command '^GE'" do + expect(tokens_ellipse[3]).to eq "^GE" + end + + it "ellipse contains the graphic width" do + expect(tokens_ellipse[4]).to eq "200" + end + + it "ellipse contains the graphic height" do + expect(tokens_ellipse[5]).to eq "300" + end + + it "ellipse contains the line thickness" do + expect(tokens_ellipse[6]).to eq "2" + end + + it "ellipse contains the color" do + expect(tokens_ellipse[7]).to eq "B" + end + + #Box Attributes + + it "box contains the box graphic command '^GB'" do + expect(tokens_box[3]).to eq "^GB" + end + + it "box contains the graphic width" do + expect(tokens_box[4]).to eq "200" + end + + it "box contains the graphic height" do + expect(tokens_box[5]).to eq "300" + end + + it "box contains the line thickness" do + expect(tokens_box[6]).to eq "2" + end + + it "box contains the color" do + expect(tokens_box[7]).to eq "B" + end + + it "box contains the orientation" do + expect(tokens_box[8]).to eq "L" + end + + #Circle Attributes + + it "circle contains the circle graphic command '^GC'" do + expect(tokens_circle[3]).to eq "^GC" + end + + it "circle contains the graphic width" do + expect(tokens_circle[4]).to eq "200" + end + + it "circle contains the line thickness" do + expect(tokens_circle[5]).to eq "2" + end + + it "circle contains the color" do + expect(tokens_circle[6]).to eq "B" + end + + #Diagonal Attributes + + it "diagonal contains the diagonal graphic command '^GD'" do + expect(tokens_diagonal[3]).to eq "^GD" + end + + it "diagonal contains the graphic width" do + expect(tokens_diagonal[4]).to eq "200" + end + + it "diagonal contains the graphic width" do + expect(tokens_diagonal[5]).to eq "300" + end + + it "diagonal contains the line thickness" do + expect(tokens_diagonal[6]).to eq "2" + end + + it "diagonal contains the color" do + expect(tokens_diagonal[7]).to eq "B" + end + + it "diagonal contains the orientation" do + expect(tokens_diagonal[8]).to eq "L" + end + + #Symbol Attributes + + it "symbol contains the symbol graphic command '^GS'" do + expect(tokens_symbol[3][0..2]).to eq "^GS" + end + + it "symbol contains the orientation" do + expect(tokens_symbol[3][3]).to eq "L" + end + + it "symbol contains the graphic height" do + expect(tokens_symbol[4]).to eq "300" + end + + it "symbol contains the graphic width" do + expect(tokens_symbol[5]).to eq "200" + end + end +end