patch_utils 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/patch_utils/hash_diff.rb +55 -0
  3. metadata +10 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 10a5d7b6c2fa8fc358ac8b47a1ba8a8e4df4ecee
4
- data.tar.gz: dc15778636814c7932b2310f0e11013e3ccec958
3
+ metadata.gz: 5537fe95975adea086f8d941bc7c4dd50d4f58e7
4
+ data.tar.gz: 8653e5a852dc0129973d359bf019e57eca548a62
5
5
  SHA512:
6
- metadata.gz: 1877b89fe38a9b45a948a5026baf3b6a5e88a9a0e6a52ed26537c052d01c128e84f49c35a6f9cbfddb1ae330d7ac6be13e81515cd42b2e27f383d8bd81b16db6
7
- data.tar.gz: e629ef33dfff7f4e73e96394134acd2fc2af8d62f9c5bd8ef4c42d49f95f0ff2cb4c5e2fba464ca97334e73a4e2cc090097aeebca12606391c85cd0e15351c90
6
+ metadata.gz: 99bcbdfc12cb679e72688a5e8cc8e3894e09e0723a37c57ce5b998fae96c8749e02277e2a1e44a7164875e41a59fd28834ba87e3e13cf16db5cb71b06347b310
7
+ data.tar.gz: cce78a2258e92fbd841a6bfe0735b194f97a8782c5ede8784a86544d14a481519bdec5376ed6525c60b41a2d8582a5c72e1c917f973162bcb51d2d12c21af65d
@@ -0,0 +1,55 @@
1
+ # encoding:utf-8
2
+ #
3
+ # Get diff from two hashes. (or from other data structure)
4
+ # return format: {:from => , :to => }
5
+ #
6
+ # HashDiff.get_diff 1, 1 # => nil
7
+ # HashDiff.get_diff 1, 2 # => {:from=>1, :to=>2}
8
+ # HashDiff.get_diff({:a => 1, :b => 2, :c => '4'}, {:a => 4, :b => true, :c => 5}) do |x, y| x.is_a?(Fixnum) || y.is_a?(TrueClass) end # => {:from=>{:c=>"4"}, :to=>{:c=>5}}
9
+ # HashDiff.get_diff [1,2,3],[1,4,5] # => {:from=>[2,3], :to=>[4,5]}
10
+ # a = {:a_num=>1, :same_num=>0, :an_other_num=>3, :create=>{:user=>["new", "save"]}, :an_array=>[1, 2, 3], :complex_json=>{:a=>{:b=>{:c=>"d", :f=>"x"}}}}
11
+ # b = {:a_num=>2, :same_num=>0, :update=>{:user=>["edit", "update"]}, :an_array=>[1, 2, 4], :an_other_array=>["x"], :complex_json=>{:a=>{:b=>{:c=>"d", :f=>"g"}, :h=>"i"}}}
12
+ # HashDiff.get_diff a, b # =>
13
+ # {:from=>{:a_num=>1, :an_other_num=>3, :create=>{:user=>["new", "save"]}, :an_array=>[3], :complex_json=>{:a=>{:b=>{:f=>"x"}}}}, :to=>{:a_num=>2, :update=>{:user=>["edit", "update"]}, :an_array=>[4], :an_other_array=>["x"], :complex_json=>{:a=>{:b=>{:f=>"g"}, :h=>"i"}}}}
14
+
15
+ class HashDiff
16
+ def self.get_diff h1, h2, &blk
17
+ if block_given?
18
+ return nil if yield(h1, h2)
19
+ else
20
+ return nil if h1 == h2
21
+ end
22
+
23
+ if h1.is_a?(Array) && h2.is_a?(Array)
24
+ return {:from => (h1 - h2), :to => (h2 - h1)}
25
+ end
26
+
27
+ if h1.is_a?(Hash) && h2.is_a?(Hash)
28
+ all_keys = (h1.keys | h2.keys)
29
+ from = h1.clone
30
+ to = h2.clone
31
+ all_keys.each do |k|
32
+ if h1.keys.include?(k) && h2.keys.include?(k)
33
+ if block_given?
34
+ if yield(h1[k], h2[k])
35
+ from.delete(k)
36
+ to.delete(k)
37
+ end
38
+ else
39
+ if h1[k] == h2[k]
40
+ from.delete(k)
41
+ to.delete(k)
42
+ end
43
+ end
44
+ _diff = self.get_diff(h1[k], h2[k], &blk)
45
+ if _diff.present?
46
+ from[k] = _diff[:from]
47
+ to[k] = _diff[:to]
48
+ end
49
+ end
50
+ end
51
+ return {:from => from, :to => to}
52
+ end
53
+ {:from => h1, :to => h2}
54
+ end
55
+ end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: patch_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - zzqwdss
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-10 00:00:00.000000000 Z
11
+ date: 2014-12-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: some useful utils and monkey patches
14
- email: zzqwdss@163.com
14
+ email: zzqwdss@gmail.com
15
15
  executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
@@ -19,10 +19,12 @@ files:
19
19
  - lib/patch_utils.rb
20
20
  - lib/patch_utils/active_support_encoding.rb
21
21
  - lib/patch_utils/hash.rb
22
+ - lib/patch_utils/hash_diff.rb
22
23
  - lib/patch_utils/string.rb
23
24
  - lib/patch_utils/time_with_zone.rb
24
25
  homepage: http://rubygems.org/gems/patch_utils
25
- licenses: []
26
+ licenses:
27
+ - MIT
26
28
  metadata: {}
27
29
  post_install_message:
28
30
  rdoc_options: []
@@ -30,18 +32,19 @@ require_paths:
30
32
  - lib
31
33
  required_ruby_version: !ruby/object:Gem::Requirement
32
34
  requirements:
33
- - - ! '>='
35
+ - - '>='
34
36
  - !ruby/object:Gem::Version
35
37
  version: '0'
36
38
  required_rubygems_version: !ruby/object:Gem::Requirement
37
39
  requirements:
38
- - - ! '>='
40
+ - - '>='
39
41
  - !ruby/object:Gem::Version
40
42
  version: '0'
41
43
  requirements: []
42
44
  rubyforge_project:
43
- rubygems_version: 2.2.2
45
+ rubygems_version: 2.1.11
44
46
  signing_key:
45
47
  specification_version: 4
46
48
  summary: patch_utils by zzqwdss
47
49
  test_files: []
50
+ has_rdoc: