Sha256: 41a3dcc1ce0665f6fb0e7260e4a63abeb5c39a588ffdb8d047e5362820afa8b1

Contents?: true

Size: 1.44 KB

Versions: 13

Compression:

Stored size: 1.44 KB

Contents

module Eancom
  module Edifact
    class Body < DocumentElement

      attr_accessor :hash, :items, :messages

      def initialize
        super
        @hash = {}
        @messages = []
      end

      def to_json_hash
        @segments.each do |segment|
          start_message(segment) if segment.starts_message?
          start_item(segment) if segment.starts_item?
          if @item
            @item << segment
          else
            @message << segment
          end
        end
        @message.add_item(@item.to_json_hash)
        @messages << @message.to_json_hash
        hash.merge!({ messages: messages })
        { body: hash }
      end

      def start_message(segment)
        if @message
          @messages << @message.to_json_hash
          if @item && !@item.empty?
            @message.add_item(@item.to_json_hash)
          end
          @item = nil
        end
        @message = Eancom::Edifact::Message.new()
      end

      def start_item(segment)
        if @item && !@item.empty?
          @message.add_item(@item.to_json_hash)
        end
        @item = Eancom::Edifact::Item.new()
      end

      private

      def merge(hash, 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 group_name(segment)
        return segment.group_name
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

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