Class: PuppetX::Puppetlabs::Swagger::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_x/puppetlabs/swagger/fuzzy_compare.rb

Class Method Summary (collapse)

Class Method Details

Method: PuppetX::Puppetlabs::Swagger::Utils.fuzzy_compare

Defined in:
lib/puppet_x/puppetlabs/swagger/fuzzy_compare.rb

+ (Object) fuzzy_compare(existing, intended)



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/puppet_x/puppetlabs/swagger/fuzzy_compare.rb', line 9

def self.fuzzy_compare(existing, intended)
  normalized_is = existing.symbolize_keys.fixnumify
  normalized_should = intended.symbolize_keys.fixnumify
  if normalized_is.all? { |k,v| v.class == String }
    diff = normalized_is.merge(normalized_should)
    diff == normalized_is
  elsif normalized_is == normalized_should
    true
  else
    tests = test_complex_structure(normalized_should, normalized_is)
    tests.flatten.compact.all?
  end
end

Method: PuppetX::Puppetlabs::Swagger::Utils.test_complex_structure

Defined in:
lib/puppet_x/puppetlabs/swagger/fuzzy_compare.rb

+ (Object) test_complex_structure(normalized_should, normalized_is)



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/puppet_x/puppetlabs/swagger/fuzzy_compare.rb', line 22

def self.test_complex_structure(normalized_should, normalized_is)
  normalized_should.keys.collect do |key|
    klass = normalized_is[key].class
    if [String, Fixnum].include? klass
      normalized_is[key].to_s == normalized_should[key].to_s
    elsif klass == Array
      normalized_is[key].collect do |is_value|
        normalized_should[key].collect do |should_value|
          diff = if is_value.class == Hash
            is_value.merge(should_value)
          else
            should_value
          end
          diff == is_value
        end
      end
    elsif klass == Hash
      diff = normalized_is[key].merge(normalized_should[key])
      if diff == normalized_is[key]
        true
      else
        normalized_should[key].keys.collect do |inner_key|
          test_complex_structure(normalized_should[key][inner_key], normalized_is[key][inner_key])
        end
      end
    end
  end
end