module Eancom module Parser class Segment attr_accessor :tag, :string, :components, :segment_class def initialize(string) @string = string @components = [] @tag = una? ? 'UNA' : string.split(data_delimiter)[0] @segment_class = find_segment_class end def parse parse_component klass = find_segment_class structure = find_structure klass.initialize_by_components(structure, @components) end private def parse_component if 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 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 def una? string =~ /^UNA/ end end end end