Sha256: 0f798b05145b7ce82cbd0b3f8afc877ffbdd44935e69b6159ba8c6c6572fbfbc

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

module GS1
  module Barcode
    # Base class for a barcode.
    #
    class Base
      include Definitions

      def initialize(options = {})
        options.each do |(attribute_name, data)|
          validate_attribute_data(attribute_name)
          validate_attribute_record(attribute_name) do |record|
            instance_variable_set("@#{attribute_name}", record.new(data))
          end
        end

        @params_order = options.to_h.keys
      end

      def errors
        @errors ||= Errors.new
      end

      class << self
        def from_scan!(barcode, separator: GS1.configuration.barcode_separator)
          new(scan_to_params!(barcode, separator: separator))
        end

        def from_scan(barcode, separator: GS1.configuration.barcode_separator)
          new(scan_to_params(barcode, separator: separator))
        end

        def scan_to_params!(barcode, separator: GS1.configuration.barcode_separator)
          Tokenizer.new(barcode, separator: separator).to_params!
        end

        def scan_to_params(barcode, separator: GS1.configuration.barcode_separator)
          Tokenizer.new(barcode, separator: separator).to_params
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gs1-1.1.0 lib/gs1/barcode/base.rb
gs1-1.0.0 lib/gs1/barcode/base.rb