Class: StructureFlatter::ArrayHashStructure
- Inherits:
-
Object
- Object
- StructureFlatter::ArrayHashStructure
- Defined in:
- lib/structure_flatter/array_hash_structure.rb
Class Method Summary collapse
-
.flatten_key_value_pair!(key, value) ⇒ Object
it tries to flatten the given key value pair.
-
.is_key_value_pair_flattable?(key, value) ⇒ Boolean
check if given key value pair is flattable.
Instance Method Summary collapse
-
#flatten_structure!(array_hash_structure = @array_hash_structure) ⇒ Object
it flattens the given structure.
-
#initialize(array_hash_structure = nil) ⇒ ArrayHashStructure
constructor
initializing element passed structure should be a simple or complex structure comprising Hashes and Arrays.
Constructor Details
#initialize(array_hash_structure = nil) ⇒ ArrayHashStructure
initializing element passed structure should be a simple or complex structure comprising Hashes and Arrays
8 9 10 |
# File 'lib/structure_flatter/array_hash_structure.rb', line 8 def initialize(array_hash_structure = nil) @array_hash_structure = array_hash_structure end |
Class Method Details
.flatten_key_value_pair!(key, value) ⇒ Object
it tries to flatten the given key value pair
42 43 44 45 46 47 48 49 |
# File 'lib/structure_flatter/array_hash_structure.rb', line 42 def self.flatten_key_value_pair!(key, value) if self.is_key_value_pair_flattable?(key, value) value.each_index do |index| value[index] = value[index].values.first end end return [key, value] end |
.is_key_value_pair_flattable?(key, value) ⇒ Boolean
check if given key value pair is flattable
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/structure_flatter/array_hash_structure.rb', line 52 def self.is_key_value_pair_flattable?(key, value) # key should be plural return false unless key.to_s.pluralize == key.to_s # value should be Array return false unless value.class == Array # checking each element of value (which is Array) value.each do |value_element| # should be hash return false unless value_element.class == Hash # it should have only one key-value pair return false unless value_element.count == 1 # its key should be singular form of original key return false unless (value_element.keys.first.to_s != key.to_s) && (value_element.keys[0].to_s.pluralize == key.to_s) end return true end |
Instance Method Details
#flatten_structure!(array_hash_structure = @array_hash_structure) ⇒ Object
it flattens the given structure
13 14 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 |
# File 'lib/structure_flatter/array_hash_structure.rb', line 13 def flatten_structure!(array_hash_structure = @array_hash_structure) case array_hash_structure when Array array_hash_structure.each do |array_element| flatten_structure!(array_element) if ((array_element.class == Array) || (array_element.class == Hash)) end when Hash array_hash_structure.each do |hash_key, hash_value| if hash_value.class == Array # trying to flatten the value ArrayHashStructure.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 return array_hash_structure end |