Sha256: 3413b172711b2188e7a6505d7945c6684e1ddb2341ec3370fa0f2b608268ee60

Contents?: true

Size: 869 Bytes

Versions: 2

Compression:

Stored size: 869 Bytes

Contents

# frozen_string_literal: true

module Eancom
  module Edifact
    class Body < DocumentElement
      attr_accessor :hash, :messages, :locations, :items

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

      def to_json_hash
        @to_json_hash ||= build_json_hash
      end

      def start_message
        if @message
          @messages << @message.to_json_hash
        end
        @message = Eancom::Edifact::Message.new
      end

      private

      def group_name(segment)
        segment.group_name
      end

      def build_json_hash
        @segments.each do |segment|
          start_message if segment.starts_message?
          @message << segment
        end
        messages << @message.to_json_hash if @message

        {
          body: hash.merge({ messages: messages })
        }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
eancom-1.6.3 lib/eancom/edifact/body.rb
eancom-1.6.2 lib/eancom/edifact/body.rb