Skip to content

Commit

Permalink
Merge pull request #42 from LagTag/graphic_class
Browse files Browse the repository at this point in the history
Consolidate Graphic Elements to One Class
  • Loading branch information
mtking2 authored Oct 31, 2019
2 parents 6a81605 + 03ac057 commit bb342f6
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 101 deletions.
3 changes: 1 addition & 2 deletions lib/zebra/zpl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -19,4 +17,5 @@
require 'zebra/zpl/pdf417'
require 'zebra/zpl/justification'
require 'zebra/zpl/raw'
require 'zebra/zpl/graphic'
require 'zebra/zpl/datamatrix'
1 change: 1 addition & 0 deletions lib/zebra/zpl/box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
44 changes: 0 additions & 44 deletions lib/zebra/zpl/circle.rb

This file was deleted.

55 changes: 0 additions & 55 deletions lib/zebra/zpl/diagonal.rb

This file was deleted.

79 changes: 79 additions & 0 deletions lib/zebra/zpl/graphic.rb
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit bb342f6

Please sign in to comment.