more_core_extensions 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -36,7 +36,10 @@ module MoreCoreExtensions
36
36
  self[key] = value
37
37
  else
38
38
  child = self[key]
39
- child = self[key] = {} unless child.kind_of?(Hash)
39
+ unless child.kind_of?(Hash)
40
+ self[key] = self.class.new
41
+ child = self[key]
42
+ end
40
43
  child.store_path(args[1..-1], value)
41
44
  end
42
45
  end
@@ -1,3 +1,3 @@
1
1
  module MoreCoreExtensions
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2"
3
3
  end
@@ -1,50 +1,63 @@
1
1
  require_relative "../../spec_helper"
2
2
 
3
- describe Hash do
4
- let(:hash) do
5
- {
6
- "a" => 1,
7
- "b" => {},
8
- "c" => {"c1" => 2},
9
- "d" => {"d1" => {"d2" => {"d3" => 3}}},
10
- "e" => Hash.new(4),
11
- "f" => Hash.new { |h, k| Hash.new }
12
- }
3
+ shared_examples_for "core_ext/hash/nested will not modify arguments" do |meth|
4
+ it "will not modify arguments" do
5
+ args = (meth == :store_path ? [1] : [])
6
+
7
+ key = ["d", "d1", "d2", "d3"]
8
+ key2 = key.dup
9
+ hash.send(meth, key2, *args)
10
+ key2.should == key
11
+
12
+ key = ["e", "e1", "e2"]
13
+ key2 = key.dup
14
+ hash.send(meth, key2, *args)
15
+ key2.should == key
13
16
  end
17
+ end
14
18
 
15
- it '#fetch_path' do
16
- hash.fetch_path("a").should == 1
17
- hash.fetch_path("b").should == {}
18
- hash.fetch_path("b", "b1").should be_nil
19
- hash.fetch_path("b", "b1", "b2").should be_nil
20
- hash.fetch_path("c").should == {"c1" => 2}
21
- hash.fetch_path("c", "c1").should == 2
22
- hash.fetch_path("c", "c1", "c2").should be_nil
23
- hash.fetch_path("d", "d1", "d2", "d3").should == 3
24
- hash.fetch_path("d", "d1", "d2", "dx").should be_nil
25
- hash.fetch_path("d", "d1", "d2", "d3", "d4").should be_nil
26
- hash.fetch_path("e").should == {}
27
- hash.fetch_path("e", "e1").should == 4
28
- hash.fetch_path("e", "e1", "e2").should be_nil
29
- hash.fetch_path("f").should == {}
30
- hash.fetch_path("f", "f1").should == {}
31
- hash.fetch_path("f", "f1", "f2").should be_nil
32
-
33
- hash.fetch_path(nil).should be_nil
34
- hash.fetch_path("d", nil, "d1").should be_nil
35
- hash.fetch_path("e", nil).should == 4
36
- hash.fetch_path("e", nil, "e1").should be_nil
37
-
38
- lambda { hash.fetch_path }.should raise_error(ArgumentError)
19
+ shared_examples_for "core_ext/hash/nested" do
20
+ context '#fetch_path' do
21
+ it "with various values" do
22
+ hash.fetch_path("a").should == 1
23
+ hash.fetch_path("b").should == {}
24
+ hash.fetch_path("b", "b1").should be_nil
25
+ hash.fetch_path("b", "b1", "b2").should be_nil
26
+ hash.fetch_path("c").should == {"c1" => 2}
27
+ hash.fetch_path("c", "c1").should == 2
28
+ hash.fetch_path("c", "c1", "c2").should be_nil
29
+ hash.fetch_path("d", "d1", "d2", "d3").should == 3
30
+ hash.fetch_path("d", "d1", "d2", "dx").should be_nil
31
+ hash.fetch_path("d", "d1", "d2", "d3", "d4").should be_nil
32
+ hash.fetch_path("e").should == {}
33
+ hash.fetch_path("e", "e1").should == 4
34
+ hash.fetch_path("e", "e1", "e2").should be_nil
35
+ hash.fetch_path("f").should == {}
36
+ hash.fetch_path("f", "f1").should == {}
37
+ hash.fetch_path("f", "f1", "f2").should be_nil
38
+ end
39
+
40
+ it "with a nil value" do
41
+ hash.fetch_path(nil).should be_nil
42
+ hash.fetch_path("d", nil, "d1").should be_nil
43
+ hash.fetch_path("e", nil).should == 4
44
+ hash.fetch_path("e", nil, "e1").should be_nil
45
+ end
46
+
47
+ it "with invalid values" do
48
+ lambda { hash.fetch_path }.should raise_error(ArgumentError)
49
+ end
50
+
51
+ include_examples "core_ext/hash/nested will not modify arguments", :fetch_path
39
52
  end
