tonsser_hash_utils 1.0.2 → 1.0.3.pre.alpha
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -0
- data/lib/hash_with_quick_access.rb +16 -4
- data/lib/tonsser_hash_utils/version.rb +1 -1
- data/spec/lib/hash_with_quick_access_spec.rb +21 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c94a26118be02055b25fa3b377aa61e075ccc77a
|
4
|
+
data.tar.gz: 0e346aec2880d232db2cd09bb2421a13e79a03e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e394b024fd7a12d3a01a8f3b655b2c11f8a60930964e406559a039cc7d16d25db611ead524fa8b96ea38b4fbbf995be43a3aeff64c604fe4996598644ff9fe21
|
7
|
+
data.tar.gz: 49ced59c1509fe5d7c5a72102d116ee450f593d0af2c60a86bb1f4b0ccf243ea9bbe731062df9014d6bf54fe2bcb22c5ecd68c62c4d89fecaea7dc8ad142275f
|
data/README.md
CHANGED
@@ -3,9 +3,11 @@ class HashWithQuickAccess
|
|
3
3
|
@hash = hash
|
4
4
|
end
|
5
5
|
|
6
|
-
def method_missing(key)
|
6
|
+
def method_missing(key, *args, &block)
|
7
7
|
if key?(key)
|
8
8
|
fetch_possibly_decorated_value(key)
|
9
|
+
elsif hash.respond_to?(key)
|
10
|
+
delegate_and_decorate(key, *args, &block)
|
9
11
|
else
|
10
12
|
fail KeyError, "key :#{key} was not found"
|
11
13
|
end
|
@@ -16,7 +18,7 @@ class HashWithQuickAccess
|
|
16
18
|
end
|
17
19
|
|
18
20
|
def keys
|
19
|
-
|
21
|
+
hash.keys.map(&:to_sym)
|
20
22
|
end
|
21
23
|
|
22
24
|
def [](key)
|
@@ -25,9 +27,19 @@ class HashWithQuickAccess
|
|
25
27
|
|
26
28
|
private
|
27
29
|
|
30
|
+
attr_reader :hash
|
31
|
+
|
32
|
+
def delegate_and_decorate(method_name, *args, &block)
|
33
|
+
obj = hash.send(method_name, *args, &block)
|
34
|
+
possibly_decorate(obj)
|
35
|
+
end
|
36
|
+
|
28
37
|
def fetch_possibly_decorated_value(key)
|
29
|
-
obj =
|
38
|
+
obj = hash.fetch(key) { hash.fetch(key.to_s) }
|
39
|
+
possibly_decorate(obj)
|
40
|
+
end
|
30
41
|
|
42
|
+
def possibly_decorate(obj)
|
31
43
|
if should_be_decorated(obj)
|
32
44
|
decorate(obj)
|
33
45
|
else
|
@@ -52,7 +64,7 @@ class HashWithQuickAccess
|
|
52
64
|
end
|
53
65
|
|
54
66
|
def all_keys
|
55
|
-
|
67
|
+
hash.keys.flat_map do |key|
|
56
68
|
[key, key.to_sym]
|
57
69
|
end
|
58
70
|
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require "hash_with_quick_access"
|
2
|
+
require "pp"
|
2
3
|
|
3
4
|
describe HashWithQuickAccess do
|
4
5
|
it "gives you quick access to hashes or arrays nested in other hashes" do
|
@@ -55,4 +56,24 @@ describe HashWithQuickAccess do
|
|
55
56
|
|
56
57
|
expect(hash[:job][:title]).to eq "Programmer"
|
57
58
|
end
|
59
|
+
|
60
|
+
it "delegate methods to the hash" do
|
61
|
+
hash = HashWithQuickAccess.new(a: 1, b: 2)
|
62
|
+
|
63
|
+
expect(hash.map(&:last)).to eq [1, 2]
|
64
|
+
end
|
65
|
+
|
66
|
+
it "doesn't delegate if a key exists with the name" do
|
67
|
+
hash = HashWithQuickAccess.new(values: 1)
|
68
|
+
|
69
|
+
expect(hash.values).to eq 1
|
70
|
+
end
|
71
|
+
|
72
|
+
it "wraps delegated methods in HashWithQuickAccess if they're hashes" do
|
73
|
+
hash = HashWithQuickAccess.new(a: 1, b: 2)
|
74
|
+
|
75
|
+
derived_hash = hash.reduce(new: 1337) { |acc, (_, _)| acc }
|
76
|
+
|
77
|
+
expect(derived_hash.new).to eq 1337
|
78
|
+
end
|
58
79
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tonsser_hash_utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3.pre.alpha
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Pedersen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-07-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -107,9 +107,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
107
107
|
version: '0'
|
108
108
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
109
|
requirements:
|
110
|
-
- - "
|
110
|
+
- - ">"
|
111
111
|
- !ruby/object:Gem::Version
|
112
|
-
version:
|
112
|
+
version: 1.3.1
|
113
113
|
requirements: []
|
114
114
|
rubyforge_project:
|
115
115
|
rubygems_version: 2.4.5
|