module Eancom module Edifact class Message attr_accessor :items, :hash def initialize() @items = [] @hash = {} end def add_item(item) @items << item end def << (segment) if name = group_name(segment) @hash[name] = [] if @hash[name].nil? @hash[name] << segment.to_json_hash else @hash.merge!(segment.to_json_hash) end end def to_json_hash message_hash = {} items_hash = { items: @items } message_hash.merge!(@hash) message_hash.merge!(items_hash) message_hash end private def group_name(segment) segment.group_name end end end end