class Hash

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

Public Instance Methods

diff(other = {}) click to toggle source

Use flattened properties to calculate a diff

# File lib/convection/model/template.rb, line 180
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 = '') click to toggle source

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

# File lib/convection/model/template.rb, line 200
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