HappyMapper::Differ
In the unlikely scenario you use HappyMapper and you need to find differences between two documents, this could be a solution.
Ok, your probably won’t use or need this. But since I enjoy writing to myself here is how to use it.
Note: this library is wildly memory inefficient
````ruby
def left
%Q
end
def right
%Q
end
require ‘happymapper_differ’
class Person include HappyMapper
tag ‘person’ attribute :name, String has_many :items, “Item” end
class Item include HappyMapper has_one :name, String end
result = HappyMapper::Differ.new(Person.parse(left), Person.parse(right)).diff
result.changed? #=> true
result.changes #=> “items”=>[#<Item:0x007ffd2d8d5a68 @name=”Red Box”>, #<Item:0x007ffd2d8d5630 @name=”Cup”>]
result.name.changed? #=> true
result.name.was #=> “Carl”
result.items[0].changed? #=> false result.items[1].changes? #=> Box” result.items[2].changed? #=> true result.items[2] #=> nil result.items[2].was #=> #<Item:0x007fde2c045ee0 @name=”Cup”> ````
Extended methods
HappyMapper::Differ extends each HappyMapper instance and element with the following methods:
method | purpose |
---|---|
changed? | returns true if this element, or a child has changed |
changes | returns a hash with each element or attribute which changed, and changed value |
was | returns the prior object from the right. If there is no change this will be the current value. |
Contributing
- Fork it ( https://github.com/pharos-ie/happymapper-differ/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request