Class: StructureFlatter::ArrayHashStructure

Inherits:
Object
  • Object
show all
Defined in:
lib/structure_flatter/array_hash_structure.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array_hash_structure = nil) ⇒ ArrayHashStructure

Initializing element passed structure should be a simple or complex structure comprising Hashes and Arrays



10
11
12
# File 'lib/structure_flatter/array_hash_structure.rb', line 10

def initialize(array_hash_structure = nil)
  @ahs = array_hash_structure
end

Instance Attribute Details

#ahsObject (readonly)

Returns the value of attribute ahs.



6
7
8
# File 'lib/structure_flatter/array_hash_structure.rb', line 6

def ahs
  @ahs
end

Instance Method Details

#flatten_structure!(strt = ahs) ⇒ Object

Flattening the given structure



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/structure_flatter/array_hash_structure.rb', line 15

def flatten_structure!(strt = ahs)
  case strt
    when Array
      strt.each do |array_element|
        flatten_structure!(array_element) if ((array_element.class == Array) || (array_element.class == Hash))
      end
    when Hash
      strt.each do |hash_key, hash_value|
        if hash_value.class == Array
          # trying to flatten the value
          flatten_key_value_pair!(hash_key, hash_value)
          # recursive call
          flatten_structure!(hash_value)
        elsif hash_value.class == Hash
          hash_value.each do |hash_value_key, hash_value_value|
            # recursive call
            flatten_structure!(hash_value_value)
          end
        else
          # do nothing
        end
      end
    else
      # do nothing
  end
  strt
end