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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/convection/model/template.rb', line 142

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



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/convection/model/template.rb', line 162

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

  memo
end