lib/eancom/edifact/message.rb in eancom-0.1.0 vs lib/eancom/edifact/message.rb in eancom-1.1.0

- old
+ new

@@ -1,32 +1,40 @@ module Eancom module Edifact - class Message < Element + class Message + attr_accessor :items, :hash + def initialize() - @me = Segment.new - @segments = [@me] + @items = [] + @hash = {} end - def add_segment(segment) - @segments << segment + def add_item(item) + @items << item end - def composite(composite) - @me.composite(composite) + 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 name=(name) - @me.name = name + def to_json_hash + message_hash = {} + items_hash = { items: @items } + message_hash.merge!(@hash) + message_hash.merge!(items_hash) + message_hash end - def to_s - stream = "" - @segments.each do |segment| - stream << segment.to_s - stream << DELIMITERS[:segment] - end - stream + private + + def group_name(segment) + segment.group_name end end end end