class Eancom::Edifact::Structure

Attributes

composites[RW]
tag[R]

Public Class Methods

new(tag:) click to toggle source
# File lib/eancom/edifact/structure.rb, line 10
def initialize(tag:)
  @tag = tag
  @composites = []
end

Public Instance Methods

<<(composite) click to toggle source
# File lib/eancom/edifact/structure.rb, line 15
def <<(composite)
  @composites << composite
end
build_array(hash) click to toggle source
# File lib/eancom/edifact/structure.rb, line 46
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
build_hash(array) click to toggle source
# File lib/eancom/edifact/structure.rb, line 33
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
dictionary_lookup(identifier, value) click to toggle source
# File lib/eancom/edifact/structure.rb, line 63
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
find(key) click to toggle source

TODO: Find on Array??

# File lib/eancom/edifact/structure.rb, line 25
def find(key)
  @composites.each do |composite|
    data = composite.get(key)
    return data if data
  end
  raise KeyNotFoundError.new("Key #{key} not found.")
end
to_s() click to toggle source
# File lib/eancom/edifact/structure.rb, line 59
def to_s
  structure_array
end
validate!(key, value) click to toggle source
# File lib/eancom/edifact/structure.rb, line 19
def validate!(key, value)
  data = find(key)
  data.valid?(key, value)
end

Private Instance Methods

structure_array() click to toggle source
# File lib/eancom/edifact/structure.rb, line 78
def structure_array
  array = []
  @composites.each do |c|
    array << c.to_array
  end
  array
end