hash-selector 1.0.0 → 1.1.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 +4 -4
- data/README.md +1 -1
- data/lib/hash_selector.rb +14 -6
- data/lib/hash_selector/version.rb +1 -1
- data/spec/hash_selector_spec.rb +7 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd79cb3f4b022a4dc715e84cc8b0aefa3c3c0f72
|
4
|
+
data.tar.gz: e22414a6f58dc2823dd80cf508ae964db361eca9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6d13fc70a478ef619be306ac379905f998ac6115d4d5c344d046cf302e6624ae4c3be3776b2eee2c5bd5e072b34bc5e12825e6b78ffaa66bffc5a96eab3df47a
|
7
|
+
data.tar.gz: 559d3a07bf68c80d3701c6a8bba2a53fc27937b23d26918cdad051d29e56937d4c59a97764a98f8191603d4ce4fc67242ffbd2f757e5ac70d739945030a316df
|
data/README.md
CHANGED
@@ -62,7 +62,7 @@ Or install it yourself as:
|
|
62
62
|
|
63
63
|
## Contributing
|
64
64
|
|
65
|
-
1. Fork it ( https://github.com/
|
65
|
+
1. Fork it ( https://github.com/pezra/hash-selector/fork )
|
66
66
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
67
67
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
68
68
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/lib/hash_selector.rb
CHANGED
@@ -21,7 +21,8 @@ class HashSelector
|
|
21
21
|
rescue IndexError
|
22
22
|
raise KeyError
|
23
23
|
end
|
24
|
-
} ]
|
24
|
+
} ],
|
25
|
+
desc + "[#{key.inspect}]"
|
25
26
|
end
|
26
27
|
|
27
28
|
# Returns a new selector that selects an entry from the result of the current
|
@@ -34,29 +35,36 @@ class HashSelector
|
|
34
35
|
else
|
35
36
|
needle
|
36
37
|
end
|
37
|
-
} ]
|
38
|
+
} ],
|
39
|
+
desc + ".find{...}"
|
38
40
|
end
|
39
41
|
alias_method :detect, :find
|
40
42
|
|
41
43
|
# Returns the value from a hash at the location specified by this selector.
|
42
44
|
def find_in(hay_stack)
|
43
|
-
|
45
|
+
idx = 0
|
46
|
+
steps.reduce(hay_stack) { |search_area, step| idx +=1; step.call(search_area) }
|
44
47
|
|
45
48
|
rescue KeyError
|
46
49
|
if block_given?
|
47
50
|
yield hay_stack
|
48
51
|
else
|
49
|
-
raise
|
52
|
+
raise KeyError, "step #{idx} of #{self} failed to match"
|
50
53
|
end
|
51
54
|
end
|
52
55
|
|
56
|
+
def to_s
|
57
|
+
desc
|
58
|
+
end
|
59
|
+
|
53
60
|
protected
|
54
61
|
|
55
62
|
MISSING = Object.new
|
56
63
|
|
57
|
-
def initialize(steps = [])
|
64
|
+
def initialize(steps = [], desc="HashSelector.new")
|
58
65
|
@steps = steps
|
66
|
+
@desc = desc
|
59
67
|
end
|
60
68
|
|
61
|
-
attr_reader :steps
|
69
|
+
attr_reader :steps, :desc
|
62
70
|
end
|
data/spec/hash_selector_spec.rb
CHANGED
@@ -9,6 +9,11 @@ RSpec.describe "HashSelector" do
|
|
9
9
|
] },
|
10
10
|
qux: 42 } }
|
11
11
|
|
12
|
+
it "has a useful to_s" do
|
13
|
+
expect( HashSelector.new[:foo].find{true}[:bar].to_s )
|
14
|
+
.to eq "HashSelector.new[:foo].find{...}[:bar]"
|
15
|
+
end
|
16
|
+
|
12
17
|
it "can select values from top level" do
|
13
18
|
expect(
|
14
19
|
HashSelector.new[:qux]
|
@@ -35,7 +40,7 @@ RSpec.describe "HashSelector" do
|
|
35
40
|
|
36
41
|
it "raise error on a array index miss" do
|
37
42
|
expect { HashSelector.new[:foo][:bar][42].find_in(data)
|
38
|
-
}.to raise_error KeyError
|
43
|
+
}.to raise_error KeyError, %r/\[:foo\]\[:bar\]\[42\]/
|
39
44
|
end
|
40
45
|
|
41
46
|
it "returns value of default value block a array index miss" do
|
@@ -61,7 +66,7 @@ RSpec.describe "HashSelector" do
|
|
61
66
|
expect {
|
62
67
|
HashSelector.new[:foo][:bar].detect{|it| it[:name] == "mallory"}
|
63
68
|
.find_in(data)
|
64
|
-
}.to raise_error KeyError
|
69
|
+
}.to raise_error KeyError, %r/\[:foo\]\[:bar\].find{...}/
|
65
70
|
end
|
66
71
|
|
67
72
|
it "returns value of default value block on a #find/#detect miss" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash-selector
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Williams
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|