navigable_hash 0.0.1 → 1.0.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/.coveralls.yml +1 -0
- data/.gitignore +1 -0
- data/.travis.yml +15 -0
- data/README.md +41 -2
- data/Rakefile +4 -0
- data/lib/navigable_hash.rb +147 -11
- data/navigable_hash.gemspec +5 -7
- data/spec/helpers/helper_methods.rb +54 -0
- data/spec/lib/navigable_hash_spec.rb +201 -14
- data/spec/spec_helper.rb +8 -1
- metadata +24 -21
- data/lib/navigable_hash/version.rb +0 -5
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
repo_token: 0r24ERdqRyaWZ5GjVaqJXl2K9vnXsgTia
|
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,6 +1,11 @@
|
|
1
1
|
# NavigableHash
|
2
2
|
|
3
|
-
|
3
|
+
[](https://travis-ci.org/jwaldrip/navigable_hash)
|
4
|
+
[](https://coveralls.io/r/jwaldrip/navigable_hash)
|
5
|
+
[](https://codeclimate.com/github/jwaldrip/navigable_hash)
|
6
|
+
[](https://gemnasium.com/jwaldrip/navigable_hash)
|
7
|
+
|
8
|
+
NavigableHash was built as lightweight and quick way to navigate through a hash or array object using the familiar ruby dot notation. See 'Usage' below for examples. Keys as strings or symbols don't matter, its all included.
|
4
9
|
|
5
10
|
## Installation
|
6
11
|
|
@@ -18,7 +23,41 @@ Or install it yourself as:
|
|
18
23
|
|
19
24
|
## Usage
|
20
25
|
|
21
|
-
|
26
|
+
(Check out the rspec tests for coverage examples)
|
27
|
+
|
28
|
+
Most basic usage:
|
29
|
+
```ruby
|
30
|
+
test_hash = { :example_key => 'example value' }
|
31
|
+
navigable_hash = NavigableHash.new(test_hash)
|
32
|
+
|
33
|
+
navigable_hash.example_key
|
34
|
+
# => 'example_value'
|
35
|
+
```
|
36
|
+
|
37
|
+
|
38
|
+
More:
|
39
|
+
```ruby
|
40
|
+
new_hash = { :second_key => { :inner_key => true }, :array_item => [{}, "string", :symbol] }
|
41
|
+
navigable_hash = NavigableHash.new(new_hash)
|
42
|
+
|
43
|
+
navigable_hash.second_key.inner_key
|
44
|
+
# => true
|
45
|
+
|
46
|
+
navigable_hash.array_item.last
|
47
|
+
# => :symbol
|
48
|
+
```
|
49
|
+
|
50
|
+
|
51
|
+
Call Agnostic (key as a string, symbol or dot notation):
|
52
|
+
```ruby
|
53
|
+
new_hash = { "first_key" => "value 1" }
|
54
|
+
navigable_hash = NavigableHash.new(new_hash)
|
55
|
+
|
56
|
+
navigable_hash.first_key
|
57
|
+
navigable_hash[:first_key]
|
58
|
+
navigable_hash["first_key"]
|
59
|
+
# => "value 1"
|
60
|
+
```
|
22
61
|
|
23
62
|
## Contributing
|
24
63
|
|
data/Rakefile
CHANGED
data/lib/navigable_hash.rb
CHANGED
@@ -1,29 +1,152 @@
|
|
1
|
-
|
2
|
-
require 'active_support/core_ext/hash'
|
3
|
-
require "active_support/hash_with_indifferent_access"
|
1
|
+
class NavigableHash < Hash
|
4
2
|
|
5
|
-
|
3
|
+
def initialize(constructor = {})
|
4
|
+
if constructor.is_a?(Hash)
|
5
|
+
super()
|
6
|
+
update(constructor)
|
7
|
+
else
|
8
|
+
super(constructor)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
alias_method :get_value, :[] unless method_defined?(:get_value)
|
13
|
+
alias_method :set_value, :[]= unless method_defined?(:set_value)
|
14
|
+
|
15
|
+
def ==(other_hash)
|
16
|
+
to_hash == self.class.new(other_hash).to_hash
|
17
|
+
end
|
18
|
+
|
19
|
+
# Assigns a new value to the hash:
|
20
|
+
#
|
21
|
+
# hash = NavigableHash.new
|
22
|
+
# hash[:key] = "value"
|
23
|
+
#
|
24
|
+
def []=(key, value)
|
25
|
+
set_value convert_key(key), navigate(value)
|
26
|
+
end
|
27
|
+
|
28
|
+
alias :store :[]=
|
6
29
|
|
7
30
|
def [](key)
|
8
|
-
|
31
|
+
get_value convert_key(key)
|
9
32
|
end
|
10
33
|
|
11
|
-
|
12
|
-
|
34
|
+
# Removes a specified key from the hash.
|
35
|
+
def delete(key)
|
36
|
+
super(convert_key(key))
|
37
|
+
end
|
38
|
+
|
39
|
+
# Returns an exact copy of the hash.
|
40
|
+
def dup
|
41
|
+
self.class.new to_hash
|
42
|
+
end
|
43
|
+
|
44
|
+
# Same as <tt>Hash#fetch</tt> where the key passed as argument can be
|
45
|
+
# either a string or a symbol:
|
46
|
+
#
|
47
|
+
# counters = HashWithIndifferentAccess.new
|
48
|
+
# counters[:foo] = 1
|
49
|
+
#
|
50
|
+
# counters.fetch("foo") # => 1
|
51
|
+
# counters.fetch(:bar, 0) # => 0
|
52
|
+
# counters.fetch(:bar) {|key| 0} # => 0
|
53
|
+
# counters.fetch(:zoo) # => KeyError: key not found: "zoo"
|
54
|
+
#
|
55
|
+
def fetch(key, *extras)
|
56
|
+
super(convert_key(key), *extras)
|
13
57
|
end
|
14
58
|
|
15
|
-
|
59
|
+
# Checks the hash for a key matching the argument passed in:
|
60
|
+
#
|
61
|
+
# hash = HashWithIndifferentAccess.new
|
62
|
+
# hash["key"] = "value"
|
63
|
+
# hash.key? :key # => true
|
64
|
+
# hash.key? "key" # => true
|
65
|
+
#
|
66
|
+
def key?(key)
|
67
|
+
super(convert_key(key))
|
68
|
+
end
|
69
|
+
|
70
|
+
alias_method :include?, :key?
|
71
|
+
alias_method :has_key?, :key?
|
72
|
+
alias_method :member?, :key?
|
73
|
+
|
74
|
+
# Merges the instantiated and the specified hashes together, giving precedence to the values from the second hash.
|
75
|
+
# Does not overwrite the existing hash.
|
76
|
+
def merge(hash)
|
77
|
+
self.dup.update(hash)
|
78
|
+
end
|
79
|
+
|
80
|
+
def respond_to?(m, include_private = false)
|
16
81
|
true
|
17
82
|
end
|
18
83
|
|
19
|
-
def
|
20
|
-
|
84
|
+
def to_hash
|
85
|
+
reduce({}) do |hash, (key, value)|
|
86
|
+
hash.merge key => convert_for_to_hash(value)
|
87
|
+
end
|
21
88
|
end
|
22
89
|
|
23
|
-
|
90
|
+
# Updates the instantized hash with values from the second:
|
91
|
+
#
|
92
|
+
# hash_1 = NavigableHash.new
|
93
|
+
# hash_1[:key] = "value"
|
94
|
+
#
|
95
|
+
# hash_2 = NavigableHash.new
|
96
|
+
# hash_2[:key] = "New Value!"
|
97
|
+
#
|
98
|
+
# hash_1.update(hash_2) # => {"key"=>"New Value!"}
|
99
|
+
#
|
100
|
+
def update(other_hash)
|
101
|
+
other_hash.reduce(self) { |hash, (k, v)| hash[k] = navigate(v) ; hash }
|
102
|
+
end
|
103
|
+
|
104
|
+
alias_method :merge!, :update
|
105
|
+
|
106
|
+
# Returns an array of the values at the specified indices:
|
107
|
+
#
|
108
|
+
# hash = HashWithIndifferentAccess.new
|
109
|
+
# hash[:a] = "x"
|
110
|
+
# hash[:b] = "y"
|
111
|
+
# hash.values_at("a", "b") # => ["x", "y"]
|
112
|
+
#
|
113
|
+
def values_at(*indices)
|
114
|
+
indices.collect {|key| self[convert_key(key)]}
|
115
|
+
end
|
116
|
+
|
117
|
+
protected :get_value, :set_value
|
118
|
+
|
119
|
+
def convert_for_to_hash(value)
|
120
|
+
case value
|
121
|
+
when NavigableHash
|
122
|
+
convert_navigable_hash_for_to_hash value
|
123
|
+
when Array
|
124
|
+
convert_array_for_to_hash value
|
125
|
+
else
|
126
|
+
convert_value_for_to_hash value
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def convert_key(key)
|
131
|
+
key.kind_of?(Symbol) ? key.to_s : key
|
132
|
+
end
|
133
|
+
|
134
|
+
def convert_navigable_hash_for_to_hash(value)
|
135
|
+
value.to_hash
|
136
|
+
end
|
137
|
+
|
138
|
+
def convert_array_for_to_hash(value)
|
139
|
+
value.map { |item| convert_for_to_hash item }
|
140
|
+
end
|
141
|
+
|
142
|
+
def convert_value_for_to_hash(value)
|
143
|
+
value
|
144
|
+
end
|
24
145
|
|
25
146
|
def navigate value
|
26
147
|
case value
|
148
|
+
when NavigableHash
|
149
|
+
value
|
27
150
|
when Hash
|
28
151
|
navigate_hash value
|
29
152
|
when Array
|
@@ -45,4 +168,17 @@ class NavigableHash < HashWithIndifferentAccess
|
|
45
168
|
value
|
46
169
|
end
|
47
170
|
|
171
|
+
private
|
172
|
+
|
173
|
+
def method_missing(m, *args, &block)
|
174
|
+
m = m.to_s
|
175
|
+
if args.size == 1 && m.gsub!(/(.+)=$/, '\1')
|
176
|
+
self.send :[]=, m, args.first
|
177
|
+
elsif args.size == 0
|
178
|
+
self.send :[], m
|
179
|
+
else
|
180
|
+
raise ArgumentError, "wrong number of arguments(#{args.size} for 0)"
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
48
184
|
end
|
data/navigable_hash.gemspec
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'navigable_hash/version'
|
5
4
|
|
6
5
|
Gem::Specification.new do |spec|
|
7
6
|
spec.name = "navigable_hash"
|
8
|
-
spec.version =
|
7
|
+
spec.version = "1.0.0"
|
9
8
|
spec.authors = ["Jason Waldrip", "Kevin Wanek"]
|
10
9
|
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}
|
10
|
+
spec.description = %q{Allows a hash to be navigated with dot notation or indifferent access.}
|
11
|
+
spec.summary = %q{Allows a hash to be navigated with dot notation or indifferent access.}
|
13
12
|
spec.homepage = "http://github.com/jwaldrip/navigable_hash"
|
14
13
|
spec.license = "MIT"
|
15
14
|
|
@@ -18,12 +17,11 @@ Gem::Specification.new do |spec|
|
|
18
17
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
18
|
spec.require_paths = ["lib"]
|
20
19
|
|
21
|
-
spec.add_dependency "activesupport"
|
22
|
-
|
23
20
|
spec.add_development_dependency "bundler", "~> 1.3"
|
24
21
|
spec.add_development_dependency "rake"
|
25
22
|
spec.add_development_dependency "rspec"
|
26
23
|
spec.add_development_dependency "pry"
|
27
|
-
spec.add_development_dependency "
|
24
|
+
spec.add_development_dependency "coveralls"
|
25
|
+
spec.add_development_dependency "simplecov-multi"
|
28
26
|
|
29
27
|
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module NavigableHash::HelperMethods
|
2
|
+
|
3
|
+
def test_key_with_dot_notation(key, hash)
|
4
|
+
case (value = hash[key])
|
5
|
+
when Hash
|
6
|
+
value.keys.each { |key| test_key_with_dot_notation key, hash }
|
7
|
+
end
|
8
|
+
describe "##{key}" do
|
9
|
+
it "should call navigate" do
|
10
|
+
navigable.should_receive(:navigate).any_number_of_times
|
11
|
+
navigable.send(key)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should have a valid value" do
|
15
|
+
navigable.send(key).should == hash[key]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_key_with_symbol_notation(key, hash)
|
21
|
+
case (value = hash[key])
|
22
|
+
when Hash
|
23
|
+
value.keys.each { |key| test_key_with_dot_notation key, hash }
|
24
|
+
end
|
25
|
+
describe "##{key}" do
|
26
|
+
it "should call navigate" do
|
27
|
+
navigable.should_receive(:navigate).any_number_of_times
|
28
|
+
navigable[key.to_sym]
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should have a valid value" do
|
32
|
+
navigable[key.to_sym].should == hash[key]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_key_with_string_notation(key, hash)
|
38
|
+
case (value = hash[key])
|
39
|
+
when Hash
|
40
|
+
value.keys.each { |key| test_key_with_dot_notation key, hash }
|
41
|
+
end
|
42
|
+
describe "##{key}" do
|
43
|
+
it "should call navigate" do
|
44
|
+
navigable.should_receive(:navigate).any_number_of_times
|
45
|
+
navigable[key.to_s]
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should have a valid value" do
|
49
|
+
navigable[key.to_s].should == hash[key]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
@@ -1,57 +1,237 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'pry'
|
3
|
+
require 'helpers/helper_methods'
|
4
|
+
|
5
|
+
RSpec.configure do |c|
|
6
|
+
c.extend NavigableHash::HelperMethods
|
7
|
+
end
|
3
8
|
|
4
9
|
describe NavigableHash do
|
5
10
|
|
6
|
-
TEST_HASH = { symbol_key
|
11
|
+
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 }
|
12
|
+
|
13
|
+
describe ".new" do
|
14
|
+
context "given an array" do
|
15
|
+
it "should not raise an error" do
|
16
|
+
expect { NavigableHash.new([:a, 1]) }.to_not raise_error
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "given a hash" do
|
21
|
+
it "should not raise an error" do
|
22
|
+
expect { NavigableHash.new({:a => 1}) }.to_not raise_error
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe ".new(TEST_HASH)" do
|
28
|
+
context "with dot notation" do
|
29
|
+
TEST_HASH.keys.each do |key_name|
|
30
|
+
test_key_with_dot_notation(key_name, TEST_HASH)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context "with symbol notation" do
|
35
|
+
TEST_HASH.keys.each do |key_name|
|
36
|
+
test_key_with_symbol_notation(key_name, TEST_HASH)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "with string notation" do
|
41
|
+
TEST_HASH.keys.each do |key_name|
|
42
|
+
test_key_with_string_notation(key_name, TEST_HASH)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
7
46
|
|
8
47
|
let(:hash){ TEST_HASH }
|
9
48
|
let(:navigable){ NavigableHash.new(hash) }
|
10
49
|
|
50
|
+
context "with a deep setter" do
|
51
|
+
let(:hash){ {} }
|
52
|
+
it "should be able to set and get a deep value" do
|
53
|
+
value = "world"
|
54
|
+
navigable.foo = {}
|
55
|
+
navigable.foo.bar = {}
|
56
|
+
navigable.foo.bar.baz = value
|
57
|
+
navigable.foo.bar.baz.should == value
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "#__any_method__" do
|
62
|
+
it "should not raise an error" do
|
63
|
+
expect { navigable.__any_method__ }.to_not raise_error
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should get a value" do
|
67
|
+
navigable.__any_method__ = "foo"
|
68
|
+
navigable.__any_method__.should == "foo"
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should raise an error with arguments" do
|
72
|
+
expect { navigable.__any_method__ :foo }.to raise_error
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe "#__any_method__=" do
|
77
|
+
it "should not raise an error" do
|
78
|
+
expect { navigable.__any_method__ = 'value' }.to_not raise_error
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should set a value" do
|
82
|
+
expect { navigable.__any_method__ = 'value' }.to change { navigable.__any_method__ }
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should raise an error with more than one argument" do
|
86
|
+
expect { navigable.send :__any_method__, :foo, :bar }.to raise_error
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
11
90
|
describe "#[]" do
|
12
91
|
|
13
92
|
context "given a hash" do
|
14
93
|
it "should return a new instance of navigable hash" do
|
15
|
-
navigable[:hash_item].should
|
94
|
+
navigable[:hash_item].should be_an_instance_of NavigableHash
|
16
95
|
end
|
17
96
|
end
|
18
97
|
|
19
98
|
context "given an array" do
|
20
99
|
it "should call #navigate with each value" do
|
21
|
-
|
22
|
-
navigable.should_receive(:navigate_array).with(hash[:array])
|
23
|
-
navigable[:array]
|
100
|
+
navigable[:array].should be_a Array
|
24
101
|
end
|
25
102
|
end
|
26
103
|
|
27
104
|
context "given any object" do
|
28
105
|
it "should return a value" do
|
29
|
-
navigable
|
30
|
-
navigable[:object].should == hash[:object]
|
106
|
+
navigable[:object].should be_an_instance_of Object
|
31
107
|
end
|
32
108
|
end
|
33
109
|
|
34
110
|
context "given nil" do
|
35
111
|
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
112
|
navigable[:nil_value].should be_nil
|
39
113
|
end
|
40
114
|
end
|
41
115
|
|
42
116
|
end
|
43
117
|
|
118
|
+
describe "#[]=" do
|
119
|
+
it "should set a value" do
|
120
|
+
expect { navigable[:new_key] = 'value' }.to change { navigable[:new_key] }
|
121
|
+
end
|
122
|
+
|
123
|
+
context "given a hash" do
|
124
|
+
it "should return a new instance of navigable hash" do
|
125
|
+
hash = {:a => 1, :b => 2, :c => 3}
|
126
|
+
navigable.should_receive(:navigate_hash).with(hash)
|
127
|
+
navigable[:hash] = hash
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
context "given an array" do
|
132
|
+
it "should call #navigate with each value" do
|
133
|
+
array = [1, 2, 3]
|
134
|
+
navigable.should_receive(:navigate_array).with(array)
|
135
|
+
navigable[:array] = array
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
context "given any object" do
|
140
|
+
it "should return a value" do
|
141
|
+
object = Object.new
|
142
|
+
navigable.should_receive(:navigate_value).with(object)
|
143
|
+
navigable[:object] = object
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
context "given nil" do
|
148
|
+
it "should return a value" do
|
149
|
+
navigable.should_receive(:navigate_value).with(nil)
|
150
|
+
navigable[:nil_value] = nil
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
44
155
|
describe "#==" do
|
45
156
|
it "should be equal to a hash" do
|
46
157
|
navigable.should == hash
|
47
158
|
end
|
48
159
|
end
|
49
160
|
|
50
|
-
|
51
|
-
|
52
|
-
it "should
|
53
|
-
navigable.
|
54
|
-
|
161
|
+
describe "#delete" do
|
162
|
+
context "given a string as the key" do
|
163
|
+
it "should delete a value" do
|
164
|
+
expect { navigable.delete('hash_item') }.to change { navigable[:hash_item] }.to(nil)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
context "given a symbol as the key" do
|
169
|
+
it "should delete a value" do
|
170
|
+
expect { navigable.delete(:hash_item) }.to change { navigable[:hash_item] }.to(nil)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
|
176
|
+
describe "#dup" do
|
177
|
+
it "should return a copy of navigable hash" do
|
178
|
+
navigable.dup.should be_an_instance_of NavigableHash
|
179
|
+
end
|
180
|
+
|
181
|
+
it "should be equal to its original" do
|
182
|
+
navigable.dup.should == navigable
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
describe "#key?" do
|
187
|
+
context "if a key exists" do
|
188
|
+
it "should be true" do
|
189
|
+
navigable[:present_key] = 'value'
|
190
|
+
navigable.key?(:present_key).should be_true
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
context "if a key does not exist" do
|
195
|
+
it "should be false" do
|
196
|
+
navigable.key?(:no_key).should be_false
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
describe "#fetch" do
|
202
|
+
context "given a hash" do
|
203
|
+
let(:hash){ { :hash => {} } }
|
204
|
+
it "should return an instance of NavigableHash" do
|
205
|
+
navigable.fetch(:hash).should be_an_instance_of NavigableHash
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
describe "#merge" do
|
211
|
+
context "given a hash" do
|
212
|
+
it "should have a navigable hash" do
|
213
|
+
merged = navigable.merge({ :some_hash => {}})
|
214
|
+
merged[:some_hash].should be_an_instance_of NavigableHash
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
describe "#to_hash" do
|
220
|
+
it "should be an instance of Hash" do
|
221
|
+
navigable.to_hash.should be_an_instance_of Hash
|
222
|
+
end
|
223
|
+
|
224
|
+
it "inner hash should be an instance of hash" do
|
225
|
+
navigable[:hash_item].should be_an_instance_of NavigableHash
|
226
|
+
navigable.to_hash['hash_item'].should be_an_instance_of Hash
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
describe "#update" do
|
231
|
+
context "given a hash" do
|
232
|
+
it "should have a navigable hash" do
|
233
|
+
navigable.update({ :some_hash => {}})
|
234
|
+
navigable[:some_hash].should be_an_instance_of NavigableHash
|
55
235
|
end
|
56
236
|
end
|
57
237
|
end
|
@@ -62,6 +242,13 @@ describe NavigableHash do
|
|
62
242
|
end
|
63
243
|
end
|
64
244
|
|
245
|
+
describe "#values_at" do
|
246
|
+
let (:hash){ { :foo => "foo_value", :bar => "bar_value", :baz => "baz_value" } }
|
247
|
+
it "should have the correct values" do
|
248
|
+
navigable.values_at(:foo, :bar).should == [hash[:foo], hash[:bar]]
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
65
252
|
describe "#method_missing" do
|
66
253
|
it "should return nil" do
|
67
254
|
navigable.this_is_not_a_method.should be_nil
|
data/spec/spec_helper.rb
CHANGED
@@ -5,7 +5,14 @@
|
|
5
5
|
#
|
6
6
|
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
7
|
require 'simplecov'
|
8
|
+
require 'coveralls'
|
9
|
+
|
10
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
11
|
+
SimpleCov::Formatter::HTMLFormatter,
|
12
|
+
Coveralls::SimpleCov::Formatter
|
13
|
+
]
|
8
14
|
SimpleCov.start
|
15
|
+
|
9
16
|
require 'navigable_hash'
|
10
17
|
|
11
18
|
RSpec.configure do |config|
|
@@ -17,5 +24,5 @@ RSpec.configure do |config|
|
|
17
24
|
# order dependency and want to debug it, you can fix the order by providing
|
18
25
|
# the seed, which is printed after each run.
|
19
26
|
# --seed 1234
|
20
|
-
config.order = 'random'
|
27
|
+
# config.order = 'random'
|
21
28
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: navigable_hash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,42 +10,42 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-03-
|
13
|
+
date: 2013-03-29 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
|
-
name:
|
16
|
+
name: bundler
|
17
17
|
requirement: !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
|
-
- -
|
20
|
+
- - ~>
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '
|
23
|
-
type: :
|
22
|
+
version: '1.3'
|
23
|
+
type: :development
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
none: false
|
27
27
|
requirements:
|
28
|
-
- -
|
28
|
+
- - ~>
|
29
29
|
- !ruby/object:Gem::Version
|
30
|
-
version: '
|
30
|
+
version: '1.3'
|
31
31
|
- !ruby/object:Gem::Dependency
|
32
|
-
name:
|
32
|
+
name: rake
|
33
33
|
requirement: !ruby/object:Gem::Requirement
|
34
34
|
none: false
|
35
35
|
requirements:
|
36
|
-
- -
|
36
|
+
- - ! '>='
|
37
37
|
- !ruby/object:Gem::Version
|
38
|
-
version: '
|
38
|
+
version: '0'
|
39
39
|
type: :development
|
40
40
|
prerelease: false
|
41
41
|
version_requirements: !ruby/object:Gem::Requirement
|
42
42
|
none: false
|
43
43
|
requirements:
|
44
|
-
- -
|
44
|
+
- - ! '>='
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version: '
|
46
|
+
version: '0'
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
|
-
name:
|
48
|
+
name: rspec
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
@@ -61,7 +61,7 @@ dependencies:
|
|
61
61
|
- !ruby/object:Gem::Version
|
62
62
|
version: '0'
|
63
63
|
- !ruby/object:Gem::Dependency
|
64
|
-
name:
|
64
|
+
name: pry
|
65
65
|
requirement: !ruby/object:Gem::Requirement
|
66
66
|
none: false
|
67
67
|
requirements:
|
@@ -77,7 +77,7 @@ dependencies:
|
|
77
77
|
- !ruby/object:Gem::Version
|
78
78
|
version: '0'
|
79
79
|
- !ruby/object:Gem::Dependency
|
80
|
-
name:
|
80
|
+
name: coveralls
|
81
81
|
requirement: !ruby/object:Gem::Requirement
|
82
82
|
none: false
|
83
83
|
requirements:
|
@@ -93,7 +93,7 @@ dependencies:
|
|
93
93
|
- !ruby/object:Gem::Version
|
94
94
|
version: '0'
|
95
95
|
- !ruby/object:Gem::Dependency
|
96
|
-
name: simplecov
|
96
|
+
name: simplecov-multi
|
97
97
|
requirement: !ruby/object:Gem::Requirement
|
98
98
|
none: false
|
99
99
|
requirements:
|
@@ -108,7 +108,7 @@ dependencies:
|
|
108
108
|
- - ! '>='
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
|
-
description: Allows a hash to be navigated with dot notation or indifferent access
|
111
|
+
description: Allows a hash to be navigated with dot notation or indifferent access.
|
112
112
|
email:
|
113
113
|
- jason@waldrip.net
|
114
114
|
- k@dmcy.us
|
@@ -116,15 +116,17 @@ executables: []
|
|
116
116
|
extensions: []
|
117
117
|
extra_rdoc_files: []
|
118
118
|
files:
|
119
|
+
- .coveralls.yml
|
119
120
|
- .gitignore
|
120
121
|
- .rspec
|
122
|
+
- .travis.yml
|
121
123
|
- Gemfile
|
122
124
|
- LICENSE.txt
|
123
125
|
- README.md
|
124
126
|
- Rakefile
|
125
127
|
- lib/navigable_hash.rb
|
126
|
-
- lib/navigable_hash/version.rb
|
127
128
|
- navigable_hash.gemspec
|
129
|
+
- spec/helpers/helper_methods.rb
|
128
130
|
- spec/lib/navigable_hash_spec.rb
|
129
131
|
- spec/spec_helper.rb
|
130
132
|
homepage: http://github.com/jwaldrip/navigable_hash
|
@@ -148,10 +150,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
148
150
|
version: '0'
|
149
151
|
requirements: []
|
150
152
|
rubyforge_project:
|
151
|
-
rubygems_version: 1.8.
|
153
|
+
rubygems_version: 1.8.25
|
152
154
|
signing_key:
|
153
155
|
specification_version: 3
|
154
|
-
summary: Allows a hash to be navigated with dot notation or indifferent access
|
156
|
+
summary: Allows a hash to be navigated with dot notation or indifferent access.
|
155
157
|
test_files:
|
158
|
+
- spec/helpers/helper_methods.rb
|
156
159
|
- spec/lib/navigable_hash_spec.rb
|
157
160
|
- spec/spec_helper.rb
|