Difference is used to find difference between 2 objects of same class.
<img src=“https://badge.fury.io/rb/difference.png” alt=“Gem Version” /> <img src=“https://travis-ci.org/gouravtiwari/difference.png?branch=master” alt=“Build Status” />
Supported versions Ruby 1.9.3+
Usage
-
Paste this line in your Gemfile
gem 'difference'
-
This will give your active record objects a method called ‘differs_from’. This is how you use it.
Eg.
Class Foo < ActiveRecord::Base end a=Foo.find 1 b=Foo.find 3
Now you can use,
a.differs_from b
will return a hash:
{:attr1=>[<value of attr1 in a>,<value of attr1 in b>], :attr2=>[<value of attr2 in a>,<value of attr2 in b>]} if both values are different in a and b.
Options for differs_from:
-
ignore_attributes: This options helps to ignore attributes from the AR objects while comparison.
a.differs_from b, :ignore_attributes=>[:id, :created_at]
-
only_attribues: This options helps to compare only the specified attributes.
a.differs_from b :only_attributes=> :name
Tests This repo uses very lightweight test-suite ‘minitest’ To run tests:
-
$ bundle install #at root
-
$ ruby test/test_difference.rb
License Copyright © 2012 Manish Puri Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.