Class: Draisine::HashDiff
- Inherits:
-
Struct
- Object
- Struct
- Draisine::HashDiff
- Defined in:
- lib/draisine/util/hash_diff.rb
Instance Attribute Summary collapse
-
#added ⇒ Object
Returns the value of attribute added.
-
#changed ⇒ Object
Returns the value of attribute changed.
-
#removed ⇒ Object
Returns the value of attribute removed.
-
#unchanged ⇒ Object
Returns the value of attribute unchanged.
Class Method Summary collapse
Instance Method Summary collapse
Instance Attribute Details
#added ⇒ Object
Returns the value of attribute added
2 3 4 |
# File 'lib/draisine/util/hash_diff.rb', line 2 def added @added end |
#changed ⇒ Object
Returns the value of attribute changed
2 3 4 |
# File 'lib/draisine/util/hash_diff.rb', line 2 def changed @changed end |
#removed ⇒ Object
Returns the value of attribute removed
2 3 4 |
# File 'lib/draisine/util/hash_diff.rb', line 2 def removed @removed end |
#unchanged ⇒ Object
Returns the value of attribute unchanged
2 3 4 |
# File 'lib/draisine/util/hash_diff.rb', line 2 def unchanged @unchanged end |
Class Method Details
.diff(hash1, hash2, equality = -> (a, b) { a == b }) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/draisine/util/hash_diff.rb', line 3 def self.diff(hash1, hash2, equality = -> (a, b) { a == b }) unless hash1.respond_to?(:key?) && hash2.respond_to?(:key?) fail ArgumentError, "both arguments should be hashes" end added = [] removed = [] changed = [] unchanged = [] (hash1.keys | hash2.keys).each do |key| if hash1.key?(key) && hash2.key?(key) if equality.call(hash1[key], hash2[key]) unchanged << key else changed << key end elsif hash1.key?(key) removed << key else added << key end end new(added, removed, changed, unchanged) end |
.sf_diff(hash1, hash2) ⇒ Object
30 31 32 |
# File 'lib/draisine/util/hash_diff.rb', line 30 def self.sf_diff(hash1, hash2) diff(hash1, hash2, SalesforceComparisons.method(:salesforce_equals?)) end |
Instance Method Details
#diff_keys ⇒ Object
34 35 36 |
# File 'lib/draisine/util/hash_diff.rb', line 34 def diff_keys changed | added | removed end |