Sha256: 0ca9e1e85077a5207d3dc8b9f92c137793e3810642185f9f692effb2eb65de7c

Contents?: true

Size: 1.38 KB

Versions: 14

Compression:

Stored size: 1.38 KB

Contents

module Eancom
  module Parser
    class Segment

      attr_accessor :tag, :components, :string

      def initialize(string)
        @string = string
        @components = []
      end

      def parse
        parse_component
        klass = find_segment_class
        structure = find_structure
        klass.initialize_by_components(structure, @components)
      end

      private

      def parse_component
       if string =~ /^UNA/
         parse_una
       else
          string.split(data_delimiter).each do |data|
            components << parse_data(data)
          end
       end
      end

      def parse_data(data)
        component = []
        data.split(component_delimiter).each do |c|
          component << c
        end
        component
      end

      def parse_una
        components << [string[0..2]]
        components << [string[3]]
        components << [string[4]]
        components << [string[5]]
        components << [string[6]]
        components << [string[7]]
        components << [DELIMITERS[:segment]]
      end

      def tag
        components.first.first
      end

      def find_segment_class
        Eancom.find_segment(tag: tag)
      end

      def find_structure
        Eancom.find_structure(tag: tag)
      end

      def data_delimiter
        DELIMITERS[:data]
      end

      def component_delimiter
        DELIMITERS[:component]
      end

    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
eancom-1.6.0 lib/eancom/parser/segment.rb
eancom-1.5.7 lib/eancom/parser/segment.rb
eancom-1.5.6 lib/eancom/parser/segment.rb
eancom-1.5.5 lib/eancom/parser/segment.rb
eancom-1.5.4 lib/eancom/parser/segment.rb
eancom-1.5.2 lib/eancom/parser/segment.rb
eancom-1.5.1 lib/eancom/parser/segment.rb
eancom-1.5.0 lib/eancom/parser/segment.rb
eancom-1.4.0 lib/eancom/parser/segment.rb
eancom-1.3.0 lib/eancom/parser/segment.rb
eancom-1.2.0 lib/eancom/parser/segment.rb
eancom-1.1.1 lib/eancom/parser/segment.rb
eancom-1.1.0 lib/eancom/parser/segment.rb
eancom-2.0.0 lib/eancom/parser/segment.rb