ds_hash 0.0.2 → 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.
- checksums.yaml +9 -9
- data/ds_hash.gemspec +1 -0
- data/lib/ds_hash/hash_core_ext.rb +51 -12
- data/lib/ds_hash/version.rb +3 -0
- data/spec/ds_hash_spec.rb +27 -6
- metadata +4 -3
checksums.yaml
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
---
|
|
2
2
|
!binary "U0hBMQ==":
|
|
3
3
|
metadata.gz: !binary |-
|
|
4
|
-
|
|
4
|
+
ODI2MDk2YWY5ZDI2NzdlZGQxM2IxNDA2NTk4ZWE2NDk2MjY2YWMyZg==
|
|
5
5
|
data.tar.gz: !binary |-
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
YmJmYzBiN2E3ZmFkMGVmZGEzZTIyOTIyZmM1NTVmNDVlNmU5NGU2OQ==
|
|
7
|
+
SHA512:
|
|
8
8
|
metadata.gz: !binary |-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
MmUyOWI4Nzk5MWNlMmExNDI0NDRlZTVkZGEwYjc5MjE1OTYzOWU5Y2NjOTM2
|
|
10
|
+
Mjc0MzMxNWRmYTQ3ZDgxN2Y4Y2FhYWI5NGRhZWE1Y2QyYjc3MDUzMmYzYTE3
|
|
11
|
+
ZjJlOTI2YTljN2Y4MjUyZjU2OGMyNzEwOTViYTZlMWRmZDJjYmE=
|
|
12
12
|
data.tar.gz: !binary |-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
NzljOWY5OTBkNGRhOTEzNGU1NmE4N2QxZWExODU3YzMyOGRkMmU3NjAzMzk4
|
|
14
|
+
NGZhMWY2ZGNhMGFmMDRiZGNjYmI5NTZjMWEwOTE3NzUyYWFlZWI5OGNhZjI4
|
|
15
|
+
ODg2NzA1NTIyNjhlMDNmMjQwYTM1NjNkZTdkZjE1YjQzNDhkNzc=
|
data/ds_hash.gemspec
CHANGED
|
@@ -5,6 +5,7 @@ class Hash
|
|
|
5
5
|
# :method: deep_dup
|
|
6
6
|
# Returns a deep copy of hash. like rails active support
|
|
7
7
|
|
|
8
|
+
|
|
8
9
|
define_method 'deep_dup' do
|
|
9
10
|
duplicate = self.dup
|
|
10
11
|
duplicate.each_pair do |k,v|
|
|
@@ -14,18 +15,56 @@ class Hash
|
|
|
14
15
|
duplicate
|
|
15
16
|
end unless self.method_defined? 'deep_dup'
|
|
16
17
|
|
|
17
|
-
##
|
|
18
|
-
# self remove all empty values and/or keys
|
|
19
|
-
def compact! compact_key=true
|
|
20
|
-
deep_swap_proc = Proc.new { |k, v| v.delete_if(&deep_swap_proc) if v.kind_of?(Hash); v.to_s.empty? || (compact_key && k.to_s.empty? ) }
|
|
21
|
-
delete_if &deep_swap_proc
|
|
22
|
-
end
|
|
23
18
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
19
|
+
unless self.method_defined? 'clean'
|
|
20
|
+
|
|
21
|
+
def clean!
|
|
22
|
+
swoop = Proc.new { |k, v|
|
|
23
|
+
if v.respond_to? 'clean!'
|
|
24
|
+
v.clean!
|
|
25
|
+
false
|
|
26
|
+
elsif v.respond_to? 'compact!'
|
|
27
|
+
v.compact!
|
|
28
|
+
false
|
|
29
|
+
else
|
|
30
|
+
v.to_s.empty?
|
|
31
|
+
end
|
|
32
|
+
}
|
|
33
|
+
delete_if &swoop
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def clean
|
|
37
|
+
hsh = self.deep_dup
|
|
38
|
+
hsh.clean!
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
alias_method :compact, :clean unless self.method_defined? 'compact'
|
|
42
|
+
alias_method :compact!, :clean! unless self.method_defined? 'compact!'
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
unless self.method_defined? 'deep_key?'
|
|
47
|
+
def deep_key? *keys
|
|
48
|
+
key = keys.shift
|
|
49
|
+
return key?(key) if keys.empty?
|
|
50
|
+
return self[key].deep_key?(*keys) if self[key].is_a? Hash
|
|
51
|
+
false
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
unless self.method_defined? 'deep_fetch'
|
|
56
|
+
def deep_fetch *keys
|
|
57
|
+
key = keys.shift
|
|
58
|
+
if keys.empty?
|
|
59
|
+
if block_given?
|
|
60
|
+
yield self[key] || {}
|
|
61
|
+
else
|
|
62
|
+
self[key]
|
|
63
|
+
end
|
|
64
|
+
elsif self[key].is_a? Hash
|
|
65
|
+
self[key].deep_fetch(*keys)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
30
69
|
|
|
31
70
|
end
|
data/spec/ds_hash_spec.rb
CHANGED
|
@@ -3,26 +3,47 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
|
3
3
|
describe "DsHash" do
|
|
4
4
|
|
|
5
5
|
let(:hash_clean_values){
|
|
6
|
-
{ a: { b: 1, c: { d: 2 } }, g: 3 }
|
|
6
|
+
{ a: { b: 1, c: { d: 2 } }, g: 3 , j: [1,2]}
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
let(:hash_with_empty_values) {
|
|
10
|
-
{ a: { b: 1, c: { d: 2, e: nil}, f: nil }, g: 3, h: nil }
|
|
10
|
+
{ a: { b: 1, c: { d: 2, e: nil}, f: nil }, g: 3, h: nil, j: [nil, 1, 2] }
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
let(:hash_deep_dup) { hash_clean_values.deep_dup }
|
|
14
14
|
|
|
15
15
|
it "#compact" do
|
|
16
|
-
|
|
16
|
+
hash_with_empty_values.compact.should eq hash_clean_values
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
it "#compact!" do
|
|
20
|
-
|
|
20
|
+
hash_with_empty_values.compact!.should eq hash_clean_values
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "#clean" do
|
|
24
|
+
hash_with_empty_values.compact.should eq hash_clean_values
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it "#clean!" do
|
|
28
|
+
hash_with_empty_values.compact!.should eq hash_clean_values
|
|
21
29
|
end
|
|
22
30
|
|
|
23
31
|
context "#deep_dup" do
|
|
24
|
-
|
|
25
|
-
|
|
32
|
+
it { hash_deep_dup.should eq hash_clean_values }
|
|
33
|
+
it { hash_deep_dup.object_id.should_not eq hash_clean_values.object_id }
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
context "#deep_key?" do
|
|
37
|
+
it { hash_clean_values.deep_key?(:a, :c, :d).should be_true }
|
|
38
|
+
it { hash_clean_values.deep_key?(:d).should be_false }
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
context "#deep_fetch" do
|
|
42
|
+
it { hash_clean_values.deep_fetch(:a, :c, :d).should eq 2 }
|
|
43
|
+
it { hash_clean_values.deep_fetch(:a, :b).should eq 1 }
|
|
44
|
+
it { hash_clean_values.deep_fetch(:g).should eq 3 }
|
|
45
|
+
it { hash_clean_values.deep_fetch(:d).should eq nil }
|
|
46
|
+
it { hash_clean_values.deep_fetch(:d, :a, :j).should eq nil }
|
|
26
47
|
end
|
|
27
48
|
|
|
28
49
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ds_hash
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0
|
|
4
|
+
version: 1.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- nardele salomon
|
|
@@ -97,6 +97,7 @@ files:
|
|
|
97
97
|
- ds_hash.rb
|
|
98
98
|
- lib/ds_hash.rb
|
|
99
99
|
- lib/ds_hash/hash_core_ext.rb
|
|
100
|
+
- lib/ds_hash/version.rb
|
|
100
101
|
- spec/ds_hash_spec.rb
|
|
101
102
|
- spec/spec_helper.rb
|
|
102
103
|
homepage: https://github.com/delsoft/ds_hash
|
|
@@ -119,8 +120,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
119
120
|
version: '0'
|
|
120
121
|
requirements: []
|
|
121
122
|
rubyforge_project:
|
|
122
|
-
rubygems_version: 2.
|
|
123
|
+
rubygems_version: 2.2.1
|
|
123
124
|
signing_key:
|
|
124
125
|
specification_version: 4
|
|
125
|
-
summary: Hash class extensions v. 0.0
|
|
126
|
+
summary: Hash class extensions v. 1.0.0
|
|
126
127
|
test_files: []
|