Class: Eancom::Edifact::Structure

Inherits:
Object
  • Object
show all
Defined in:
lib/eancom/edifact/structure.rb

Defined Under Namespace

Classes: KeyNotFoundError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag:) ⇒ Structure

Returns a new instance of Structure.



10
11
12
13
# File 'lib/eancom/edifact/structure.rb', line 10

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

Instance Attribute Details

#compositesObject

Returns the value of attribute composites.



8
9
10
# File 'lib/eancom/edifact/structure.rb', line 8

def composites
  @composites
end

#tagObject (readonly)

Returns the value of attribute tag.



7
8
9
# File 'lib/eancom/edifact/structure.rb', line 7

def tag
  @tag
end

Instance Method Details

#<<(composite) ⇒ Object



15
16
17
# File 'lib/eancom/edifact/structure.rb', line 15

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

#build_array(hash) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# 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) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# 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) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# 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
    value
  else
    value
  end
end

#find(key) ⇒ Object

TODO: Find on Array??

Raises:



25
26
27
28
29
30
31
# 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_sObject



59
60
61
# File 'lib/eancom/edifact/structure.rb', line 59

def to_s
  structure_array
end

#validate!(key, value) ⇒ Object



19
20
21
22
# File 'lib/eancom/edifact/structure.rb', line 19

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