40
53
 
41
54
  context "#store_path" do
42
55
  it "on an empty hash" do
43
- h = {}
56
+ h = described_class.new
44
57
  h.store_path("a", 1)
45
58
  h.should == {"a" => 1}
46
59
 
47
- h = {}
60
+ h = described_class.new
48
61
  h.store_path("b", "b1", 2)
49
62
  h.should == {"b" => {"b1" => 2}}
50
63
  end
@@ -64,62 +77,121 @@ describe Hash do
64
77
  end
65
78
 
66
79
  it "with an array of keys" do
67
- h = {}
80
+ h = described_class.new
68
81
  h.store_path(["d", "d1", "d2", "d3"], 3)
69
82
  h.should == {"d" => {"d1" => {"d2" => {"d3" => 3}}}}
70
83
  end
71
84
 
72
85
  it "with a nil value" do
73
- h = {}
86
+ h = described_class.new
74
87
  h.store_path("a", "b", nil)
75
88
  h.should == {"a" => {"b" => nil}}
76
89
  end
77
90
 
91
+ it "with an Array value" do
92
+ h = described_class.new
93
+ h.store_path("a", "b", ["c", "d"])
94
+ h.should == {"a" => {"b" => ["c", "d"]}}
95
+ end
96
+
97
+ it "with a Hash value" do
98
+ h = described_class.new
99
+ h.store_path("a", "b", {"c" => "d"})
100
+ h.should == {"a" => {"b" => {"c" => "d"}}}
101
+ end
102
+
78
103
  it "with invalid values" do
79
- lambda { {}.store_path }.should raise_error(ArgumentError)
80
- lambda { {}.store_path(nil) }.should raise_error(ArgumentError)
104
+ lambda { described_class.new.store_path }.should raise_error(ArgumentError)
105
+ lambda { described_class.new.store_path(1) }.should raise_error(ArgumentError)
81
106
  end
82
- end
83
107
 
84
- it '#has_key_path?' do
85
- hash.has_key_path?("a").should be_true
86
- hash.has_key_path?("b").should be_true
87
- hash.has_key_path?("b", "b1").should be_false
88
- hash.has_key_path?("b", "b1", "b2").should be_false
89
- hash.has_key_path?("c").should be_true
90
- hash.has_key_path?("c", "c1").should be_true
91
- hash.has_key_path?("c", "c1", "c2").should be_false
92
- hash.has_key_path?("d", "d1", "d2", "d3").should be_true
93
- hash.has_key_path?("d", "d1", "d2", "dx").should be_false
94
- hash.has_key_path?("d", "d1", "d2", "d3", "d4").should be_false
95
- hash.has_key_path?("e").should be_true
96
- hash.has_key_path?("e", "e1").should be_false
97
- hash.has_key_path?("e", "e1", "e2").should be_false
98
- hash.has_key_path?("f").should be_true
99
- hash.has_key_path?("f", "f1").should be_false
100
- hash.has_key_path?("f", "f1", "f2").should be_false
101
-
102
- hash.has_key_path?(nil).should be_false
103
- hash.has_key_path?("d", nil, "d1").should be_false
104
- hash.has_key_path?("e", nil).should be_false
105
- hash.has_key_path?("e", nil, "e1").should be_false
106
-
107
- lambda { hash.has_key_path? }.should raise_error(ArgumentError)
108
+ include_examples "core_ext/hash/nested will not modify arguments", :store_path
108
109
  end
109
110
 
110
- [:fetch_path, :has_key_path?, :store_path, :delete_path].each do |meth|
111
- it "##{meth} will not modify arguments" do
112
- args = (meth == :store_path ? [1] : [])
111
+ context '#has_key_path?' do
112
+ it "with various values" do
113
+ hash.has_key_path?("a").should be_true
114
+ hash.has_key_path?("b").should be_true
115
+ hash.has_key_path?("b", "b1").should be_false
116
+ hash.has_key_path?("b", "b1", "b2").should be_false
117
+ hash.has_key_path?("c").should be_true
118
+ hash.has_key_path?("c", "c1").should be_true
119
+ hash.has_key_path?("c", "c1", "c2").should be_false
120
+ hash.has_key_path?("d", "d1", "d2", "d3").should be_true
121
+ hash.has_key_path?("d", "d1", "d2", "dx").should be_false
122
+ hash.has_key_path?("d", "d1", "d2", "d3", "d4").should be_false
123
+ hash.has_key_path?("e").should be_true
124
+ hash.has_key_path?("e", "e1").should be_false
125
+ hash.has_key_path?("e", "e1", "e2").should be_false
126
+ hash.has_key_path?("f").should be_true
127
+ hash.has_key_path?("f", "f1").should be_false
128
+ hash.has_key_path?("f", "f1", "f2").should be_false
129
+ end
113
130
 
