hash_subtraction 1.0.0
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.
- data/lib/hash_subtraction.rb +76 -0
- metadata +79 -0
@@ -0,0 +1,76 @@
|
|
1
|
+
module HashSubtraction
|
2
|
+
#extending with HashSubtraction allows for removal of duplicate keys by subtracting one hash from another. This is used to keep us from re-translating already translated text.
|
3
|
+
def - (h)
|
4
|
+
h.extend(HashSubtraction)
|
5
|
+
self.deep_subtraction(h).extend(HashSubtraction).compact
|
6
|
+
end
|
7
|
+
|
8
|
+
def remove_duplicates(h)
|
9
|
+
h.extend(HashSubtraction)
|
10
|
+
self.deep_remove_duplicates(h).extend(HashSubtraction).compact
|
11
|
+
end
|
12
|
+
|
13
|
+
def remove_duplicate_keys(h)
|
14
|
+
h.extend(HashSubtraction)
|
15
|
+
self.deep_remove_duplicate_keys(h).extend(HashSubtraction).compact
|
16
|
+
end
|
17
|
+
|
18
|
+
def diff(h)
|
19
|
+
h.extend(HashSubtraction)
|
20
|
+
self.deep_diff(h).extend(HashSubtraction).compact
|
21
|
+
end
|
22
|
+
|
23
|
+
def compact(opts={})
|
24
|
+
inject({}) do |new_hash, (k,v)|
|
25
|
+
if !v.nil?
|
26
|
+
new_hash[k] = opts[:recurse] && v.class == Hash ? v.compact(opts) : v
|
27
|
+
end
|
28
|
+
new_hash
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def deep_diff(h)
|
33
|
+
self.merge(h) do |k, old, new|
|
34
|
+
old.extend(HashSubtraction) if old.is_a?(Hash)
|
35
|
+
case old
|
36
|
+
when Hash then (new.class == Hash) ? (old.diff(new)) : old
|
37
|
+
else (old != new) ? new : nil
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def deep_subtraction(h)
|
43
|
+
self.merge(h) do |k, old, new|
|
44
|
+
old.extend(HashSubtraction) if old.is_a?(Hash)
|
45
|
+
|
46
|
+
case old
|
47
|
+
when Array then (new.class == Array) ? (old - new) : (old - [new])
|
48
|
+
when Hash then (new.class == Hash) ? (old - new) : old
|
49
|
+
when Fixnum, Float, Integer then (old - new)
|
50
|
+
when String
|
51
|
+
old = (new.class == String) ? old.gsub(new,'') : old
|
52
|
+
old = nil if old.empty?
|
53
|
+
old
|
54
|
+
else (old == new) ? nil : old
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def deep_remove_duplicates(h)
|
60
|
+
self.merge(h) do |k, old, new|
|
61
|
+
old.extend(HashSubtraction) if old.is_a?(Hash)
|
62
|
+
case old
|
63
|
+
when Hash then (new.class == Hash) ? (old.remove_duplicates(new)) : old
|
64
|
+
else (old == new) ? nil : old
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def deep_remove_duplicate_keys(h)
|
70
|
+
self.merge(h) do |k, old, new|
|
71
|
+
nil
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash_subtraction
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kelly Mahan
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-10-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: hash_subtraction is a gem that provides methods for different subtractive
|
47
|
+
methods for hashes
|
48
|
+
email: kmahan@kmahan.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- lib/hash_subtraction.rb
|
54
|
+
homepage: https://github.com/KellyMahan/hash_subtraction
|
55
|
+
licenses: []
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 1.8.24
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: hash_subtraction is a gem that provides methods for different subtractive
|
78
|
+
methods for hashes
|
79
|
+
test_files: []
|