navigable_hash 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,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in navigable_hash.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jason Waldrip
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # NavigableHash
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'navigable_hash'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install navigable_hash
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,5 @@
1
+ require 'active_support/hash_with_indifferent_access'
2
+
3
+ class NavigableHash < HashWithIndifferentAccess
4
+ VERSION = "0.0.1"
5
+ end
@@ -0,0 +1,48 @@
1
+ require "navigable_hash/version"
2
+ require 'active_support/core_ext/hash'
3
+ require "active_support/hash_with_indifferent_access"
4
+
5
+ class NavigableHash < HashWithIndifferentAccess
6
+
7
+ def [](key)
8
+ navigate super(key)
9
+ end
10
+
11
+ def method_missing(m, *args, &block)
12
+ self[m]
13
+ end
14
+
15
+ def respond_to_missing?(m, include_private = false)
16
+ true
17
+ end
18
+
19
+ def ==(other_hash)
20
+ to_hash == self.class.new(other_hash).to_hash
21
+ end
22
+
23
+ private
24
+
25
+ def navigate value
26
+ case value
27
+ when Hash
28
+ navigate_hash value
29
+ when Array
30
+ navigate_array value
31
+ else
32
+ navigate_value value
33
+ end
34
+ end
35
+
36
+ def navigate_hash(value)
37
+ self.class.new value
38
+ end
39
+
40
+ def navigate_array(value)
41
+ value.map { |item| navigate item }
42
+ end
43
+
44
+ def navigate_value(value)
45
+ value
46
+ end
47
+
48
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'navigable_hash/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "navigable_hash"
8
+ spec.version = NavigableHash::VERSION
9
+ spec.authors = ["Jason Waldrip", "Kevin Wanek"]
10
+ spec.email = ["jason@waldrip.net", "k@dmcy.us"]
11
+ spec.description = %q{Allows a hash to be navigated with dot notation or indifferent access}
12
+ spec.summary = %q{Allows a hash to be navigated with dot notation or indifferent access}
13
+ spec.homepage = "http://github.com/jwaldrip/navigable_hash"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "pry"
27
+ spec.add_development_dependency "simplecov"
28
+
29
+ end
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+ require 'pry'
3
+
4
+ describe NavigableHash do
5
+
6
+ TEST_HASH = { symbol_key: 'symbol_key_value', 'string_key' => 'string_key_value', hash_item: { inner_value: true }, array: [ {}, 'string', :symbol ] , nil_value: nil, object: Object.new }
7
+
8
+ let(:hash){ TEST_HASH }
9
+ let(:navigable){ NavigableHash.new(hash) }
10
+
11
+ describe "#[]" do
12
+
13
+ context "given a hash" do
14
+ it "should return a new instance of navigable hash" do
15
+ navigable[:hash_item].should be_a NavigableHash
16
+ end
17
+ end
18
+
19
+ context "given an array" do
20
+ it "should call #navigate with each value" do
21
+ size = hash[:array].size
22
+ navigable.should_receive(:navigate_array).with(hash[:array])
23
+ navigable[:array]
24
+ end
25
+ end
26
+
27
+ context "given any object" do
28
+ it "should return a value" do
29
+ navigable.should_receive(:navigate_value).with(hash[:object]).any_number_of_times.and_call_original
30
+ navigable[:object].should == hash[:object]
31
+ end
32
+ end
33
+
34
+ context "given nil" do
35
+ it "should return a value" do
36
+ navigable.should_receive(:navigate_value).with(hash[:nil_value]).any_number_of_times.and_call_original
37
+ navigable[:nil_value].should == hash[:nil_value]
38
+ navigable[:nil_value].should be_nil
39
+ end
40
+ end
41
+
42
+ end
43
+
44
+ describe "#==" do
45
+ it "should be equal to a hash" do
46
+ navigable.should == hash
47
+ end
48
+ end
49
+
50
+ TEST_HASH.keys.each do |key_name|
51
+ describe "##{key_name}" do
52
+ it "should call navigate" do
53
+ navigable.should_receive(:navigate).any_number_of_times.and_call_original
54
+ navigable.send(key_name).should == TEST_HASH[key_name]
55
+ end
56
+ end
57
+ end
58
+
59
+ describe "#respond_to?" do
60
+ it "should always be true" do
61
+ navigable.respond_to?(:this_is_not_a_method).should be_true
62
+ end
63
+ end
64
+
65
+ describe "#method_missing" do
66
+ it "should return nil" do
67
+ navigable.this_is_not_a_method.should be_nil
68
+ end
69
+
70
+ it "should not raise an error" do
71
+ expect { navigable.this_is_not_a_method }.to_not raise_error
72
+ end
73
+ end
74
+
75
+ end
@@ -0,0 +1,21 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ require 'simplecov'
8
+ SimpleCov.start
9
+ require 'navigable_hash'
10
+
11
+ RSpec.configure do |config|
12
+ config.treat_symbols_as_metadata_keys_with_true_values = true
13
+ config.run_all_when_everything_filtered = true
14
+ config.filter_run :focus
15
+
16
+ # Run specs in random order to surface order dependencies. If you find an
17
+ # order dependency and want to debug it, you can fix the order by providing
18
+ # the seed, which is printed after each run.
19
+ # --seed 1234
20
+ config.order = 'random'
21
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: navigable_hash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jason Waldrip
9
+ - Kevin Wanek
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-03-28 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: bundler
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: '1.3'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: '1.3'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ - !ruby/object:Gem::Dependency
64
+ name: rspec
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ - !ruby/object:Gem::Dependency
80
+ name: pry
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ type: :development
88
+ prerelease: false
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ - !ruby/object:Gem::Dependency
96
+ name: simplecov
97
+ requirement: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Allows a hash to be navigated with dot notation or indifferent access
112
+ email:
113
+ - jason@waldrip.net
114
+ - k@dmcy.us
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - .gitignore
120
+ - .rspec
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - lib/navigable_hash.rb
126
+ - lib/navigable_hash/version.rb
127
+ - navigable_hash.gemspec
128
+ - spec/lib/navigable_hash_spec.rb
129
+ - spec/spec_helper.rb
130
+ homepage: http://github.com/jwaldrip/navigable_hash
131
+ licenses:
132
+ - MIT
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ! '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ none: false
145
+ requirements:
146
+ - - ! '>='
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 1.8.23
152
+ signing_key:
153
+ specification_version: 3
154
+ summary: Allows a hash to be navigated with dot notation or indifferent access
155
+ test_files:
156
+ - spec/lib/navigable_hash_spec.rb
157
+ - spec/spec_helper.rb