easy_diff 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.3.0"
10
+ gem "yard", "~> 0.6.0"
11
+ gem "bundler", "~> 1.0.0"
12
+ gem "jeweler", "~> 1.5.2"
13
+ gem "rcov", ">= 0"
14
+ end
@@ -0,0 +1,30 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ git (1.2.5)
6
+ jeweler (1.5.2)
7
+ bundler (~> 1.0.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.8.7)
11
+ rcov (0.9.9)
12
+ rspec (2.3.0)
13
+ rspec-core (~> 2.3.0)
14
+ rspec-expectations (~> 2.3.0)
15
+ rspec-mocks (~> 2.3.0)
16
+ rspec-core (2.3.1)
17
+ rspec-expectations (2.3.0)
18
+ diff-lcs (~> 1.1.2)
19
+ rspec-mocks (2.3.0)
20
+ yard (0.6.4)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ bundler (~> 1.0.0)
27
+ jeweler (~> 1.5.2)
28
+ rcov
29
+ rspec (~> 2.3.0)
30
+ yard (~> 0.6.0)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Abner Qian
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,136 @@
1
+ = easy_diff
2
+
3
+ Easy Diff enhances the functionality of Hash, allowing recursive diff, merge, and unmerge of arbitrarily constructed hashes.
4
+ This is perfect for people who need to do diffs on not only plain text files but also data as Hash or JSON objects. Unmerge
5
+ is included with diff and merge to more easily allow versioning of arbitrary data.
6
+
7
+ == Install
8
+
9
+ gem install easy_diff
10
+
11
+ == Examples
12
+
13
+ === Hash#easy_diff
14
+
15
+ Takes another hash and recursively determines the differences between the two. This method returns two hashes.
16
+ The first contains what has to be removed from self to create the second hash. The second contains what has to be added.
17
+
18
+ original = {
19
+ :tags => ['a', 'b', 'c'],
20
+ :pos => {:x => '1', :y => '2'},
21
+ :some_str => "bla",
22
+ :some_int => 1,
23
+ :some_bool => false,
24
+ :extra_removed => "bye"
25
+ }
26
+
27
+ modified = {
28
+ :tags => ['b', 'c', 'd'],
29
+ :pos => {:x => '3', :y => '2'},
30
+ :some_str => "bla",
31
+ :some_int => 2,
32
+ :some_bool => true,
33
+ :extra_added => "hi"
34
+ }
35
+
36
+ removed, added = original.easy_diff modified
37
+
38
+ # The removed and added hashes should contain the following:
39
+ # removed = {
40
+ # :tags => ['a'],
41
+ # :pos => {:x => '1'},
42
+ # :some_int => 1,
43
+ # :some_bool => false,
44
+ # :extra_removed => "bye"
45
+ # }
46
+ #
47
+ # added = {
48
+ # :tags => ['d'],
49
+ # :pos => {:x => '3'},
50
+ # :some_int => 2,
51
+ # :some_bool => true,
52
+ # :extra_added => "hi"
53
+ # }
54
+
55
+ === Hash#easy_merge
56
+
57
+ Takes a hash and recursively merges it with self. Arrays are merged by the union operation and then sorted.
58
+ Using Hash#easy_merge! will modify self instead of returning a new hash.
59
+
60
+ original = {
61
+ :tags => ['a', 'b', 'c'],
62
+ :pos => {:x => '1', :y => '2'},
63
+ :some_str => "bla",
64
+ :some_int => 1,
65
+ :some_bool => false,
66
+ :extra_removed => "bye"
67
+ }
68
+
69
+ extra = {
70
+ :tags => ['d'],
71
+ :pos => {:x => '3'},
72
+ :some_int => 2,
73
+ :some_bool => true,
74
+ :extra_added => "hi"
75
+ }
76
+
77
+ merged = original.easy_merge extra
78
+
79
+ # The merged hash should look like this:
80
+ # merged = {
81
+ # :tags => ['a', 'b', 'c', 'd'],
82
+ # :pos => {:x => '3', :y => '2'},
83
+ # :some_str => "bla",
84
+ # :some_int => 2,
85
+ # :some_bool => true,
86
+ # :extra_removed => "bye",
87
+ # :extra_added => "hi"
88
+ # }
89
+
90
+ === Hash#easy_unmerge
91
+
92
+ Takes a hash and recursively unmerges it with self. By unmerging, I mean it will remove all matching values from
93
+ the hash. All matching elements will be removed in arrays as well and then the arrays will be sorted.
94
+ Using Hash#easy_unmerge! will modify self instead of returning a new hash.
95
+
96
+ original = {
97
+ :tags => ['b', 'c', 'd'],
98
+ :pos => {:x => '3', :y => '2'},
99
+ :some_str => "bla",
100
+ :some_int => 2,
101
+ :some_bool => true,
102
+ :extra_added => "hi"
103
+ }
104
+
105
+ extra = {
106
+ :tags => ['d'],
107
+ :pos => {:x => '3'},
108
+ :some_int => 2,
109
+ :some_bool => true,
110
+ :extra_added => "hi"
111
+ }
112
+
113
+ unmerged = original.easy_unmerge extra
114
+
115
+ # The unmerged hash should look like this:
116
+ # unmerged = {
117
+ # :tags => ['b', 'c'],
118
+ # :pos => {:y => '2'},
119
+ # :some_str => "bla"
120
+ # }
121
+
122
+ == Contributing to easy_diff
123
+
124
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
125
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
126
+ * Fork the project
127
+ * Start a feature/bugfix branch
128
+ * Commit and push until you are happy with your contribution
129
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
130
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
131
+
132
+ == Copyright
133
+
134
+ Copyright (c) 2011 Abner Qian. See LICENSE.txt for
135
+ further details.
136
+
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "easy_diff"
16
+ gem.homepage = "http://github.com/Blargel/easy_diff"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Recursive diff, merge, and unmerge for hashes and arrays.}
19
+ gem.description = %Q{Easy Diff enhances the functionality of Hash, allowing recursive diff, merge, and unmerge of arbitrarily constructed hashes.
20
+ This is perfect for people who need to do diffs on not only plain text files but also data as Hash or JSON objects. Unmerge
21
+ is included with diff and merge to more easily allow versioning of arbitrary data.}
22
+ gem.email = "LargeBagel@gmail.com"
23
+ gem.authors = ["Abner Qian"]
24
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
25
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
26
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
27
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
28
+ end
29
+ Jeweler::RubygemsDotOrgTasks.new
30
+
31
+ require 'rspec/core'
32
+ require 'rspec/core/rake_task'
33
+ RSpec::Core::RakeTask.new(:spec) do |spec|
34
+ spec.pattern = FileList['spec/**/*_spec.rb']
35
+ end
36
+
37
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
38
+ spec.pattern = 'spec/**/*_spec.rb'
39
+ spec.rcov = true
40
+ end
41
+
42
+ task :default => :spec
43
+
44
+ require 'yard'
45
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,6 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/easy_diff/safe_dup')
2
+ require File.expand_path(File.dirname(__FILE__) + '/easy_diff/core')
3
+ require File.expand_path(File.dirname(__FILE__) + '/easy_diff/hash_ext')
4
+
5
+ Object.send :include, EasyDiff::SafeDup
6
+ Hash.send :include, EasyDiff::HashExt
@@ -0,0 +1,66 @@
1
+ module EasyDiff
2
+ module Core
3
+ def self.easy_diff(original, modified)
4
+ removed = nil
5
+ added = nil
6
+
7
+ if original.nil?
8
+ added = modified.safe_dup
9
+ elsif modified.nil?
10
+ removed = original.safe_dup
11
+ elsif original.is_a?(Hash) && modified.is_a?(Hash)
12
+ removed = {}
13
+ added = {}
14
+ original_keys = original.keys
15
+ modified_keys = modified.keys
16
+ keys_in_common = original_keys & modified_keys
17
+ keys_removed = original_keys - modified_keys
18
+ keys_added = modified_keys - original_keys
19
+ keys_removed.each{ |key| removed[key] = original[key].safe_dup }
20
+ keys_added.each{ |key| added[key] = modified[key].safe_dup }
21
+ keys_in_common.each do |key|
22
+ r, a = easy_diff original[key], modified[key]
23
+ removed[key] = r unless r.nil?
24
+ added[key] = a unless a.nil?
25
+ end
26
+ elsif original.is_a?(Array) && modified.is_a?(Array)
27
+ removed = original - modified
28
+ added = modified - original
29
+ elsif original != modified
30
+ removed = original
31
+ added = modified
32
+ end
33
+ return removed, added
34
+ end
35
+
36
+ def self.easy_unmerge!(original, removed)
37
+ if original.is_a?(Hash) && removed.is_a?(Hash)
38
+ original_keys = original.keys
39
+ removed_keys = removed.keys
40
+ keys_in_common = original_keys & removed_keys
41
+ keys_in_common.each{ |key| original.delete(key) if easy_unmerge!(original[key], removed[key]).nil? }
42
+ elsif original.is_a?(Array) && removed.is_a?(Array)
43
+ original.reject!{ |e| removed.include?(e) }
44
+ original.sort!
45
+ elsif original == removed
46
+ original = nil
47
+ end
48
+ original
49
+ end
50
+
51
+ def self.easy_merge!(original, added)
52
+ if added.nil?
53
+ return original
54
+ elsif original.is_a?(Hash) && added.is_a?(Hash)
55
+ added_keys = added.keys
56
+ added_keys.each{ |key| original[key] = easy_merge!(original[key], added[key])}
57
+ elsif original.is_a?(Array) && added.is_a?(Array)
58
+ original |= added
59
+ original.sort!
60
+ else
61
+ original = added.safe_dup
62
+ end
63
+ original
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,23 @@
1
+ module EasyDiff
2
+ module HashExt
3
+ def easy_diff(other)
4
+ EasyDiff::Core.easy_diff self, other
5
+ end
6
+
7
+ def easy_merge!(other)
8
+ EasyDiff::Core.easy_merge! self, other
9
+ end
10
+
11
+ def easy_unmerge!(other)
12
+ EasyDiff::Core.easy_unmerge! self, other
13
+ end
14
+
15
+ def easy_merge(other)
16
+ self.dup.easy_merge!(other)
17
+ end
18
+
19
+ def easy_unmerge(other)
20
+ self.dup.easy_unmerge!(other)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ module EasyDiff
2
+ module SafeDup
3
+ def safe_dup
4
+ begin
5
+ self.dup
6
+ rescue TypeError
7
+ self
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,73 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe EasyDiff do
4
+ before :each do
5
+ @original = {
6
+ :tags => ['a', 'b', 'c'],
7
+ :pos => {:x => '1', :y => '2'},
8
+ :some_str => "bla",
9
+ :some_int => 1,
10
+ :some_bool => false,
11
+ :extra_removed => "bye"
12
+ }
13
+
14
+ @modified = {
15
+ :tags => ['b', 'c', 'd'],
16
+ :pos => {:x => '3', :y => '2'},
17
+ :some_str => "bla",
18
+ :some_int => 2,
19
+ :some_bool => true,
20
+ :extra_added => "hi"
21
+ }
22
+
23
+ @removed = {
24
+ :tags => ['a'],
25
+ :pos => {:x => '1'},
26
+ :some_int => 1,
27
+ :some_bool => false,
28
+ :extra_removed => "bye"
29
+ }
30
+
31
+ @added = {
32
+ :tags => ['d'],
33
+ :pos => {:x => '3'},
34
+ :some_int => 2,
35
+ :some_bool => true,
36
+ :extra_added => "hi"
37
+ }
38
+ end
39
+ it "should compute easy_diff" do
40
+ removed, added = @original.easy_diff @modified
41
+ removed.should == @removed
42
+ added.should == @added
43
+ end
44
+
45
+ it "should compute easy_unmerge" do
46
+ unmerged = @modified.easy_unmerge @added
47
+ unmerged.should == {
48
+ :tags => ['b', 'c'],
49
+ :pos => {:y => '2'},
50
+ :some_str => "bla"
51
+ }
52
+ end
53
+
54
+ it "should compute easy_merge" do
55
+ merged = @original.easy_merge @added
56
+ merged.should == {
57
+ :tags => ['a', 'b', 'c', 'd'],
58
+ :pos => {:x => '3', :y => '2'},
59
+ :some_str => "bla",
60
+ :some_int => 2,
61
+ :some_bool => true,
62
+ :extra_removed => "bye",
63
+ :extra_added => "hi"
64
+ }
65
+ end
66
+
67
+ it "should stay the same" do
68
+ removed, added = @original.easy_diff @modified
69
+ unmerged = @modified.easy_unmerge added
70
+ original = unmerged.easy_merge removed
71
+ original.should == @original
72
+ end
73
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'easy_diff'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_diff
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Abner Qian
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-03 00:00:00 -08:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rspec
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 2.3.0
24
+ type: :development
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: yard
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: 0.6.0
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: bundler
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.0.0
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: jeweler
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ version: 1.5.2
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: rcov
62
+ requirement: &id005 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: *id005
71
+ description: |-
72
+ Easy Diff enhances the functionality of Hash, allowing recursive diff, merge, and unmerge of arbitrarily constructed hashes.
73
+ This is perfect for people who need to do diffs on not only plain text files but also data as Hash or JSON objects. Unmerge
74
+ is included with diff and merge to more easily allow versioning of arbitrary data.
75
+ email: LargeBagel@gmail.com
76
+ executables: []
77
+
78
+ extensions: []
79
+
80
+ extra_rdoc_files:
81
+ - LICENSE.txt
82
+ - README.rdoc
83
+ files:
84
+ - .document
85
+ - .rspec
86
+ - Gemfile
87
+ - Gemfile.lock
88
+ - LICENSE.txt
89
+ - README.rdoc
90
+ - Rakefile
91
+ - VERSION
92
+ - lib/easy_diff.rb
93
+ - lib/easy_diff/core.rb
94
+ - lib/easy_diff/hash_ext.rb
95
+ - lib/easy_diff/safe_dup.rb
96
+ - spec/easy_diff_spec.rb
97
+ - spec/spec_helper.rb
98
+ has_rdoc: true
99
+ homepage: http://github.com/Blargel/easy_diff
100
+ licenses:
101
+ - MIT
102
+ post_install_message:
103
+ rdoc_options: []
104
+
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ hash: 744956757776922527
113
+ segments:
114
+ - 0
115
+ version: "0"
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: "0"
122
+ requirements: []
123
+
124
+ rubyforge_project:
125
+ rubygems_version: 1.5.2
126
+ signing_key:
127
+ specification_version: 3
128
+ summary: Recursive diff, merge, and unmerge for hashes and arrays.
129
+ test_files:
130
+ - spec/easy_diff_spec.rb
131
+ - spec/spec_helper.rb