fetch_in 0.1.1 → 0.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.
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/fetch_in.rb +31 -17
- data/spec/fetch_in_spec.rb +11 -0
- metadata +16 -7
data/Rakefile
CHANGED
@@ -10,7 +10,7 @@ begin
|
|
10
10
|
gem.email = "mccraigmccraig@googlemail.com"
|
11
11
|
gem.homepage = "http://github.com/trampoline/fetch_in"
|
12
12
|
gem.authors = ["mccraig mccraig of the clan mccraig"]
|
13
|
-
gem.add_development_dependency "rspec", ">= 1.2.
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.8"
|
14
14
|
gem.add_development_dependency "rr", ">= 0.10.5"
|
15
15
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
16
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/fetch_in.rb
CHANGED
@@ -1,29 +1,43 @@
|
|
1
1
|
module FetchIn
|
2
|
-
|
3
|
-
keys
|
4
|
-
|
5
|
-
|
2
|
+
class << self
|
3
|
+
def fetch_in(h, *keys)
|
4
|
+
keys.reduce(h) do |result,key|
|
5
|
+
return nil if !result
|
6
|
+
result[key]
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
# store_in stores a value in a nested associative structure
|
11
|
+
# new levels in the structure are created as required, by the supplied Proc.
|
12
|
+
# if no Proc is supplied, new levels are created with the same class as
|
13
|
+
# the previous level
|
14
|
+
def store_in(h, *keys_and_value, &proc)
|
15
|
+
proc ||= lambda{|rx_key_stack| rx_key_stack[-1][0].class.new}
|
16
|
+
keys = keys_and_value[0..-2]
|
17
|
+
value = keys_and_value[-1]
|
18
|
+
|
19
|
+
# find or create the last associative receiver in the chain to the value
|
20
|
+
last_rx = keys[0..-2].reduce([h,[]]) do |(rx,rx_key_stack),key|
|
21
|
+
rx_key_stack = rx_key_stack << [rx,key]
|
22
|
+
rx[key] = proc.call(rx_key_stack) if !rx[key]
|
23
|
+
[rx[key], rx_key_stack]
|
24
|
+
end[0]
|
25
|
+
|
26
|
+
# set the value
|
27
|
+
last_rx[keys[-1]]=value
|
6
28
|
end
|
7
29
|
end
|
8
30
|
|
31
|
+
def fetch_in(*keys)
|
32
|
+
FetchIn.fetch_in(self, *keys)
|
33
|
+
end
|
34
|
+
|
9
35
|
# store_in stores a value in a nested associative structure
|
10
36
|
# new levels in the structure are created as required, by the supplied Proc.
|
11
37
|
# if no Proc is supplied, new levels are created with the same class as
|
12
38
|
# the previous level
|
13
39
|
def store_in(*keys_and_value, &proc)
|
14
|
-
|
15
|
-
keys = keys_and_value[0..-2]
|
16
|
-
value = keys_and_value[-1]
|
17
|
-
|
18
|
-
# find or create the last associative receiver in the chain to the value
|
19
|
-
last_rx = keys[0..-2].reduce([self,[]]) do |(rx,rx_key_stack),key|
|
20
|
-
rx_key_stack = rx_key_stack << [rx,key]
|
21
|
-
rx[key] = proc.call(rx_key_stack) if !rx[key]
|
22
|
-
[rx[key], rx_key_stack]
|
23
|
-
end[0]
|
24
|
-
|
25
|
-
# set the value
|
26
|
-
last_rx[keys[-1]]=value
|
40
|
+
FetchIn.store_in(self, *keys_and_value, &proc)
|
27
41
|
end
|
28
42
|
end
|
29
43
|
|
data/spec/fetch_in_spec.rb
CHANGED
@@ -37,6 +37,11 @@ describe "FetchIn" do
|
|
37
37
|
it "should fetch from mixed array and hash nestings" do
|
38
38
|
{:foo=>[nil,[nil,{:bar=>5}]]}.fetch_in(:foo,1,1,:bar).should == 5
|
39
39
|
end
|
40
|
+
|
41
|
+
it "should permit direct class-method invocation" do
|
42
|
+
FetchIn.fetch_in({:foo=>[nil,[nil,{:bar=>5}]]},
|
43
|
+
:foo,1,1,:bar).should == 5
|
44
|
+
end
|
40
45
|
end
|
41
46
|
|
42
47
|
describe "store_in" do
|
@@ -99,5 +104,11 @@ describe "FetchIn" do
|
|
99
104
|
s.store_in(1,2,3){|rx_key_stack| {}}
|
100
105
|
s.should == [nil,{2=>3}]
|
101
106
|
end
|
107
|
+
|
108
|
+
it "should permit direct class-method invocation" do
|
109
|
+
s=[]
|
110
|
+
FetchIn.store_in(s,1,2,3){|rx_key_stack| {}}
|
111
|
+
s.should == [nil,{2=>3}]
|
112
|
+
end
|
102
113
|
end
|
103
114
|
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fetch_in
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- mccraig mccraig of the clan mccraig
|
@@ -14,30 +15,34 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2010-
|
18
|
+
date: 2010-10-15 00:00:00 +01:00
|
18
19
|
default_executable:
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
21
22
|
name: rspec
|
22
23
|
prerelease: false
|
23
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
24
26
|
requirements:
|
25
27
|
- - ">="
|
26
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 15
|
27
30
|
segments:
|
28
31
|
- 1
|
29
32
|
- 2
|
30
|
-
-
|
31
|
-
version: 1.2.
|
33
|
+
- 8
|
34
|
+
version: 1.2.8
|
32
35
|
type: :development
|
33
36
|
version_requirements: *id001
|
34
37
|
- !ruby/object:Gem::Dependency
|
35
38
|
name: rr
|
36
39
|
prerelease: false
|
37
40
|
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
38
42
|
requirements:
|
39
43
|
- - ">="
|
40
44
|
- !ruby/object:Gem::Version
|
45
|
+
hash: 61
|
41
46
|
segments:
|
42
47
|
- 0
|
43
48
|
- 10
|
@@ -75,23 +80,27 @@ rdoc_options:
|
|
75
80
|
require_paths:
|
76
81
|
- lib
|
77
82
|
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
78
84
|
requirements:
|
79
85
|
- - ">="
|
80
86
|
- !ruby/object:Gem::Version
|
87
|
+
hash: 3
|
81
88
|
segments:
|
82
89
|
- 0
|
83
90
|
version: "0"
|
84
91
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
none: false
|
85
93
|
requirements:
|
86
94
|
- - ">="
|
87
95
|
- !ruby/object:Gem::Version
|
96
|
+
hash: 3
|
88
97
|
segments:
|
89
98
|
- 0
|
90
99
|
version: "0"
|
91
100
|
requirements: []
|
92
101
|
|
93
102
|
rubyforge_project:
|
94
|
-
rubygems_version: 1.3.
|
103
|
+
rubygems_version: 1.3.7
|
95
104
|
signing_key:
|
96
105
|
specification_version: 3
|
97
106
|
summary: simple value fetching in nested associative structures
|