hash_walker 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format=documentation
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ group :development, :test do
6
+ gem 'rake'
7
+ gem 'rspec'
8
+ gem 'simplecov'
9
+ end
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # hash_walker
2
+
3
+ [![Build
4
+ Status](https://secure.travis-ci.org/lloydmeta/hash_walker.png)](http://travis-ci.org/lloydmeta/hash_walker)
5
+
6
+ ## Installing
7
+
8
+ Add to your `Gemfile`:
9
+
10
+ ```ruby
11
+ gem 'hash_walker'
12
+ ```
13
+
14
+ ## License
15
+
16
+ Copyright (c) 2012 by Lloyd Chan
17
+
18
+ Permission is hereby granted, free of charge, to any person obtaining a
19
+ copy of this software and associated documentation files (the
20
+ "Software"), to deal in the Software without restriction, including
21
+ without limitation the rights to use, copy, modify, merge, publish,
22
+ distribute, sublicense, and/or sell copies of the Software, and to
23
+ permit persons to whom the Software is furnished to do so, subject to
24
+ the following conditions:
25
+
26
+ The above copyright notice and this permission notice shall be included
27
+ in all copies or substantial portions of the Software.
28
+
29
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
30
+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
32
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
33
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
34
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
35
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new do |t|
6
+ end
7
+
8
+ task :default => :spec
@@ -0,0 +1,25 @@
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
+ gem.name = %q{hash_walker}
13
+ gem.version = "0.0.1"
14
+ gem.date = %q{2012-09-29}
15
+ gem.authors = ["Lloyd Meta"]
16
+ gem.email = ["lloydmeta@gmail.com"]
17
+ 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 return the primitive values at those keys.}
19
+ gem.summary = gem.description
20
+
21
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ gem.files = `git ls-files`.split("\n")
23
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
24
+ gem.require_paths = ["lib"]
25
+ end
@@ -0,0 +1,52 @@
1
+ module HashWalker
2
+ module CoreExtensions
3
+ module Hash
4
+ def each_primitive_value_at(keys, path = [], &block)
5
+ keys.each do |key|
6
+ if key.is_a?(Hash)
7
+ # iterate for each key, value in this hash
8
+ key.each do |k, v|
9
+ node_key_value = self["#{k.to_s}"]
10
+ if node_key_value.is_a?(Array)
11
+ node_key_value.each_with_index do |value, i|
12
+ #new_path = path + %Q~["#{k.to_s}"][#{i}]~
13
+ new_path = path + ["#{k.to_s}"] + [i]
14
+ value.each_primitive_value_at(v, new_path, &block)
15
+ end
16
+ else
17
+ #new_path = path + %Q~["#{k.to_s}"]~
18
+ new_path = path + ["#{k.to_s}"]
19
+ node_key_value.each_primitive_value_at(v, new_path, &block) unless node_key_value.nil?
20
+ end
21
+ end
22
+ else
23
+ node_key_value = self["#{key.to_s}"]
24
+ if node_key_value.is_a?(Array)
25
+ node_key_value.each_with_index do |value, i|
26
+ #new_path = path + %Q~["#{key.to_s}"][#{i}]~
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]
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ class Hash
51
+ include HashWalker::CoreExtensions::Hash
52
+ end
@@ -0,0 +1 @@
1
+ require 'hash_walker/core_extensions/hash'
@@ -0,0 +1,136 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hash do
4
+ subject do
5
+ {
6
+ "a_array" => [1,2,3,4,5],
7
+ "b_hash" => {
8
+ "b_value_string" => 'b value as string',
9
+ "b_value_int" => 420,
10
+ "b_value_bool" => true,
11
+ "b_value_float" => 4.20,
12
+ "b_inner_array" => [
13
+ {
14
+ "content" => 'b_inner_array content 1',
15
+ "b_inner_array_unneeded" => "don't read me!"
16
+ },
17
+ {
18
+ "content" => 'b_inner_array content 2',
19
+ "b_inner_array_unneeded" => "don't read me! 2"
20
+ },
21
+ {
22
+ "content" => 'b_inner_array content 3',
23
+ "b_inner_array_unneeded" => "don't read me! 3"
24
+ },
25
+ {
26
+ "content" => 'b_inner_array content 4',
27
+ "b_inner_array_unneeded" => "don't read me! 3",
28
+ "b_inner_array_inner_hash" => {
29
+ "content" => "really, really hidden...",
30
+ "inner_array" => [3.14159, 2.71828]
31
+ }
32
+ }
33
+ ]
34
+ }
35
+ }
36
+ end
37
+
38
+ before(:all) do
39
+
40
+ @keys_to_read_a_array = [
41
+ "a_array"
42
+ ]
43
+
44
+ @keys_to_read_b_hash_primitives = [
45
+ "b_hash" => [
46
+ "b_value_string",
47
+ "b_value_int",
48
+ "b_value_bool",
49
+ "b_value_float",
50
+ ]
51
+ ]
52
+
53
+ @keys_to_read_complete = [
54
+ "a_array",
55
+ "b_hash" => [
56
+ "b_value_string",
57
+ "b_value_int",
58
+ "b_value_bool",
59
+ "b_value_float",
60
+ "b_inner_array" => ["content"]
61
+ ]
62
+ ]
63
+
64
+ @keys_to_read_REALLY_complete = [
65
+ "a_array",
66
+ "b_hash" => [
67
+ "b_value_string",
68
+ "b_value_int",
69
+ "b_value_bool",
70
+ "b_value_float",
71
+ "b_inner_array" => [
72
+ "content",
73
+ "b_inner_array_inner_hash" => [
74
+ "content",
75
+ "inner_array"
76
+ ]
77
+ ]
78
+ ]
79
+ ]
80
+ end
81
+
82
+ it 'should have the each_primitive_value_at method' do
83
+ subject.should respond_to(:each_primitive_value_at)
84
+ end
85
+
86
+ it 'should find 5 values for @keys_to_read_a_array key array' do
87
+ values_found = []
88
+ subject.each_primitive_value_at(@keys_to_read_a_array){|value,path|
89
+ values_found << value
90
+ subject['a_array'].should include(value)
91
+ path.should include('a_array')
92
+ path[1].class.should eq(Fixnum)
93
+ }
94
+ values_found.size.should eq(5)
95
+ end
96
+
97
+ it 'should find 5 values for @keys_to_read_b_hash_primitives key array' do
98
+ values_found = []
99
+ b_hash_primitives = [
100
+ 'b value as string',
101
+ 420,
102
+ true,
103
+ 4.2
104
+ ]
105
+ subject.each_primitive_value_at(@keys_to_read_b_hash_primitives){|value,path|
106
+ values_found << value
107
+ b_hash_primitives.should include(value)
108
+ path.should include('b_hash')
109
+ @keys_to_read_b_hash_primitives[0]['b_hash'].should include(path[1])
110
+ }
111
+ values_found.size.should eq(4)
112
+ end
113
+
114
+ it 'should find 12 values in total for @keys_to_read_complete' do
115
+ values_found = []
116
+ paths_found = []
117
+ subject.each_primitive_value_at(@keys_to_read_complete){|value,path|
118
+ values_found << value
119
+ paths_found << path
120
+ }
121
+ values_found.size.should eq(13)
122
+ paths_found.size.should eq(13)
123
+ end
124
+
125
+ it 'should find 16 values in total for @keys_to_read_REALLY_complete' do
126
+ values_found = []
127
+ paths_found = []
128
+ subject.each_primitive_value_at(@keys_to_read_REALLY_complete){|value,path|
129
+ values_found << value
130
+ paths_found << path
131
+ }
132
+ values_found.size.should eq(16)
133
+ paths_found.size.should eq(16)
134
+ end
135
+
136
+ end
@@ -0,0 +1,8 @@
1
+ require 'bundler/setup'
2
+ require 'rspec'
3
+ require 'hash_walker'
4
+ Dir[File.expand_path('../support/**/*', __FILE__)].each { |f| require f }
5
+
6
+
7
+ RSpec.configure do |config|
8
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash_walker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Lloyd Meta
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-29 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A simple gem that allows you to traverse/walk a Hash according to a set
15
+ of keys (also a hash) and return the primitive values at those keys.
16
+ email:
17
+ - lloydmeta@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - .rspec
24
+ - .travis.yml
25
+ - Gemfile
26
+ - README.md
27
+ - Rakefile
28
+ - hash_walker.gemspec
29
+ - lib/hash_walker.rb
30
+ - lib/hash_walker/core_extensions/hash.rb
31
+ - spec/hash_walker/hash_walker_spec.rb
32
+ - spec/spec_helper.rb
33
+ homepage: http://github.com/lloydmeta/hash_walker
34
+ licenses: []
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 1.8.24
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: A simple gem that allows you to traverse/walk a Hash according to a set of
57
+ keys (also a hash) and return the primitive values at those keys.
58
+ test_files:
59
+ - spec/hash_walker/hash_walker_spec.rb
60
+ - spec/spec_helper.rb