hash_walker 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +63 -1
- data/hash_walker.gemspec +2 -2
- data/lib/hash_walker/core_extensions/hash.rb +6 -6
- data/spec/hash_walker/hash_walker_spec.rb +514 -114
- metadata +4 -4
data/README.md
CHANGED
@@ -3,7 +3,9 @@
|
|
3
3
|
[![Build
|
4
4
|
Status](https://secure.travis-ci.org/lloydmeta/hash_walker.png)](http://travis-ci.org/lloydmeta/hash_walker)
|
5
5
|
|
6
|
-
A simple gem that allows you to traverse/walk a Hash according to a set of keys (also a hash), passing in a block to perform actions
|
6
|
+
A simple gem that allows you to traverse/walk a Hash (perhaps obtained from doing JSON::parse on a JSON string) according to a set of keys (also a hash), passing in a block to perform actions. This method will yield your block with each value found and the Hash 'path' of the value as arguments.
|
7
|
+
|
8
|
+
__Note__ This gem was built on Ruby 1.9.3, but Travis testing tells me its 1.9.2 compatible as well. Your mileage may vary...
|
7
9
|
|
8
10
|
## Installing
|
9
11
|
|
@@ -19,6 +21,66 @@ Where you want to use the method on your hashes:
|
|
19
21
|
require 'hash_walker'
|
20
22
|
```
|
21
23
|
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
require 'hash_walker'
|
28
|
+
|
29
|
+
my_hash = {
|
30
|
+
"a_array" => [1,2,3,4,5],
|
31
|
+
"b_hash" => {
|
32
|
+
"b_value_string" => 'b value as string',
|
33
|
+
"b_value_int" => 420,
|
34
|
+
"b_value_bool" => true,
|
35
|
+
"b_value_float" => 4.20,
|
36
|
+
"b_inner_array" => [
|
37
|
+
{
|
38
|
+
"content" => 'b_inner_array content 1',
|
39
|
+
"b_inner_array_unneeded" => "don't read me!"
|
40
|
+
},
|
41
|
+
{
|
42
|
+
"content" => 'b_inner_array content 2',
|
43
|
+
"b_inner_array_unneeded" => "don't read me! 2"
|
44
|
+
},
|
45
|
+
{
|
46
|
+
"content" => 'b_inner_array content 3',
|
47
|
+
"b_inner_array_unneeded" => "don't read me! 3"
|
48
|
+
},
|
49
|
+
{
|
50
|
+
"content" => 'b_inner_array content 4',
|
51
|
+
"b_inner_array_unneeded" => "don't read me! 3",
|
52
|
+
"b_inner_array_inner_hash" => {
|
53
|
+
"content" => "really, really hidden...",
|
54
|
+
"inner_array" => [3.14159, 2.71828]
|
55
|
+
}
|
56
|
+
}
|
57
|
+
]
|
58
|
+
}
|
59
|
+
}
|
60
|
+
|
61
|
+
keys_to_read = [
|
62
|
+
"a_array",
|
63
|
+
"b_hash" => [
|
64
|
+
"b_value_string",
|
65
|
+
"b_value_int",
|
66
|
+
"b_value_bool",
|
67
|
+
"b_value_float",
|
68
|
+
{"b_inner_array" => [
|
69
|
+
"content",
|
70
|
+
{"b_inner_array_inner_hash" => [
|
71
|
+
"content",
|
72
|
+
"inner_array"
|
73
|
+
]}
|
74
|
+
]}
|
75
|
+
]
|
76
|
+
]
|
77
|
+
|
78
|
+
my_hash.each_primitive_value_at(keys_to_read){|value,path|
|
79
|
+
puts value, path
|
80
|
+
}
|
81
|
+
```
|
82
|
+
|
83
|
+
|
22
84
|
## License
|
23
85
|
|
24
86
|
Copyright (c) 2012 by Lloyd Chan
|
data/hash_walker.gemspec
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
Gem::Specification.new do |gem|
|
2
2
|
gem.name = %q{hash_walker}
|
3
|
-
gem.version = "0.0.
|
3
|
+
gem.version = "0.0.3"
|
4
4
|
gem.date = %q{2012-09-30}
|
5
5
|
gem.authors = ["Lloyd Meta"]
|
6
6
|
gem.email = ["lloydmeta@gmail.com"]
|
7
7
|
gem.homepage = "http://github.com/lloydmeta/hash_walker"
|
8
|
-
gem.description = %q{A simple gem that allows you to traverse/walk a Hash according to a set of keys (also a hash), passing in a block to perform actions
|
8
|
+
gem.description = %q{A simple gem that allows you to traverse/walk a Hash according to a set of keys (also a hash), passing in a block to perform actions. This method will yield your block with each value found and the Hash 'path' of the value as arguments}
|
9
9
|
gem.summary = gem.description
|
10
10
|
|
11
11
|
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
@@ -15,23 +15,23 @@ module HashWalker
|
|
15
15
|
if key.is_a?(Hash)
|
16
16
|
# iterate for each key, value in this hash
|
17
17
|
key.each do |k, v|
|
18
|
-
node_key_value = self[
|
18
|
+
node_key_value = self[k]
|
19
19
|
if node_key_value.is_a?(Array)
|
20
20
|
node_key_value.each_with_index do |value, i|
|
21
|
-
value.each_primitive_value_at(v, path + [
|
21
|
+
value.each_primitive_value_at(v, path + [k] + [i], &block)
|
22
22
|
end
|
23
23
|
else
|
24
|
-
node_key_value.each_primitive_value_at(v, path + [
|
24
|
+
node_key_value.each_primitive_value_at(v, path + [k], &block) unless node_key_value.nil?
|
25
25
|
end
|
26
26
|
end
|
27
27
|
else
|
28
|
-
node_key_value = self[
|
28
|
+
node_key_value = self[key]
|
29
29
|
if node_key_value.is_a?(Array)
|
30
30
|
node_key_value.each_with_index do |value, i|
|
31
|
-
yield value, path + [
|
31
|
+
yield value, path + [key] + [i] if block_given?
|
32
32
|
end
|
33
33
|
elsif PRIMITIVE_CLASSES_TO_RETURN_VALUES_FOR.any?{|x| node_key_value.is_a?(x)}
|
34
|
-
yield node_key_value, path + [
|
34
|
+
yield node_key_value, path + [key] if block_given?
|
35
35
|
end
|
36
36
|
end
|
37
37
|
end
|
@@ -1,136 +1,536 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Hash do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
"
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
"
|
30
|
-
|
4
|
+
context 'Simple Example' do
|
5
|
+
subject do
|
6
|
+
{
|
7
|
+
"a_array" => [1,2,3,4,5],
|
8
|
+
"b_hash" => {
|
9
|
+
"b_value_string" => 'b value as string',
|
10
|
+
"b_value_int" => 420,
|
11
|
+
"b_value_bool" => true,
|
12
|
+
"b_value_float" => 4.20,
|
13
|
+
"b_inner_array" => [
|
14
|
+
{
|
15
|
+
"content" => 'b_inner_array content 1',
|
16
|
+
"b_inner_array_unneeded" => "don't read me!"
|
17
|
+
},
|
18
|
+
{
|
19
|
+
"content" => 'b_inner_array content 2',
|
20
|
+
"b_inner_array_unneeded" => "don't read me! 2"
|
21
|
+
},
|
22
|
+
{
|
23
|
+
"content" => 'b_inner_array content 3',
|
24
|
+
"b_inner_array_unneeded" => "don't read me! 3"
|
25
|
+
},
|
26
|
+
{
|
27
|
+
"content" => 'b_inner_array content 4',
|
28
|
+
"b_inner_array_unneeded" => "don't read me! 3",
|
29
|
+
"b_inner_array_inner_hash" => {
|
30
|
+
"content" => "really, really hidden...",
|
31
|
+
"inner_array" => [3.14159, 2.71828]
|
32
|
+
}
|
31
33
|
}
|
32
|
-
|
33
|
-
|
34
|
+
]
|
35
|
+
}
|
34
36
|
}
|
35
|
-
|
36
|
-
end
|
37
|
+
end
|
37
38
|
|
38
|
-
|
39
|
+
before(:all) do
|
39
40
|
|
40
|
-
|
41
|
-
|
42
|
-
|
41
|
+
@keys_to_read_a_array = [
|
42
|
+
"a_array"
|
43
|
+
]
|
43
44
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
45
|
+
@keys_to_read_b_hash_primitives = [
|
46
|
+
"b_hash" => [
|
47
|
+
"b_value_string",
|
48
|
+
"b_value_int",
|
49
|
+
"b_value_bool",
|
50
|
+
"b_value_float",
|
51
|
+
]
|
50
52
|
]
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
53
|
+
|
54
|
+
@keys_to_read_complete = [
|
55
|
+
"a_array",
|
56
|
+
"b_hash" => [
|
57
|
+
"b_value_string",
|
58
|
+
"b_value_int",
|
59
|
+
"b_value_bool",
|
60
|
+
"b_value_float",
|
61
|
+
{"b_inner_array" => ["content"]}
|
62
|
+
]
|
61
63
|
]
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
{"b_inner_array" => [
|
72
|
-
"content",
|
73
|
-
{"b_inner_array_inner_hash" => [
|
64
|
+
|
65
|
+
@keys_to_read_REALLY_complete = [
|
66
|
+
"a_array",
|
67
|
+
"b_hash" => [
|
68
|
+
"b_value_string",
|
69
|
+
"b_value_int",
|
70
|
+
"b_value_bool",
|
71
|
+
"b_value_float",
|
72
|
+
{"b_inner_array" => [
|
74
73
|
"content",
|
75
|
-
"
|
74
|
+
{"b_inner_array_inner_hash" => [
|
75
|
+
"content",
|
76
|
+
"inner_array"
|
77
|
+
]}
|
76
78
|
]}
|
77
|
-
]
|
79
|
+
]
|
78
80
|
]
|
79
|
-
|
80
|
-
end
|
81
|
+
end
|
81
82
|
|
82
|
-
|
83
|
-
|
84
|
-
|
83
|
+
it 'should have the each_primitive_value_at method' do
|
84
|
+
subject.should respond_to(:each_primitive_value_at)
|
85
|
+
end
|
85
86
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
87
|
+
it 'should find 5 values for @keys_to_read_a_array key array' do
|
88
|
+
values_found = []
|
89
|
+
subject.each_primitive_value_at(@keys_to_read_a_array){|value,path|
|
90
|
+
values_found << value
|
91
|
+
subject['a_array'].should include(value)
|
92
|
+
path.should include('a_array')
|
93
|
+
path[1].class.should eq(Fixnum)
|
94
|
+
}
|
95
|
+
values_found.size.should eq(5)
|
96
|
+
end
|
96
97
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
98
|
+
it 'should find 5 values for @keys_to_read_b_hash_primitives key array' do
|
99
|
+
values_found = []
|
100
|
+
b_hash_primitives = [
|
101
|
+
'b value as string',
|
102
|
+
420,
|
103
|
+
true,
|
104
|
+
4.2
|
105
|
+
]
|
106
|
+
subject.each_primitive_value_at(@keys_to_read_b_hash_primitives){|value,path|
|
107
|
+
values_found << value
|
108
|
+
b_hash_primitives.should include(value)
|
109
|
+
path.should include('b_hash')
|
110
|
+
@keys_to_read_b_hash_primitives[0]['b_hash'].should include(path[1])
|
111
|
+
}
|
112
|
+
values_found.size.should eq(4)
|
113
|
+
end
|
113
114
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
115
|
+
it 'should find 12 values in total for @keys_to_read_complete' do
|
116
|
+
values_found = []
|
117
|
+
paths_found = []
|
118
|
+
subject.each_primitive_value_at(@keys_to_read_complete){|value,path|
|
119
|
+
values_found << value
|
120
|
+
paths_found << path
|
121
|
+
}
|
122
|
+
values_found.size.should eq(13)
|
123
|
+
paths_found.size.should eq(13)
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'should find 16 values in total for @keys_to_read_REALLY_complete' do
|
127
|
+
values_found = []
|
128
|
+
paths_found = []
|
129
|
+
subject.each_primitive_value_at(@keys_to_read_REALLY_complete){|value,path|
|
130
|
+
values_found << value
|
131
|
+
paths_found << path
|
132
|
+
}
|
133
|
+
values_found.size.should eq(16)
|
134
|
+
paths_found.size.should eq(16)
|
135
|
+
end
|
123
136
|
end
|
124
137
|
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
138
|
+
context 'complicated example' do
|
139
|
+
subject do
|
140
|
+
{
|
141
|
+
"_confidence_factor"=>90,
|
142
|
+
"_backup_level"=>4,
|
143
|
+
"_id"=>"www.something_here.com",
|
144
|
+
"_last_studio_import"=>"2012-04-03T18:19:03.139000",
|
145
|
+
"_last_updated"=>"2012-09-19T18:45:55.288000",
|
146
|
+
"_manual_article_popular_search"=>[
|
147
|
+
{
|
148
|
+
"cached_ids"=>[
|
149
|
+
{
|
150
|
+
"_id"=>"aaaa",
|
151
|
+
"summary"=>"aaa ..."
|
152
|
+
},
|
153
|
+
{
|
154
|
+
"_id"=>"bbbb",
|
155
|
+
"summary"=>"bbbb...."
|
156
|
+
}
|
157
|
+
],
|
158
|
+
"start_time"=>"2011-01-01T00:00:00"
|
159
|
+
}
|
160
|
+
],
|
161
|
+
"_type"=>"document",
|
162
|
+
"article_type_id"=>"Professional",
|
163
|
+
"asset_meta"=>{
|
164
|
+
"count"=>{
|
165
|
+
"reference"=>0,
|
166
|
+
"reference_link"=>0,
|
167
|
+
"resource"=>2,
|
168
|
+
"warning"=>0,
|
169
|
+
"word_detail"=>955,
|
170
|
+
"word_summary"=>224
|
171
|
+
}
|
172
|
+
},
|
173
|
+
"byline"=>"Contributor",
|
174
|
+
"children"=>[
|
175
|
+
|
176
|
+
],
|
177
|
+
"content_rating"=>4,
|
178
|
+
"content_type"=>"Features",
|
179
|
+
"culture"=>"en-US",
|
180
|
+
"dart_zone"=>"holidays",
|
181
|
+
"description"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join,
|
182
|
+
"documents"=>[
|
183
|
+
|
184
|
+
],
|
185
|
+
"image"=>{
|
186
|
+
"credit"=>"sjlocke/iStockphoto.com",
|
187
|
+
"height"=>565,
|
188
|
+
"thumbheight"=>"67",
|
189
|
+
"width"=>849
|
190
|
+
},
|
191
|
+
"introduction"=>{
|
192
|
+
"steps"=>[
|
193
|
+
{
|
194
|
+
"image"=>{
|
195
|
+
"credit"=>"sjlocke/iStockphoto.com",
|
196
|
+
"height"=>565,
|
197
|
+
"thumbheight"=>"67",
|
198
|
+
"width"=>849
|
199
|
+
},
|
200
|
+
"paragraph"=>[
|
201
|
+
{
|
202
|
+
"content"=>"jklas jfklsj afkl;sj flk;asd."
|
203
|
+
},
|
204
|
+
{
|
205
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
206
|
+
},
|
207
|
+
{
|
208
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
209
|
+
},
|
210
|
+
{
|
211
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
212
|
+
},
|
213
|
+
{
|
214
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
215
|
+
},
|
216
|
+
{
|
217
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
218
|
+
},
|
219
|
+
{
|
220
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
221
|
+
}
|
222
|
+
],
|
223
|
+
"title"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
224
|
+
}
|
225
|
+
]
|
226
|
+
},
|
227
|
+
"is_ugc"=>false,
|
228
|
+
"keywords"=>[
|
229
|
+
(0...50).map{ ('a'..'z').to_a[rand(26)] }.join,
|
230
|
+
(0...50).map{ ('a'..'z').to_a[rand(26)] }.join,
|
231
|
+
(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
232
|
+
],
|
233
|
+
"last_pub_date"=>"2011-04-08T10:57:56",
|
234
|
+
"last_published_date"=>"2012-04-03T16:03:00",
|
235
|
+
"last_rcp_import"=>"2012-09-24T00:04:06.116000",
|
236
|
+
"level"=>4,
|
237
|
+
"pub_date"=>"2011-07-13T00:00:00",
|
238
|
+
"publisher_id"=>7447502,
|
239
|
+
"quote"=>{
|
240
|
+
"steps"=>[
|
241
|
+
{
|
242
|
+
"paragraph"=>[
|
243
|
+
{
|
244
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
245
|
+
}
|
246
|
+
],
|
247
|
+
"title"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
248
|
+
},
|
249
|
+
{
|
250
|
+
"paragraph"=>[
|
251
|
+
{
|
252
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
253
|
+
}
|
254
|
+
],
|
255
|
+
"title"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
256
|
+
}
|
257
|
+
],
|
258
|
+
"title"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
259
|
+
},
|
260
|
+
"references"=>[
|
261
|
+
|
262
|
+
],
|
263
|
+
"resources"=>[
|
264
|
+
{
|
265
|
+
"title"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join,
|
266
|
+
"url"=>"http://www.aap.org/advocacy/releases/octhalloween.cfm"
|
267
|
+
},
|
268
|
+
{
|
269
|
+
"title"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join,
|
270
|
+
"url"=>"http://www.cdc.gov/family/halloween/"
|
271
|
+
}
|
272
|
+
],
|
273
|
+
"sections"=>[
|
274
|
+
{
|
275
|
+
"steps"=>[
|
276
|
+
{
|
277
|
+
"paragraph"=>[
|
278
|
+
{
|
279
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
280
|
+
},
|
281
|
+
{
|
282
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
283
|
+
},
|
284
|
+
{
|
285
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
286
|
+
},
|
287
|
+
{
|
288
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
289
|
+
},
|
290
|
+
{
|
291
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
292
|
+
},
|
293
|
+
{
|
294
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
295
|
+
}
|
296
|
+
]
|
297
|
+
}
|
298
|
+
],
|
299
|
+
"title"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
300
|
+
},
|
301
|
+
{
|
302
|
+
"steps"=>[
|
303
|
+
{
|
304
|
+
"paragraph"=>[
|
305
|
+
{
|
306
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
307
|
+
},
|
308
|
+
{
|
309
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
310
|
+
},
|
311
|
+
{
|
312
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
313
|
+
},
|
314
|
+
{
|
315
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
316
|
+
},
|
317
|
+
{
|
318
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
319
|
+
},
|
320
|
+
{
|
321
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
322
|
+
}
|
323
|
+
]
|
324
|
+
}
|
325
|
+
],
|
326
|
+
"title"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
327
|
+
},
|
328
|
+
{
|
329
|
+
"steps"=>[
|
330
|
+
{
|
331
|
+
"paragraph"=>[
|
332
|
+
{
|
333
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
334
|
+
},
|
335
|
+
{
|
336
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
337
|
+
},
|
338
|
+
{
|
339
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
340
|
+
},
|
341
|
+
{
|
342
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
343
|
+
},
|
344
|
+
{
|
345
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
346
|
+
}
|
347
|
+
]
|
348
|
+
}
|
349
|
+
],
|
350
|
+
"title"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
351
|
+
},
|
352
|
+
{
|
353
|
+
"steps"=>[
|
354
|
+
{
|
355
|
+
"paragraph"=>[
|
356
|
+
{
|
357
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
358
|
+
},
|
359
|
+
{
|
360
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
361
|
+
},
|
362
|
+
{
|
363
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
364
|
+
},
|
365
|
+
{
|
366
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
367
|
+
},
|
368
|
+
{
|
369
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
370
|
+
}
|
371
|
+
]
|
372
|
+
}
|
373
|
+
],
|
374
|
+
"title"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
375
|
+
},
|
376
|
+
{
|
377
|
+
"steps"=>[
|
378
|
+
{
|
379
|
+
"paragraph"=>[
|
380
|
+
{
|
381
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
382
|
+
},
|
383
|
+
{
|
384
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
385
|
+
},
|
386
|
+
{
|
387
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
388
|
+
}
|
389
|
+
]
|
390
|
+
}
|
391
|
+
],
|
392
|
+
"title"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
393
|
+
},
|
394
|
+
{
|
395
|
+
"steps"=>[
|
396
|
+
{
|
397
|
+
"paragraph"=>[
|
398
|
+
{
|
399
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
400
|
+
},
|
401
|
+
{
|
402
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
403
|
+
},
|
404
|
+
{
|
405
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
406
|
+
}
|
407
|
+
]
|
408
|
+
}
|
409
|
+
],
|
410
|
+
"title"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
411
|
+
}
|
412
|
+
],
|
413
|
+
"sidebar"=>{
|
414
|
+
"steps"=>[
|
415
|
+
{
|
416
|
+
"paragraph"=>[
|
417
|
+
{
|
418
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
419
|
+
},
|
420
|
+
{
|
421
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
422
|
+
},
|
423
|
+
{
|
424
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
425
|
+
},
|
426
|
+
{
|
427
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
428
|
+
},
|
429
|
+
{
|
430
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
431
|
+
},
|
432
|
+
{
|
433
|
+
"content"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
434
|
+
}
|
435
|
+
],
|
436
|
+
"title"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
437
|
+
}
|
438
|
+
],
|
439
|
+
"title"=>"Sidebar"
|
440
|
+
},
|
441
|
+
"subtitle"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join,
|
442
|
+
"summary"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join,
|
443
|
+
"summary_history"=>[
|
444
|
+
(0...50).map{ ('a'..'z').to_a[rand(26)] }.join,
|
445
|
+
(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
|
446
|
+
],
|
447
|
+
"summary_is_first_section"=>true,
|
448
|
+
"things_needed"=>[
|
449
|
+
|
450
|
+
],
|
451
|
+
"tips"=>[
|
452
|
+
|
453
|
+
],
|
454
|
+
"title"=>(0...50).map{ ('a'..'z').to_a[rand(26)] }.join,
|
455
|
+
"title_id"=>6031231226583,
|
456
|
+
"title_source_name"=>"ADMIN_SUGGESTED",
|
457
|
+
"type"=>"Article",
|
458
|
+
"warnings"=>[
|
459
|
+
|
460
|
+
],
|
461
|
+
"who_can_help"=>nil,
|
462
|
+
"writer"=>"www.demandstudios.com/contributor/c77ecbad-49ea-4afc-95be-3978c8033035"
|
463
|
+
}
|
464
|
+
end
|
465
|
+
|
466
|
+
describe 'validations' do
|
467
|
+
before(:all) do
|
468
|
+
@keys_to_read = [
|
469
|
+
"content",
|
470
|
+
"description",
|
471
|
+
"ingredients",
|
472
|
+
"keywords",
|
473
|
+
{"image" => ["caption"]},
|
474
|
+
{"introduction" => [
|
475
|
+
"steps" => [
|
476
|
+
{"image" => ["caption"]},
|
477
|
+
{"paragraph" => ["content"]},
|
478
|
+
"title"
|
479
|
+
]
|
480
|
+
]},
|
481
|
+
{"quote" => [
|
482
|
+
"steps" => [
|
483
|
+
{"image" => ["caption"]},
|
484
|
+
{"paragraph" => ["content"]},
|
485
|
+
"title"
|
486
|
+
]
|
487
|
+
]},
|
488
|
+
{"references" => ["title"]},
|
489
|
+
{"sections" => [
|
490
|
+
"steps" => [
|
491
|
+
{"paragraph" => ["content"]},
|
492
|
+
{"image" => ["caption"]},
|
493
|
+
"title"
|
494
|
+
]
|
495
|
+
]},
|
496
|
+
"subtitle",
|
497
|
+
"summary",
|
498
|
+
"things_needed",
|
499
|
+
"tips",
|
500
|
+
"title",
|
501
|
+
"warnings"
|
502
|
+
]
|
503
|
+
end
|
504
|
+
|
505
|
+
it "should traverse without problems" do
|
506
|
+
lambda do
|
507
|
+
subject.each_primitive_value_at(@keys_to_read)
|
508
|
+
end.should_not raise_error
|
509
|
+
end
|
510
|
+
|
511
|
+
it 'should return me values with proper paths' do
|
512
|
+
values_and_paths_found = []
|
513
|
+
|
514
|
+
subject.each_primitive_value_at(@keys_to_read){|value, path_array|
|
515
|
+
path_as_string = path_array.reduce('') {|path_so_far, path_array_element|
|
516
|
+
if path_array_element.is_a?(String)
|
517
|
+
path_so_far += %Q~["#{path_array_element}"]~
|
518
|
+
elsif path_array_element.is_a?(Integer)
|
519
|
+
path_so_far += %Q~[#{path_array_element.to_s}]~
|
520
|
+
end
|
521
|
+
path_so_far
|
522
|
+
}
|
523
|
+
values_and_paths_found << [value, path_array]
|
524
|
+
}
|
525
|
+
values_and_paths_found.size.should eq(47)
|
526
|
+
values_and_paths_found.each do |value_and_path|
|
527
|
+
subject_value_at_path = value_and_path[1].reduce(subject){|value_so_far, step|
|
528
|
+
value_so_far = value_so_far[step]
|
529
|
+
}
|
530
|
+
subject_value_at_path.should eq(value_and_path[0])
|
531
|
+
end
|
532
|
+
end
|
533
|
+
end
|
134
534
|
end
|
135
535
|
|
136
536
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_walker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -12,8 +12,8 @@ cert_chain: []
|
|
12
12
|
date: 2012-09-30 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A simple gem that allows you to traverse/walk a Hash according to a set
|
15
|
-
of keys (also a hash), passing in a block to perform actions
|
16
|
-
|
15
|
+
of keys (also a hash), passing in a block to perform actions. This method will yield
|
16
|
+
your block with each value found and the Hash 'path' of the value as arguments
|
17
17
|
email:
|
18
18
|
- lloydmeta@gmail.com
|
19
19
|
executables: []
|
@@ -55,7 +55,7 @@ rubygems_version: 1.8.24
|
|
55
55
|
signing_key:
|
56
56
|
specification_version: 3
|
57
57
|
summary: A simple gem that allows you to traverse/walk a Hash according to a set of
|
58
|
-
keys (also a hash), passing in a block to perform actions
|
58
|
+
keys (also a hash), passing in a block to perform actions. This method will yield
|
59
59
|
your block with each value found and the Hash 'path' of the value as arguments
|
60
60
|
test_files:
|
61
61
|
- spec/hash_walker/hash_walker_spec.rb
|