difference 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +42 -0
- data/lib/difference/version.rb +1 -1
- data/lib/difference.rb +9 -6
- metadata +6 -7
data/README.rdoc
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
|
2
|
+
Difference is used to find difference between 2 objects of same class.
|
3
|
+
|
4
|
+
|
5
|
+
Usage
|
6
|
+
1. Paste this line in your Gemfile
|
7
|
+
|
8
|
+
gem 'difference'
|
9
|
+
|
10
|
+
2. This will give your active record objects a method called 'differs_from'. This is how you use it.
|
11
|
+
Eg.
|
12
|
+
Class Foo < ActiveRecord::Base
|
13
|
+
end
|
14
|
+
|
15
|
+
a=Foo.find 1
|
16
|
+
b=Foo.find 3
|
17
|
+
|
18
|
+
Now you can use,
|
19
|
+
a.differs_from b
|
20
|
+
|
21
|
+
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.
|
22
|
+
|
23
|
+
Options for differs_from:
|
24
|
+
1. ignore_attributes: This options helps to ignore attributes from the AR objects while comparison.
|
25
|
+
Eg:
|
26
|
+
a.differs_from b, :ignore_attributes=>[:id, :created_at]
|
27
|
+
|
28
|
+
2. only_attribues: This options helps to compare only the specified attributes.
|
29
|
+
a.differs_from b :only_attributes=> :name
|
30
|
+
|
31
|
+
|
32
|
+
TODO:
|
33
|
+
#specs
|
34
|
+
|
35
|
+
|
36
|
+
License
|
37
|
+
Copyright © 2012 Manish Puri
|
38
|
+
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:
|
39
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
40
|
+
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.
|
41
|
+
|
42
|
+
|
data/lib/difference/version.rb
CHANGED
data/lib/difference.rb
CHANGED
@@ -3,20 +3,23 @@ require "difference/version"
|
|
3
3
|
module Difference
|
4
4
|
class ActiveRecord::Base
|
5
5
|
def differs_from(object,opt={})
|
6
|
-
|
6
|
+
raise "Can't compare objects of different Class." if self.class.name != object.class.name
|
7
7
|
|
8
8
|
# Set attribute list to compare.
|
9
9
|
diff={}
|
10
|
-
attr_list =self.attributes.keys
|
11
|
-
attr_list= attr_list - opt[:ignore_attributes]
|
12
|
-
attr_list= opt[:only_attributes]
|
13
|
-
|
14
|
-
|
10
|
+
attr_list = clean_attr(self.attributes.keys)
|
11
|
+
attr_list = attr_list - clean_attr(opt[:ignore_attributes])
|
12
|
+
attr_list = attr_list & clean_attr(opt[:only_attributes]) if opt[:only_attributes].present?
|
15
13
|
attr_list.each do |attr|
|
16
14
|
(diff[attr.to_sym] = [self[attr],object[attr]]) if self[attr] != object[attr]
|
17
15
|
end
|
18
16
|
diff
|
19
17
|
end
|
18
|
+
|
19
|
+
private
|
20
|
+
def clean_attr(collection)
|
21
|
+
collection.present? ? collection.to_a.collect{|e| e.to_sym if e.present?} : []
|
22
|
+
end
|
20
23
|
end
|
21
24
|
|
22
25
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: difference
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 1
|
10
|
+
version: 1.0.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Manish Puri
|
@@ -15,8 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
19
|
-
default_executable:
|
18
|
+
date: 2013-07-07 00:00:00 Z
|
20
19
|
dependencies: []
|
21
20
|
|
22
21
|
description: This is a simple gem to compare 2 objects of the same class and return a hash of differences
|
@@ -31,11 +30,11 @@ extra_rdoc_files: []
|
|
31
30
|
files:
|
32
31
|
- .gitignore
|
33
32
|
- Gemfile
|
33
|
+
- README.rdoc
|
34
34
|
- Rakefile
|
35
35
|
- difference.gemspec
|
36
36
|
- lib/difference.rb
|
37
37
|
- lib/difference/version.rb
|
38
|
-
has_rdoc: true
|
39
38
|
homepage: ""
|
40
39
|
licenses: []
|
41
40
|
|
@@ -65,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
64
|
requirements: []
|
66
65
|
|
67
66
|
rubyforge_project: difference
|
68
|
-
rubygems_version: 1.
|
67
|
+
rubygems_version: 1.8.25
|
69
68
|
signing_key:
|
70
69
|
specification_version: 3
|
71
70
|
summary: difference between 2 objects
|