ds_hash 1.1.2 → 1.2.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 +8 -8
- data/README.rdoc +29 -0
- data/ds_hash.gemspec +1 -1
- data/lib/ds_hash/hash_core_ext.rb +15 -1
- data/lib/ds_hash/version.rb +1 -1
- data/spec/ds_hash_spec.rb +20 -5
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
Zjk1ZmY3OWYxNjJiZGMxY2NiZjFhZDljNTk0OWM0N2E2NjUzNGFmMg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NWIzZjcwZTJiOWI2ZjcxYWE4ZGY5MWU3OTdiMmM0NTkyMDdiNmM1OQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NzdlODU1NGY4MjE1YTBiODI2ZmIwNDNlYzFjNjY1YTZiYjUzNTQyMGNkZGJk
|
10
|
+
ZDIwNjZiNmFmNGU5ZTJiNGExZmMyN2U3OWU5ZWMwNWJjMGFkMDMwMjJmYjlk
|
11
|
+
NDNlODYyYTFhYTYzZGM3NjIzZDI2ZmMzOWIwNGNhY2U3MGVlZGY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NmZhZDA3OWNkZTU2MTA2MmZjNWJmNzk3MTNjMTYyOWJmOWU3MzE4NTlkNjVh
|
14
|
+
MGE3YzIyNGY2MjVmZThlYWYwYWNmNzdjZjU0NjM3NjczN2QwN2NkZDExOWMw
|
15
|
+
NDY4OGVjY2RkMGZmYzBlNGY2NWZlYjYzM2UzODlmZGE0YWQzNTE=
|
data/README.rdoc
CHANGED
@@ -17,6 +17,27 @@ _Hash_.+compact!+
|
|
17
17
|
|
18
18
|
vide: Hash.compact
|
19
19
|
|
20
|
+
_Hash_.+clean+
|
21
|
+
|
22
|
+
idem Hash.compact
|
23
|
+
|
24
|
+
_Hash_.+clean!+
|
25
|
+
|
26
|
+
idem Hash.compact!
|
27
|
+
|
28
|
+
_Hash_.+deep_key?+
|
29
|
+
|
30
|
+
hash = { :a => { :b => 'b' } }
|
31
|
+
hash.deep_key? :a, :b # return true
|
32
|
+
hash.deep_key? :a, :x # return false
|
33
|
+
|
34
|
+
_Hash_.+deep_fetch+
|
35
|
+
|
36
|
+
hash = { :a => { :b => 'hello' } }
|
37
|
+
hash.deep_key? :a, :b # return 'hello'
|
38
|
+
hash.deep_key? :a, :x # return nil
|
39
|
+
|
40
|
+
|
20
41
|
_Hash_.+deep_dup+
|
21
42
|
|
22
43
|
hash = { :a => { :b => 'b' } }
|
@@ -26,6 +47,14 @@ _Hash_.+deep_dup+
|
|
26
47
|
hash[:a][:c] #=> nil
|
27
48
|
dup[:a][:c] #=> "c"
|
28
49
|
|
50
|
+
_Hash_.+to_struct+
|
51
|
+
|
52
|
+
hash = { :a => { :b => 'hello' } }
|
53
|
+
struct = hash.to_struct # Returns a nested struct
|
54
|
+
puts struct.a # { :b => 'hello' }
|
55
|
+
puts struct.a.b # 'hello'
|
56
|
+
|
57
|
+
|
29
58
|
== Contributing to ds_hash
|
30
59
|
|
31
60
|
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
data/ds_hash.gemspec
CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
|
|
11
11
|
|
12
12
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
13
13
|
s.authors = ["nardele salomon"]
|
14
|
-
s.date = "2015-04-
|
14
|
+
s.date = "2015-04-15"
|
15
15
|
s.description = "Hash class extensions"
|
16
16
|
s.email = "del.soft.99@gmail.com"
|
17
17
|
s.extra_rdoc_files = [
|
@@ -1,4 +1,3 @@
|
|
1
|
-
|
2
1
|
class Hash
|
3
2
|
|
4
3
|
##
|
@@ -71,4 +70,19 @@ class Hash
|
|
71
70
|
end
|
72
71
|
end
|
73
72
|
|
73
|
+
unless self.method_defined? 'to_struct'
|
74
|
+
def to_struct
|
75
|
+
hash = self
|
76
|
+
klass = Struct.new(*hash.keys.map(&:to_sym) )
|
77
|
+
struct = klass.new(*hash.values)
|
78
|
+
hash.each do |key, value|
|
79
|
+
if value.is_a?(Hash)
|
80
|
+
v = value.to_struct
|
81
|
+
struct[key] = v
|
82
|
+
end
|
83
|
+
end
|
84
|
+
return struct
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
74
88
|
end
|
data/lib/ds_hash/version.rb
CHANGED
data/spec/ds_hash_spec.rb
CHANGED
@@ -6,15 +6,11 @@ let(:hash_clean_values){
|
|
6
6
|
{ a: { b: 1, c: { d: 2 } }, g: 3 , j: [1,2]}
|
7
7
|
}
|
8
8
|
|
9
|
-
let(:hash_with_false_values){
|
10
|
-
{ a: { b: false}}
|
11
|
-
}
|
12
9
|
|
13
10
|
let(:hash_with_empty_values) {
|
14
11
|
{ a: { b: 1, c: { d: 2, e: nil}, f: nil }, g: 3, h: nil, j: [nil, 1, 2] }
|
15
12
|
}
|
16
13
|
|
17
|
-
let(:hash_deep_dup) { hash_clean_values.deep_dup }
|
18
14
|
|
19
15
|
it "#compact" do
|
20
16
|
hash_with_empty_values.compact.should eq hash_clean_values
|
@@ -33,6 +29,7 @@ it "#clean!" do
|
|
33
29
|
end
|
34
30
|
|
35
31
|
context "#deep_dup" do
|
32
|
+
let(:hash_deep_dup) { hash_clean_values.deep_dup }
|
36
33
|
it { hash_deep_dup.should eq hash_clean_values }
|
37
34
|
it { hash_deep_dup.object_id.should_not eq hash_clean_values.object_id }
|
38
35
|
end
|
@@ -40,9 +37,15 @@ end
|
|
40
37
|
context "#deep_key?" do
|
41
38
|
it { hash_clean_values.deep_key?(:a, :c, :d).should be_true }
|
42
39
|
it { hash_clean_values.deep_key?(:d).should be_false }
|
40
|
+
pending "deep key with hash with indifferent access"
|
43
41
|
end
|
44
42
|
|
45
43
|
context "#deep_fetch" do
|
44
|
+
|
45
|
+
let(:hash_with_false_values){
|
46
|
+
{ a: { b: false}}
|
47
|
+
}
|
48
|
+
|
46
49
|
it { hash_clean_values.deep_fetch(:a, :c, :d).should eq 2 }
|
47
50
|
it { hash_clean_values.deep_fetch(:a, :b).should eq 1 }
|
48
51
|
it { hash_clean_values.deep_fetch(:g).should eq 3 }
|
@@ -53,11 +56,23 @@ context "#deep_fetch" do
|
|
53
56
|
it { hash_clean_values.deep_fetch(:d, 'a').should eq 'a' }
|
54
57
|
it { hash_clean_values.deep_fetch(:d, :a, :j, 'b').should eq 'b' }
|
55
58
|
|
56
|
-
|
57
59
|
it { hash_with_false_values.deep_fetch(:a, :b).should === false }
|
58
60
|
it { hash_with_false_values.deep_fetch(:a, :b, nil).should === false }
|
59
61
|
|
60
62
|
|
61
63
|
end
|
62
64
|
|
65
|
+
context "#to_struct" do
|
66
|
+
let(:struct_from_hash) {
|
67
|
+
{a: {b: 1, c: {d: 2, e: nil}}, 'k' => 1, 'l' => { 'm' => 2}}.to_struct
|
68
|
+
}
|
69
|
+
|
70
|
+
it { struct_from_hash.a.b.should == 1 }
|
71
|
+
it { struct_from_hash.a.c.d.should == 2 }
|
72
|
+
it { struct_from_hash.a.c.e.should == nil }
|
73
|
+
|
74
|
+
it { struct_from_hash.k.should eq 1 }
|
75
|
+
it { struct_from_hash.l.m.should eq 2 }
|
76
|
+
end
|
77
|
+
|
63
78
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ds_hash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- nardele salomon
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -109,5 +109,5 @@ rubyforge_project:
|
|
109
109
|
rubygems_version: 2.2.1
|
110
110
|
signing_key:
|
111
111
|
specification_version: 4
|
112
|
-
summary: Hash class extensions v. 1.
|
112
|
+
summary: Hash class extensions v. 1.2.0
|
113
113
|
test_files: []
|