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 else value end end private def structure_array array = [] @composites.each do |c| array << c.to_array end array end end end end