114
- key = ["d", "d1", "d2", "d3"]
115
- key2 = key.dup
116
- hash.send(meth, key2, *args)
117
- key2.should == key
131
+ it "with a nil value" do
132
+ hash.has_key_path?(nil).should be_false
133
+ hash.has_key_path?("d", nil, "d1").should be_false
134
+ hash.has_key_path?("e", nil).should be_false
135
+ hash.has_key_path?("e", nil, "e1").should be_false
136
+ end
118
137
 
119
- key = ["e", "e1", "e2"]
120
- key2 = key.dup
121
- hash.send(meth, key2, *args)
122
- key2.should == key
138
+ it "with invalid values" do
139
+ lambda { hash.has_key_path? }.should raise_error(ArgumentError)
123
140
  end
141
+
142
+ include_examples "core_ext/hash/nested will not modify arguments", :has_key_path?
143
+ end
144
+
145
+ context "#delete_path" do
146
+ include_examples "core_ext/hash/nested will not modify arguments", :delete_path
147
+ end
148
+ end
149
+
150
+ describe Hash do
151
+ let(:hash) do
152
+ {
153
+ "a" => 1,
154
+ "b" => {},
155
+ "c" => {"c1" => 2},
156
+ "d" => {"d1" => {"d2" => {"d3" => 3}}},
157
+ "e" => Hash.new(4),
158
+ "f" => Hash.new { |h, k| h[k] = Hash.new }
159
+ }
124
160
  end
161
+
162
+ include_examples "core_ext/hash/nested"
163
+ end
164
+
165
+ require 'active_support/core_ext/hash'
166
+ describe HashWithIndifferentAccess do
167
+ let(:hash) do
168
+ described_class.new.merge(
169
+ "a" => 1,
170
+ "b" => {},
171
+ "c" => {"c1" => 2},
172
+ "d" => {"d1" => {"d2" => {"d3" => 3}}},
173
+ "e" => Hash.new(4),
174
+ "f" => described_class.new { |h, k| h[k] = described_class.new }
175
+ )
176
+
177
+ # NOTE: "f" has to be initialized in that way due to a bug in
178
+ # HashWithIndifferentAccess and assigning a Hash with a default proc.
179
+ #
180
+ # 1.9.3 :001 > h1 = Hash.new
181
+ # 1.9.3 :002 > h1[:a] = Hash.new { |h, k| h[k] = Hash.new }
182
+ # 1.9.3 :003 > h1[:a].class
183
+ # => Hash
184
+ # 1.9.3 :004 > h1[:a][:b].class
185
+ # => Hash
186
+ #
187
+ # 1.9.3 :005 > require 'active_support/all'
188
+ # 1.9.3 :006 > h2 = HashWithIndifferentAccess.new
189
+ # 1.9.3 :007 > h2[:a] = Hash.new { |h, k| h[k] = Hash.new }
190
+ # 1.9.3 :008 > h2[:a].class
191
+ # => ActiveSupport::HashWithIndifferentAccess
192
+ # 1.9.3 :009 > h2[:a][:b].class
193
+ # => NilClass
194
+ end
195
+
196
+ include_examples "core_ext/hash/nested"
125
197
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: more_core_extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-13 00:00:00.000000000 Z
12
+ date: 2013-09-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -146,7 +146,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
146
146
  version: '0'
147
147
  segments:
148
148
  - 0
149
- hash: -2540507193161773470
149
+ hash: 1712580559107520706
150
150
  required_rubygems_version: !ruby/object:Gem::Requirement
151
151
  none: false
152
152
  requirements:
@@ -155,7 +155,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
155
155
  version: '0'
156
156
  segments:
157
157
  - 0
158
- hash: -2540507193161773470
158
+ hash: 1712580559107520706
159
159
  requirements: []
160
160
  rubyforge_project:
161
161
  rubygems_version: 1.8.25