Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/convection/model/template.rb

Overview

HACK: Add generic diff(other) and properties to Hash and Array

Instance Method Summary collapse

Instance Method Details

#diff(other = {}) ⇒ Object

Use flattened properties to calculate a diff



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/convection/model/template.rb', line 164

def diff(other = {})
  our_properties = properties
  their_properties = other.properties

  (our_properties.keys + their_properties.keys).uniq.each_with_object({}) do |key, memo|
    next if (our_properties[key] == their_properties[key] rescue false)

    ## HACK: String/Number/Symbol comparison
    if our_properties[key].is_a?(Numeric) ||
       their_properties[key].is_a?(Numeric) ||
       our_properties[key].is_a?(Symbol) ||
       their_properties[key].is_a?(Symbol)
      next if our_properties[key].to_s == their_properties[key].to_s
    end

    memo[key] = [our_properties[key], their_properties[key]]
  end
end

#properties(memo = {}, path = '') ⇒ Object

Recursivly flatten a hash into 1st order key/value pairs



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/convection/model/template.rb', line 184

def properties(memo = {}, path = '')
  keys.each do |key|
    if self[key].is_a?(Hash) || self[key].is_a?(Array)
      new_path = "#{path}#{path.empty? ? '' : '.'}#{key}"
      resource_type = self['Type']
      new_path = "#{new_path}.#{resource_type}" if resource_type && !resource_type.empty?
      self[key].properties(memo, new_path)
    else
      memo["#{path}.#{key}"] = self[key]
    end
  end

  memo
end