hash_walker 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +8 -0
- data/hash_walker.gemspec +3 -13
- data/lib/hash_walker/core_extensions/hash.rb +14 -21
- data/spec/hash_walker/hash_walker_spec.rb +5 -5
- metadata +6 -4
data/README.md
CHANGED
@@ -3,6 +3,8 @@
|
|
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 on. This method will yield your block with each value found and the Hash 'path' of the value as arguments
|
7
|
+
|
6
8
|
## Installing
|
7
9
|
|
8
10
|
Add to your `Gemfile`:
|
@@ -11,6 +13,12 @@ Add to your `Gemfile`:
|
|
11
13
|
gem 'hash_walker'
|
12
14
|
```
|
13
15
|
|
16
|
+
Where you want to use the method on your hashes:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
require 'hash_walker'
|
20
|
+
```
|
21
|
+
|
14
22
|
## License
|
15
23
|
|
16
24
|
Copyright (c) 2012 by Lloyd Chan
|
data/hash_walker.gemspec
CHANGED
@@ -1,21 +1,11 @@
|
|
1
1
|
Gem::Specification.new do |gem|
|
2
|
-
gem.name = %q{hash_walker}
|
3
|
-
gem.version = "0.0.1"
|
4
|
-
gem.date = %q{2012-09-29}
|
5
|
-
gem.summary = %q{A simple gem that allows you to traverse/walk a Hash according to a set of keys (also a hash) and return the primitive values at those keys.}
|
6
|
-
gem.files = [
|
7
|
-
"lib/hash_walker/core_extensions/hash.rb"
|
8
|
-
]
|
9
|
-
gem.require_paths = ["lib","lib/hash_walker", "lib/hash_walker/core_extensions"]
|
10
|
-
gem.require_paths = ["lib/hash_walker"]
|
11
|
-
|
12
2
|
gem.name = %q{hash_walker}
|
13
|
-
gem.version = "0.0.
|
14
|
-
gem.date = %q{2012-09-
|
3
|
+
gem.version = "0.0.2"
|
4
|
+
gem.date = %q{2012-09-30}
|
15
5
|
gem.authors = ["Lloyd Meta"]
|
16
6
|
gem.email = ["lloydmeta@gmail.com"]
|
17
7
|
gem.homepage = "http://github.com/lloydmeta/hash_walker"
|
18
|
-
gem.description = %q{A simple gem that allows you to traverse/walk a Hash according to a set of keys (also a hash) and
|
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 on. This method will yield your block with each value found and the Hash 'path' of the value as arguments}
|
19
9
|
gem.summary = gem.description
|
20
10
|
|
21
11
|
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
@@ -1,6 +1,15 @@
|
|
1
1
|
module HashWalker
|
2
2
|
module CoreExtensions
|
3
3
|
module Hash
|
4
|
+
|
5
|
+
PRIMITIVE_CLASSES_TO_RETURN_VALUES_FOR = [
|
6
|
+
String,
|
7
|
+
TrueClass,
|
8
|
+
FalseClass,
|
9
|
+
Integer,
|
10
|
+
Float
|
11
|
+
]
|
12
|
+
|
4
13
|
def each_primitive_value_at(keys, path = [], &block)
|
5
14
|
keys.each do |key|
|
6
15
|
if key.is_a?(Hash)
|
@@ -9,36 +18,20 @@ module HashWalker
|
|
9
18
|
node_key_value = self["#{k.to_s}"]
|
10
19
|
if node_key_value.is_a?(Array)
|
11
20
|
node_key_value.each_with_index do |value, i|
|
12
|
-
|
13
|
-
new_path = path + ["#{k.to_s}"] + [i]
|
14
|
-
value.each_primitive_value_at(v, new_path, &block)
|
21
|
+
value.each_primitive_value_at(v, path + ["#{k.to_s}"] + [i], &block)
|
15
22
|
end
|
16
23
|
else
|
17
|
-
|
18
|
-
new_path = path + ["#{k.to_s}"]
|
19
|
-
node_key_value.each_primitive_value_at(v, new_path, &block) unless node_key_value.nil?
|
24
|
+
node_key_value.each_primitive_value_at(v, path + ["#{k.to_s}"], &block) unless node_key_value.nil?
|
20
25
|
end
|
21
26
|
end
|
22
27
|
else
|
23
28
|
node_key_value = self["#{key.to_s}"]
|
24
29
|
if node_key_value.is_a?(Array)
|
25
30
|
node_key_value.each_with_index do |value, i|
|
26
|
-
|
27
|
-
new_path = path + ["#{key.to_s}"] + [i]
|
28
|
-
if block_given?
|
29
|
-
yield value, new_path
|
30
|
-
else
|
31
|
-
[value, new_path]
|
32
|
-
end
|
33
|
-
end
|
34
|
-
elsif [String, TrueClass, FalseClass, Integer, Float].any?{|x| node_key_value.is_a?(x)}
|
35
|
-
#new_path = path + %Q~["#{key.to_s}"]~
|
36
|
-
new_path = path + ["#{key.to_s}"]
|
37
|
-
if block_given?
|
38
|
-
yield node_key_value, new_path
|
39
|
-
else
|
40
|
-
[node_key_value, new_path]
|
31
|
+
yield value, path + ["#{key.to_s}"] + [i] if block_given?
|
41
32
|
end
|
33
|
+
elsif PRIMITIVE_CLASSES_TO_RETURN_VALUES_FOR.any?{|x| node_key_value.is_a?(x)}
|
34
|
+
yield node_key_value, path + ["#{key.to_s}"] if block_given?
|
42
35
|
end
|
43
36
|
end
|
44
37
|
end
|
@@ -57,7 +57,7 @@ describe Hash do
|
|
57
57
|
"b_value_int",
|
58
58
|
"b_value_bool",
|
59
59
|
"b_value_float",
|
60
|
-
"b_inner_array" => ["content"]
|
60
|
+
{"b_inner_array" => ["content"]}
|
61
61
|
]
|
62
62
|
]
|
63
63
|
|
@@ -68,13 +68,13 @@ describe Hash do
|
|
68
68
|
"b_value_int",
|
69
69
|
"b_value_bool",
|
70
70
|
"b_value_float",
|
71
|
-
"b_inner_array" => [
|
71
|
+
{"b_inner_array" => [
|
72
72
|
"content",
|
73
|
-
"b_inner_array_inner_hash" => [
|
73
|
+
{"b_inner_array_inner_hash" => [
|
74
74
|
"content",
|
75
75
|
"inner_array"
|
76
|
-
]
|
77
|
-
]
|
76
|
+
]}
|
77
|
+
]}
|
78
78
|
]
|
79
79
|
]
|
80
80
|
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.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,10 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
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)
|
15
|
+
of keys (also a hash), passing in a block to perform actions on. This method will
|
16
|
+
yield your block with each value found and the Hash 'path' of the value as arguments
|
16
17
|
email:
|
17
18
|
- lloydmeta@gmail.com
|
18
19
|
executables: []
|
@@ -54,7 +55,8 @@ rubygems_version: 1.8.24
|
|
54
55
|
signing_key:
|
55
56
|
specification_version: 3
|
56
57
|
summary: A simple gem that allows you to traverse/walk a Hash according to a set of
|
57
|
-
keys (also a hash)
|
58
|
+
keys (also a hash), passing in a block to perform actions on. This method will yield
|
59
|
+
your block with each value found and the Hash 'path' of the value as arguments
|
58
60
|
test_files:
|
59
61
|
- spec/hash_walker/hash_walker_spec.rb
|
60
62
|
- spec/spec_helper.rb
|