Sha256: d65d046faa163894c234d3116926995e709d41ee697758fa715acc5b835a9258

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module Eancom
  module Edifact
    class Location
      attr_accessor :items, :hash

      def initialize
        @items = []
        @hash = {}
      end

      def add_item(item)
        @items << item
      end

      def <<(segment)
        start_item if segment.starts_item?

        if @item
          @item << segment
        elsif 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
        if @item && !@item.empty?
          add_item(@item.to_json_hash)
        end

        @hash.merge({ items: @items })
      end

      def empty?
        @hash.empty?
      end

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

      private

      def group_name(segment)
        segment.item_group_name || segment.group_name
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
eancom-1.6.3 lib/eancom/edifact/location.rb
eancom-1.6.2 lib/eancom/edifact/location.rb
eancom-1.6.0 lib/eancom/edifact/location.rb