Sha256: 8a1a208919024f8f78a455da9540970ab8c3745aec3568eadbc65edd64a0f363

Contents?: true

Size: 1.83 KB

Versions: 15

Compression:

Stored size: 1.83 KB

Contents

module Eancom
  module Edifact
    class Structure

      class KeyNotFoundError < StandardError; end

      attr_reader :tag
      attr_accessor :composites

      def initialize(tag:)
        @tag = tag
        @composites = []
      end

      def <<(composite)
        @composites << composite
      end

      def validate!(key, value)
        data = find(key)
        data.valid?(key, value)
      end

      # TODO: Find on Array??
      def find(key)
        @composites.each do |composite|
          data = composite.get(key)
          return data if data
        end
        raise KeyNotFoundError.new("Key #{key} not found.")
      end

      def build_hash(array)
        hash = {}
        structure_array = structure_array()
        structure_array.each_with_index do |v1, i1|
          v1.each_with_index do |v2, i2|
            if array[i1] && array[i1][i2]
              hash[structure_array[i1][i2]] = array[i1][i2]
            end
          end
        end
        hash
      end

      def build_array(hash)
        array = []
        structure_array = structure_array()
        structure_array.each_with_index do |v1, i1|
          inner_array = []
          v1.each_with_index do |v2, i2|
            inner_array << hash[v2]
          end
          array << inner_array
        end
        array
      end

      def to_s
        structure_array
      end

      def dictionary_lookup(identifier, value)
        if data = find(identifier)
          dictionary = data.dictionary
          dictionary.each do |k, v|
            if v[:identifier] == value
              return k
            end
          end
          value
        else
          value
        end
      end

      private

      def structure_array
        array = []
        @composites.each do |c|
          array << c.to_array
        end
        array
      end

    end

  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
eancom-1.6.3 lib/eancom/edifact/structure.rb
eancom-1.6.2 lib/eancom/edifact/structure.rb
eancom-1.6.0 lib/eancom/edifact/structure.rb
eancom-1.5.7 lib/eancom/edifact/structure.rb
eancom-1.5.6 lib/eancom/edifact/structure.rb
eancom-1.5.5 lib/eancom/edifact/structure.rb
eancom-1.5.4 lib/eancom/edifact/structure.rb
eancom-1.5.2 lib/eancom/edifact/structure.rb
eancom-1.5.1 lib/eancom/edifact/structure.rb
eancom-1.5.0 lib/eancom/edifact/structure.rb
eancom-1.4.0 lib/eancom/edifact/structure.rb
eancom-1.3.0 lib/eancom/edifact/structure.rb
eancom-1.2.0 lib/eancom/edifact/structure.rb
eancom-1.1.1 lib/eancom/edifact/structure.rb
eancom-1.1.0 lib/eancom/edifact/structure.rb