Class: ForestLiana::IsSameDataStructureHelper::Analyser

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/forest_liana/is_same_data_structure_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(object, other, deep = 0) ⇒ Analyser

Returns a new instance of Analyser.



6
7
8
9
10
# File 'app/helpers/forest_liana/is_same_data_structure_helper.rb', line 6

def initialize(object, other, deep = 0)
  @object = object
  @other = other
  @deep = deep
end

Instance Method Details

#are_objects(object, other) ⇒ Object



12
13
14
# File 'app/helpers/forest_liana/is_same_data_structure_helper.rb', line 12

def are_objects(object, other)
  object && other && object.is_a?(Hash) && other.is_a?(Hash)
end

#check_keys(object, other, step = 0) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/helpers/forest_liana/is_same_data_structure_helper.rb', line 16

def check_keys(object, other, step = 0)
  unless are_objects(object, other)
    return false
  end

  object_keys = object.keys
  other_keys = other.keys

  if object_keys.length != other_keys.length
    return false
  end

  object_keys_set = object_keys.to_set
  other_keys.each { |key|
    if !object_keys_set.member?(key) || (step + 1 <= @deep && !check_keys(object[key], other[key], step + 1))
      return false
    end
  }

  return true
end

#performObject



38
39
40
# File 'app/helpers/forest_liana/is_same_data_structure_helper.rb', line 38

def perform
  check_keys(@object, @other)
end