dive 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,2 +1,6 @@
1
1
  ## 0.0.1 (17/1/12)
2
- - Initial release
2
+ - Initial release
3
+
4
+ ## 0.0.2 (21/1/12)
5
+ - Ruby 1.8.7 compatibility
6
+ - Made recognition of dive locations stricter - fixed issues identified while testing with Rspec build
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? Well, it all started when I was mapping human readable names taken from a Cucumber table to fields in a JSON response:
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
- ### Installation and usage
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.
@@ -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{A gem for accessing values within nested Hashes}
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
 
@@ -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
- is_symbol = key.respond_to?(:to_sym) && key[0] == ':'
12
- is_symbol ? key[1..-1].to_sym : key
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
- matches = location.to_s.match /([^\[\]]*)\[(.*)\]/ #(key)[(remainder)]
37
- matches ? dive_to_next_level(matches[1].strip, matches[2].strip) : default_value(location)
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
- matches = location.to_s.match /([^\[\]]*)\[(.*)\]/
54
- matches ? store_at_next_level(matches[1], matches[2], value): old_store(symbolise(location), value)
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
@@ -1,3 +1,3 @@
1
1
  module Dive
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -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
- @hash['first'].default_proc = proc { |hash, key| "default proc: #{key}"}
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
- version: 0.0.1
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
- date: 2012-01-17 00:00:00.000000000Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
17
+
18
+ date: 2012-01-21 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
15
21
  name: rspec
16
- requirement: &2152401600 !ruby/object:Gem::Requirement
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
- version: '2.7'
27
+ - !ruby/object:Gem::Version
28
+ hash: 13
29
+ segments:
30
+ - 2
31
+ - 7
32
+ version: "2.7"
22
33
  type: :development
23
- prerelease: false
24
- version_requirements: *2152401600
25
- description: ! 'For example: {:sausages => {:pork_and_fennel => ''DELICIOUS''}}['':sausages[:pork_and_fennel]'']'
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
- files:
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
- require_paths:
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
- version: '0'
59
- required_rubygems_version: !ruby/object:Gem::Requirement
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
- version: '0'
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: A gem for accessing values within nested Hashes
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