Sha256: c1b8bcc1997fa867beb72d71f54a99b178480b73310e4772271cc7491df34a9a

Contents?: true

Size: 1.62 KB

Versions: 4

Compression:

Stored size: 1.62 KB

Contents

require "zebra/zpl/printable"

module Zebra
  module Zpl
    class Barcode
      include Printable

      class InvalidNarrowBarWidthError < StandardError; end
      class InvalidWideBarWidthError   < StandardError; end

      attr_accessor :height
      attr_reader :type, :narrow_bar_width, :wide_bar_width, :width
      attr_writer :print_human_readable_code

      def width=(width)
        @width = width || 0
      end

      def type=(type)
        BarcodeType.validate_barcode_type(type)
        @type = type
      end

      def narrow_bar_width=(width)
        raise InvalidNarrowBarWidthError unless (1..10).include?(width.to_i)
        @narrow_bar_width = width
      end

      def wide_bar_width=(width)
        raise InvalidWideBarWidthError unless (2..30).include?(width.to_i)
        @wide_bar_width = width
      end

      def print_human_readable_code
        @print_human_readable_code || false
      end

      def to_zpl
        check_attributes
        human_readable = print_human_readable_code ? "Y" : "N"
        "^FW#{rotation}^FO#{x},#{y}^BY#{narrow_bar_width}^B#{type}#{rotation},#{height},#{human_readable}^FD#{data}^FS"
      end

      private

      def check_attributes
        super
        raise MissingAttributeError.new("the barcode type to be used is not given") unless @type
        raise MissingAttributeError.new("the height to be used is not given") unless @height
        raise MissingAttributeError.new("the narrow bar width to be used is not given") unless @narrow_bar_width
        raise MissingAttributeError.new("the wide bar width to be used is not given") unless @wide_bar_width
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
zebra-zpl-1.0.5 lib/zebra/zpl/barcode.rb
zebra-zpl-1.0.2 lib/zebra/zpl/barcode.rb
zebra-zpl-1.0.1 lib/zebra/zpl/barcode.rb
zebra-zpl-1.0.0 lib/zebra/zpl/barcode.rb