dive 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +5 -1
- data/README.md +15 -3
- data/dive.gemspec +2 -2
- data/lib/dive/dive.rb +14 -6
- data/lib/dive/version.rb +1 -1
- data/spec/invasive/hash_spec.rb +7 -0
- data/spec/noninvasive/read_spec.rb +3 -1
- data/todo.txt +1 -7
- metadata +51 -29
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,9 @@
|
|
2
2
|
|
3
3
|
Dive is a gem for accessing values within nested Hashes.
|
4
4
|
|
5
|
-
Why?
|
5
|
+
## Why?
|
6
|
+
|
7
|
+
Well, it all started when I was mapping human readable names taken from a Cucumber table to fields in a JSON response:
|
6
8
|
|
7
9
|
```cucumber
|
8
10
|
Given I have some sausages with
|
@@ -76,7 +78,9 @@ So I did.
|
|
76
78
|
|
77
79
|
Check out the specs for how it behaves.
|
78
80
|
|
79
|
-
|
81
|
+
Dive has been tested with ruby 1.9.2 and 1.8.7.
|
82
|
+
|
83
|
+
## Installation and usage
|
80
84
|
|
81
85
|
```ruby
|
82
86
|
gem install dive
|
@@ -91,4 +95,12 @@ Or if you become squeamish at the idea of overriding Hash's [] method:
|
|
91
95
|
```ruby
|
92
96
|
require 'dive/noninvasive'
|
93
97
|
foods.dive ':sausages[:pork_and_fennel]'
|
94
|
-
```
|
98
|
+
```
|
99
|
+
|
100
|
+
## A Note on Dive Keys
|
101
|
+
|
102
|
+
Anything containing square brackets can be parsed as a Dive key, for example
|
103
|
+
```ruby
|
104
|
+
'first[second]'
|
105
|
+
```
|
106
|
+
If you are experiencing strange behaviour while using dive to override Hash methods, please check that none of your keys are unintentionally being recognised as such. A test case for the unusual ones would be much appreciated.
|
data/dive.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.authors = ["Brent Snook"]
|
9
9
|
s.email = ["brent@fuglylogic.com"]
|
10
10
|
s.homepage = "http://github.com/brentsnook/dive"
|
11
|
-
s.summary = %q{
|
12
|
-
s.description = %q{For example: {:sausages => {:pork_and_fennel => 'DELICIOUS'}}[':sausages[:pork_and_fennel]']}
|
11
|
+
s.summary = %q{For deep hash access. Read and write values within nested hashes.}
|
12
|
+
s.description = %q{For accessing values within nested Hashes. For example: {:sausages => {:pork_and_fennel => 'DELICIOUS'}}[':sausages[:pork_and_fennel]']}
|
13
13
|
|
14
14
|
s.rubyforge_project = "dive"
|
15
15
|
|
data/lib/dive/dive.rb
CHANGED
@@ -7,9 +7,17 @@ module Dive
|
|
7
7
|
clazz.send :include, Write
|
8
8
|
end
|
9
9
|
|
10
|
+
def as_symbol key
|
11
|
+
key_string = key.to_s
|
12
|
+
(key_string[0, 1] == ':') ? key_string[1..-1].to_sym : key
|
13
|
+
end
|
14
|
+
|
10
15
|
def symbolise key
|
11
|
-
|
12
|
-
|
16
|
+
key.is_a?(Symbol) ? key : as_symbol(key)
|
17
|
+
end
|
18
|
+
|
19
|
+
def split_dive location
|
20
|
+
location.to_s.match /^([^\[\]]+)\[(.+)\]\s*$/ #(key)[(remainder)]
|
13
21
|
end
|
14
22
|
|
15
23
|
module Read
|
@@ -33,8 +41,8 @@ module Dive
|
|
33
41
|
end
|
34
42
|
|
35
43
|
def attempt_dive location
|
36
|
-
|
37
|
-
|
44
|
+
split_location = split_dive location
|
45
|
+
split_location ? dive_to_next_level(split_location[1].strip, split_location[2].strip) : default_value(location)
|
38
46
|
end
|
39
47
|
|
40
48
|
def dive_to_next_level key, remainder
|
@@ -50,8 +58,8 @@ module Dive
|
|
50
58
|
end
|
51
59
|
|
52
60
|
def dive_store location, value
|
53
|
-
|
54
|
-
|
61
|
+
split_location = split_dive location
|
62
|
+
split_location ? store_at_next_level(split_location[1], split_location[2], value) : old_store(symbolise(location), value)
|
55
63
|
end
|
56
64
|
|
57
65
|
private
|
data/lib/dive/version.rb
CHANGED
data/spec/invasive/hash_spec.rb
CHANGED
@@ -21,4 +21,11 @@ describe Hash, 'when extended' do
|
|
21
21
|
hash = {:x => :y}
|
22
22
|
hash[nil].should == hash.default
|
23
23
|
end
|
24
|
+
|
25
|
+
# real example from rspec build that made everything explode
|
26
|
+
it "doesn't retrieve top level hash keyed with an empty string" do
|
27
|
+
# location of :[]= was resolving to first and second level key of ''
|
28
|
+
# rspec had actual values keyed against this so it exploded
|
29
|
+
{ "" => {"" =>'empty keyed value'}}[:[]=].should_not == 'empty keyed value'
|
30
|
+
end
|
24
31
|
end
|
@@ -49,7 +49,9 @@ describe Dive, 'when reading' do
|
|
49
49
|
describe 'when that hash was created with a default proc' do
|
50
50
|
|
51
51
|
before do
|
52
|
-
|
52
|
+
first = Hash.new { |hash, key| "default proc: #{key}"}
|
53
|
+
first['second'] = 'cantdive'
|
54
|
+
@hash['first'] = first
|
53
55
|
@result = @hash.dive('first[second[cantdive[fourth]]]')
|
54
56
|
end
|
55
57
|
|
data/todo.txt
CHANGED
@@ -1,11 +1,5 @@
|
|
1
|
-
|
2
|
-
- exploratory testing
|
3
|
-
- test with 1.8.7
|
4
|
-
- update change log
|
5
|
-
- publish to gem repo
|
6
|
-
|
7
1
|
-- LATER --
|
8
2
|
|
3
|
+
- add support for arrays
|
9
4
|
- add can_dive? method - checks if there is a key
|
10
5
|
- normal override overrides has_key? also
|
11
|
-
- add support for arrays
|
metadata
CHANGED
@@ -1,34 +1,47 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: dive
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- Brent Snook
|
9
14
|
autorequire:
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
17
|
+
|
18
|
+
date: 2012-01-21 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
15
21
|
name: rspec
|
16
|
-
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
17
24
|
none: false
|
18
|
-
requirements:
|
25
|
+
requirements:
|
19
26
|
- - ~>
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 13
|
29
|
+
segments:
|
30
|
+
- 2
|
31
|
+
- 7
|
32
|
+
version: "2.7"
|
22
33
|
type: :development
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
email:
|
34
|
+
version_requirements: *id001
|
35
|
+
description: "For accessing values within nested Hashes. For example: {:sausages => {:pork_and_fennel => 'DELICIOUS'}}[':sausages[:pork_and_fennel]']"
|
36
|
+
email:
|
27
37
|
- brent@fuglylogic.com
|
28
38
|
executables: []
|
39
|
+
|
29
40
|
extensions: []
|
41
|
+
|
30
42
|
extra_rdoc_files: []
|
31
|
-
|
43
|
+
|
44
|
+
files:
|
32
45
|
- .gitignore
|
33
46
|
- CHANGELOG.md
|
34
47
|
- Gemfile
|
@@ -46,29 +59,38 @@ files:
|
|
46
59
|
- todo.txt
|
47
60
|
homepage: http://github.com/brentsnook/dive
|
48
61
|
licenses: []
|
62
|
+
|
49
63
|
post_install_message:
|
50
64
|
rdoc_options: []
|
51
|
-
|
65
|
+
|
66
|
+
require_paths:
|
52
67
|
- lib
|
53
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
69
|
none: false
|
55
|
-
requirements:
|
56
|
-
- -
|
57
|
-
- !ruby/object:Gem::Version
|
58
|
-
|
59
|
-
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 3
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
78
|
none: false
|
61
|
-
requirements:
|
62
|
-
- -
|
63
|
-
- !ruby/object:Gem::Version
|
64
|
-
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
hash: 3
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
version: "0"
|
65
86
|
requirements: []
|
87
|
+
|
66
88
|
rubyforge_project: dive
|
67
89
|
rubygems_version: 1.8.10
|
68
90
|
signing_key:
|
69
91
|
specification_version: 3
|
70
|
-
summary:
|
71
|
-
test_files:
|
92
|
+
summary: For deep hash access. Read and write values within nested hashes.
|
93
|
+
test_files:
|
72
94
|
- spec/invasive/hash_spec.rb
|
73
95
|
- spec/noninvasive/read_spec.rb
|
74
96
|
- spec/noninvasive/spec_helper.rb
|