hashmodel 0.1.1 → 0.2.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/Gemfile +5 -10
- data/Gemfile.lock +13 -14
- data/README.markdown +11 -2
- data/Rakefile +3 -8
- data/_brainstorm/hash_test.rb +169 -0
- data/_brainstorm/instance_vars.rb +24 -0
- data/_brainstorm/ref_val.rb +16 -0
- data/_brainstorm/spliting.rb +46 -0
- data/_brainstorm/test.rb +27 -0
- data/features/README +1 -1
- data/features/hash_model_flatten.feature +5 -5
- data/features/hash_model_search.feature +2 -2
- data/features/step_definitions/hash_model_steps.rb +2 -2
- data/lib/hash_model/exceptions.rb +1 -1
- data/lib/hash_model/hash_model.rb +455 -363
- data/lib/hash_model/version.rb +9 -11
- data/spec/hash_model/hash_model_spec.rb +216 -119
- metadata +14 -18
data/lib/hash_model/version.rb
CHANGED
@@ -1,14 +1,12 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
PRE = nil
|
1
|
+
class HashModel
|
2
|
+
module VERSION # :nodoc:
|
3
|
+
MAJOR = 0
|
4
|
+
MINOR = 2
|
5
|
+
TINY = 0
|
6
|
+
PRE = nil
|
8
7
|
|
9
|
-
|
8
|
+
STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
|
10
9
|
|
11
|
-
|
12
|
-
end
|
10
|
+
SUMMARY = "HashModel #{STRING}"
|
13
11
|
end
|
14
|
-
end
|
12
|
+
end
|
@@ -1,7 +1,5 @@
|
|
1
1
|
require_relative '../spec_helper'
|
2
2
|
|
3
|
-
module MikBe
|
4
|
-
|
5
3
|
# RSpec's change just does not work with arrays in tests because of how Ruby changes the data arrays point to
|
6
4
|
# So we have to manually check before and after
|
7
5
|
|
@@ -23,31 +21,31 @@ describe "HashModel" do
|
|
23
21
|
@hm2 = HashModel.new(:raw_data=>@records2)
|
24
22
|
|
25
23
|
@flat_records = [
|
26
|
-
{:switch=>"-x", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :
|
27
|
-
{:switch=>"--xtended", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :
|
28
|
-
{:switch=>"-y", :description=>"lucky what?", :
|
29
|
-
{:switch=>"-z", :parameter=>{:type=>String}, :description=>"zee svitch zu moost calz", :
|
24
|
+
{:switch=>"-x", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :_id=>0, :_group_id=>0},
|
25
|
+
{:switch=>"--xtended", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :_id=>1, :_group_id=>0},
|
26
|
+
{:switch=>"-y", :description=>"lucky what?", :_id=>2, :_group_id=>1}, {:switch=>"--why", :description=>"lucky what?", :_id=>3, :_group_id=>1},
|
27
|
+
{:switch=>"-z", :parameter=>{:type=>String}, :description=>"zee svitch zu moost calz", :_id=>4, :_group_id=>2}
|
30
28
|
]
|
31
29
|
|
32
30
|
@flat_records2 =[
|
33
|
-
{:switch=>"-p", :parameter=>{:type=>Hash, :require=>false}, :description=>"Pea soup", :
|
34
|
-
{:switch=>"--pea", :parameter=>{:type=>Hash, :require=>false}, :description=>"Pea soup", :
|
35
|
-
{:switch=>"-q", :description=>"exit the game", :
|
36
|
-
{:switch=>"--quit", :description=>"exit the game", :
|
37
|
-
{:switch=>"-r", :parameter=>{:type=>Fixnum}, :description=>"Arrrrrrrrrrgh!", :
|
31
|
+
{:switch=>"-p", :parameter=>{:type=>Hash, :require=>false}, :description=>"Pea soup", :_id=>0, :_group_id=>0},
|
32
|
+
{:switch=>"--pea", :parameter=>{:type=>Hash, :require=>false}, :description=>"Pea soup", :_id=>1, :_group_id=>0},
|
33
|
+
{:switch=>"-q", :description=>"exit the game", :_id=>2, :_group_id=>1},
|
34
|
+
{:switch=>"--quit", :description=>"exit the game", :_id=>3, :_group_id=>1},
|
35
|
+
{:switch=>"-r", :parameter=>{:type=>Fixnum}, :description=>"Arrrrrrrrrrgh!", :_id=>4, :_group_id=>2}
|
38
36
|
]
|
39
37
|
|
40
38
|
@flat_records_all = [
|
41
|
-
{:switch=>"-x", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :
|
42
|
-
{:switch=>"--xtended", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :
|
43
|
-
{:switch=>"-y", :description=>"lucky what?", :
|
44
|
-
{:switch=>"--why", :description=>"lucky what?", :
|
45
|
-
{:switch=>"-z", :parameter=>{:type=>String}, :description=>"zee svitch zu moost calz", :
|
46
|
-
{:switch=>"-p", :parameter=>{:type=>Hash, :require=>false}, :description=>"Pea soup", :
|
47
|
-
{:switch=>"--pea", :parameter=>{:type=>Hash, :require=>false}, :description=>"Pea soup", :
|
48
|
-
{:switch=>"-q", :description=>"exit the game", :
|
49
|
-
{:switch=>"--quit", :description=>"exit the game", :
|
50
|
-
{:switch=>"-r", :parameter=>{:type=>Fixnum}, :description=>"Arrrrrrrrrrgh!", :
|
39
|
+
{:switch=>"-x", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :_id=>0, :_group_id=>0},
|
40
|
+
{:switch=>"--xtended", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :_id=>1, :_group_id=>0},
|
41
|
+
{:switch=>"-y", :description=>"lucky what?", :_id=>2, :_group_id=>1},
|
42
|
+
{:switch=>"--why", :description=>"lucky what?", :_id=>3, :_group_id=>1},
|
43
|
+
{:switch=>"-z", :parameter=>{:type=>String}, :description=>"zee svitch zu moost calz", :_id=>4, :_group_id=>2},
|
44
|
+
{:switch=>"-p", :parameter=>{:type=>Hash, :require=>false}, :description=>"Pea soup", :_id=>5, :_group_id=>3},
|
45
|
+
{:switch=>"--pea", :parameter=>{:type=>Hash, :require=>false}, :description=>"Pea soup", :_id=>6, :_group_id=>3},
|
46
|
+
{:switch=>"-q", :description=>"exit the game", :_id=>7, :_group_id=>4},
|
47
|
+
{:switch=>"--quit", :description=>"exit the game", :_id=>8, :_group_id=>4},
|
48
|
+
{:switch=>"-r", :parameter=>{:type=>Fixnum}, :description=>"Arrrrrrrrrrgh!", :_id=>9, :_group_id=>5}
|
51
49
|
]
|
52
50
|
|
53
51
|
end
|
@@ -94,6 +92,12 @@ describe "HashModel" do
|
|
94
92
|
proc { @hm << {:switch => ["-x", "--xtended"], :description => "Xish stuff"} }.should_not raise_error
|
95
93
|
end
|
96
94
|
|
95
|
+
it "should allow a single hash to be added with :raw_data" do
|
96
|
+
hash = {:switch => ["-x", "--xtended"], :description => "Xish stuff"}
|
97
|
+
@hm = HashModel.new(:raw_data => hash)
|
98
|
+
@hm.raw_data.should == [{:switch=>["-x", "--xtended"], :description=>"Xish stuff"}]
|
99
|
+
end
|
100
|
+
|
97
101
|
it "should allow a hash of values to be added using the keyword 'add'" do
|
98
102
|
proc { @hm.add(:switch => ["-x", "--xtended"], :description => "Xish stuff") }.should_not raise_error
|
99
103
|
end
|
@@ -109,46 +113,33 @@ describe "HashModel" do
|
|
109
113
|
it "should allow an array of HashModels to be added" do
|
110
114
|
@hm.add([@hm, @hm2])
|
111
115
|
@hm.should == [
|
112
|
-
{:switch=>"-x", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :
|
113
|
-
{:switch=>"--xtended", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :
|
114
|
-
{:switch=>"-y", :description=>"lucky what?", :
|
115
|
-
{:switch=>"--why", :description=>"lucky what?", :
|
116
|
-
{:switch=>"-z", :parameter=>{:type=>String}, :description=>"zee svitch zu moost calz", :
|
117
|
-
{:switch=>"-x", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :
|
118
|
-
{:switch=>"--xtended", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :
|
119
|
-
{:switch=>"-y", :description=>"lucky what?", :
|
120
|
-
{:switch=>"--why", :description=>"lucky what?", :
|
121
|
-
{:switch=>"-z", :parameter=>{:type=>String}, :description=>"zee svitch zu moost calz", :
|
122
|
-
{:switch=>"-p", :parameter=>{:type=>Hash, :require=>false}, :description=>"Pea soup", :
|
123
|
-
{:switch=>"--pea", :parameter=>{:type=>Hash, :require=>false}, :description=>"Pea soup", :
|
124
|
-
{:switch=>"-q", :description=>"exit the game", :
|
125
|
-
{:switch=>"--quit", :description=>"exit the game", :
|
126
|
-
{:switch=>"-r", :parameter=>{:type=>Fixnum}, :description=>"Arrrrrrrrrrgh!", :
|
116
|
+
{:switch=>"-x", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :_id=>0, :_group_id=>0},
|
117
|
+
{:switch=>"--xtended", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :_id=>1, :_group_id=>0},
|
118
|
+
{:switch=>"-y", :description=>"lucky what?", :_id=>2, :_group_id=>1},
|
119
|
+
{:switch=>"--why", :description=>"lucky what?", :_id=>3, :_group_id=>1},
|
120
|
+
{:switch=>"-z", :parameter=>{:type=>String}, :description=>"zee svitch zu moost calz", :_id=>4, :_group_id=>2},
|
121
|
+
{:switch=>"-x", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :_id=>5, :_group_id=>3},
|
122
|
+
{:switch=>"--xtended", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :_id=>6, :_group_id=>3},
|
123
|
+
{:switch=>"-y", :description=>"lucky what?", :_id=>7, :_group_id=>4},
|
124
|
+
{:switch=>"--why", :description=>"lucky what?", :_id=>8, :_group_id=>4},
|
125
|
+
{:switch=>"-z", :parameter=>{:type=>String}, :description=>"zee svitch zu moost calz", :_id=>9, :_group_id=>5},
|
126
|
+
{:switch=>"-p", :parameter=>{:type=>Hash, :require=>false}, :description=>"Pea soup", :_id=>10, :_group_id=>6},
|
127
|
+
{:switch=>"--pea", :parameter=>{:type=>Hash, :require=>false}, :description=>"Pea soup", :_id=>11, :_group_id=>6},
|
128
|
+
{:switch=>"-q", :description=>"exit the game", :_id=>12, :_group_id=>7},
|
129
|
+
{:switch=>"--quit", :description=>"exit the game", :_id=>13, :_group_id=>7},
|
130
|
+
{:switch=>"-r", :parameter=>{:type=>Fixnum}, :description=>"Arrrrrrrrrrgh!", :_id=>14, :_group_id=>8}
|
127
131
|
]
|
128
132
|
end
|
129
133
|
|
134
|
+
it "should allow field names that are longer versions of other names" do
|
135
|
+
@hm = HashModel.new
|
136
|
+
@hm << {:long => "short", :longer => "long"}
|
137
|
+
@hm.should == [{:long => "short", :longer => "long"}]
|
138
|
+
end
|
139
|
+
|
130
140
|
it "should raise an error if something other than a hash, an array of hashes, or another HashModel (or an array of them) is added" do
|
131
141
|
proc { @hm << ["-x", "--xtended", "Xish stuff"] }.should raise_error
|
132
142
|
end
|
133
|
-
|
134
|
-
context "reserved field names" do
|
135
|
-
|
136
|
-
it "should raise an error if a protected field name is used" do
|
137
|
-
proc { @hm << {:hm_id => 1} }.should raise_error(MikBe::ReservedNameError)
|
138
|
-
proc { @hm << {:hm_group_id => 1} }.should raise_error(MikBe::ReservedNameError)
|
139
|
-
end
|
140
|
-
|
141
|
-
it "should raise an error if a reserved field name is used deep within the raw data" do
|
142
|
-
proc { @hm.raw_data = [{:switch => "--potato", :should_error=>[:hm_group_id => 1, :another => 2] }] }
|
143
|
-
.should raise_error(MikBe::ReservedNameError)
|
144
|
-
end
|
145
|
-
|
146
|
-
it "should raise an error if a reserved field name is used deep within the raw data" do
|
147
|
-
proc { @hm = HashModel.new(:raw_data=>[{:switch => "--potato", :should_error=>[:hm_group_id => 1, :another => 2] }] ) }
|
148
|
-
.should raise_error(MikBe::ReservedNameError)
|
149
|
-
end
|
150
|
-
|
151
|
-
end
|
152
143
|
|
153
144
|
it "should allow an array of hashes to be specified when creating the HashModel" do
|
154
145
|
proc { HashModel.new(:raw_data=>@records) }.should_not raise_error
|
@@ -175,6 +166,65 @@ describe "HashModel" do
|
|
175
166
|
@hm.raw_data.should == @records
|
176
167
|
end
|
177
168
|
|
169
|
+
context "flattened records" do
|
170
|
+
|
171
|
+
it "should allow a flattened record to be added" do
|
172
|
+
@hm = HashModel.new
|
173
|
+
@hm << {:switch=>"-x", :parameter__type=>String, :parameter__require=>true, :description=>"Xish stuff"}
|
174
|
+
@hm.raw_data.should == [{:switch => "-x", :parameter => {:type => String, :require => true}, :description => "Xish stuff"}]
|
175
|
+
end
|
176
|
+
|
177
|
+
it "should allow a flattened record to be added even with arrays in it" do
|
178
|
+
@hm = HashModel.new
|
179
|
+
@hm << {:switch=>["-x", "--xtend"],
|
180
|
+
:parameter__type=>String,
|
181
|
+
:parameter__require=>true,
|
182
|
+
:description=>"Xish stuff",
|
183
|
+
:field__field2 => {:field3 => "ff3", :field4 => "ff4"}
|
184
|
+
}
|
185
|
+
@hm.raw_data.should == [
|
186
|
+
{
|
187
|
+
:switch => ["-x", "--xtend"],
|
188
|
+
:parameter => {:type => String, :require => true},
|
189
|
+
:description => "Xish stuff",
|
190
|
+
:field => {:field2 => {:field3 => "ff3", :field4 => "ff4"}}
|
191
|
+
}
|
192
|
+
]
|
193
|
+
end
|
194
|
+
|
195
|
+
it "should allow deeply flattened record to be added" do
|
196
|
+
deep_hash = {
|
197
|
+
:parameter__type=>String,
|
198
|
+
:switch__deep1__deep3 => "deepTwo",
|
199
|
+
:parameter__type__ruby=>true,
|
200
|
+
:parameter => "glorp",
|
201
|
+
:parameter__require=>true,
|
202
|
+
:switch__deep2 => "deepTwo",
|
203
|
+
:description=>"Xish stuff",
|
204
|
+
:switch => "--xtend",
|
205
|
+
}
|
206
|
+
@hm = HashModel.new
|
207
|
+
@hm << deep_hash
|
208
|
+
@hm.raw_data.should == [
|
209
|
+
{
|
210
|
+
:parameter => [
|
211
|
+
{:type=>String},
|
212
|
+
"glorp",
|
213
|
+
{:require=>true}
|
214
|
+
],
|
215
|
+
:switch => [
|
216
|
+
{:deep1 => {:deep3=>"deepTwo"}},
|
217
|
+
{:deep2=>"deepTwo"},
|
218
|
+
"--xtend"
|
219
|
+
],
|
220
|
+
:description=>"Xish stuff"
|
221
|
+
}
|
222
|
+
]
|
223
|
+
end
|
224
|
+
|
225
|
+
|
226
|
+
end
|
227
|
+
|
178
228
|
context "using the + sign" do
|
179
229
|
|
180
230
|
it "should return a HashModel class when adding an Array" do
|
@@ -264,16 +314,16 @@ describe "HashModel" do
|
|
264
314
|
it "should change current raw to twice its old raw data if *= 2'd" do
|
265
315
|
@hm *= 2
|
266
316
|
@hm.should == [
|
267
|
-
{:switch=>"-x", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :
|
268
|
-
{:switch=>"--xtended", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :
|
269
|
-
{:switch=>"-y", :description=>"lucky what?", :
|
270
|
-
{:switch=>"--why", :description=>"lucky what?", :
|
271
|
-
{:switch=>"-z", :parameter=>{:type=>String}, :description=>"zee svitch zu moost calz", :
|
272
|
-
{:switch=>"-x", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :
|
273
|
-
{:switch=>"--xtended", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :
|
274
|
-
{:switch=>"-y", :description=>"lucky what?", :
|
275
|
-
{:switch=>"--why", :description=>"lucky what?", :
|
276
|
-
{:switch=>"-z", :parameter=>{:type=>String}, :description=>"zee svitch zu moost calz", :
|
317
|
+
{:switch=>"-x", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :_id=>0, :_group_id=>0},
|
318
|
+
{:switch=>"--xtended", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :_id=>1, :_group_id=>0},
|
319
|
+
{:switch=>"-y", :description=>"lucky what?", :_id=>2, :_group_id=>1},
|
320
|
+
{:switch=>"--why", :description=>"lucky what?", :_id=>3, :_group_id=>1},
|
321
|
+
{:switch=>"-z", :parameter=>{:type=>String}, :description=>"zee svitch zu moost calz", :_id=>4, :_group_id=>2},
|
322
|
+
{:switch=>"-x", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :_id=>5, :_group_id=>3},
|
323
|
+
{:switch=>"--xtended", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :_id=>6, :_group_id=>3},
|
324
|
+
{:switch=>"-y", :description=>"lucky what?", :_id=>7, :_group_id=>4},
|
325
|
+
{:switch=>"--why", :description=>"lucky what?", :_id=>8, :_group_id=>4},
|
326
|
+
{:switch=>"-z", :parameter=>{:type=>String}, :description=>"zee svitch zu moost calz", :_id=>9, :_group_id=>5}
|
277
327
|
]
|
278
328
|
end
|
279
329
|
|
@@ -283,13 +333,13 @@ describe "HashModel" do
|
|
283
333
|
|
284
334
|
it "should concatinate using a single Hash" do
|
285
335
|
@hm.concat(@records2[0]).should == [
|
286
|
-
{:switch=>"-x", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :
|
287
|
-
{:switch=>"--xtended", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :
|
288
|
-
{:switch=>"-y", :description=>"lucky what?", :
|
289
|
-
{:switch=>"--why", :description=>"lucky what?", :
|
290
|
-
{:switch=>"-z", :parameter=>{:type=>String}, :description=>"zee svitch zu moost calz", :
|
291
|
-
{:switch=>"-p", :parameter=>{:type=>Hash, :require=>false}, :description=>"Pea soup", :
|
292
|
-
{:switch=>"--pea", :parameter=>{:type=>Hash, :require=>false}, :description=>"Pea soup", :
|
336
|
+
{:switch=>"-x", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :_id=>0, :_group_id=>0},
|
337
|
+
{:switch=>"--xtended", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :_id=>1, :_group_id=>0},
|
338
|
+
{:switch=>"-y", :description=>"lucky what?", :_id=>2, :_group_id=>1},
|
339
|
+
{:switch=>"--why", :description=>"lucky what?", :_id=>3, :_group_id=>1},
|
340
|
+
{:switch=>"-z", :parameter=>{:type=>String}, :description=>"zee svitch zu moost calz", :_id=>4, :_group_id=>2},
|
341
|
+
{:switch=>"-p", :parameter=>{:type=>Hash, :require=>false}, :description=>"Pea soup", :_id=>5, :_group_id=>3},
|
342
|
+
{:switch=>"--pea", :parameter=>{:type=>Hash, :require=>false}, :description=>"Pea soup", :_id=>6, :_group_id=>3}
|
293
343
|
]
|
294
344
|
end
|
295
345
|
|
@@ -307,13 +357,13 @@ describe "HashModel" do
|
|
307
357
|
|
308
358
|
it "should add a single Hash" do
|
309
359
|
@hm.push(@records2[0]).should == [
|
310
|
-
{:switch=>"-x", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :
|
311
|
-
{:switch=>"--xtended", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :
|
312
|
-
{:switch=>"-y", :description=>"lucky what?", :
|
313
|
-
{:switch=>"--why", :description=>"lucky what?", :
|
314
|
-
{:switch=>"-z", :parameter=>{:type=>String}, :description=>"zee svitch zu moost calz", :
|
315
|
-
{:switch=>"-p", :parameter=>{:type=>Hash, :require=>false}, :description=>"Pea soup", :
|
316
|
-
{:switch=>"--pea", :parameter=>{:type=>Hash, :require=>false}, :description=>"Pea soup", :
|
360
|
+
{:switch=>"-x", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :_id=>0, :_group_id=>0},
|
361
|
+
{:switch=>"--xtended", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :_id=>1, :_group_id=>0},
|
362
|
+
{:switch=>"-y", :description=>"lucky what?", :_id=>2, :_group_id=>1},
|
363
|
+
{:switch=>"--why", :description=>"lucky what?", :_id=>3, :_group_id=>1},
|
364
|
+
{:switch=>"-z", :parameter=>{:type=>String}, :description=>"zee svitch zu moost calz", :_id=>4, :_group_id=>2},
|
365
|
+
{:switch=>"-p", :parameter=>{:type=>Hash, :require=>false}, :description=>"Pea soup", :_id=>5, :_group_id=>3},
|
366
|
+
{:switch=>"--pea", :parameter=>{:type=>Hash, :require=>false}, :description=>"Pea soup", :_id=>6, :_group_id=>3}
|
317
367
|
]
|
318
368
|
end
|
319
369
|
|
@@ -328,7 +378,26 @@ describe "HashModel" do
|
|
328
378
|
end
|
329
379
|
|
330
380
|
end # adding records
|
331
|
-
|
381
|
+
|
382
|
+
describe "reserved field names" do
|
383
|
+
|
384
|
+
it "should raise an error if a protected field name is used" do
|
385
|
+
proc { @hm << {:_id => 1} }.should raise_error(HashModel::ReservedNameError)
|
386
|
+
proc { @hm << {:_group_id => 1} }.should raise_error(HashModel::ReservedNameError)
|
387
|
+
end
|
388
|
+
|
389
|
+
it "should raise an error if a reserved field name is used deep within the raw data" do
|
390
|
+
proc { @hm.raw_data = [{:switch => "--potato", :should_error=>[:_group_id => 1, :another => 2] }] }
|
391
|
+
.should raise_error(HashModel::ReservedNameError)
|
392
|
+
end
|
393
|
+
|
394
|
+
it "should raise an error if a reserved field name is used deep within the raw data" do
|
395
|
+
proc { @hm = HashModel.new(:raw_data=>[{:switch => "--potato", :should_error=>[:_group_id => 1, :another => 2] }] ) }
|
396
|
+
.should raise_error(HashModel::ReservedNameError)
|
397
|
+
end
|
398
|
+
|
399
|
+
end
|
400
|
+
|
332
401
|
describe "using the [] sign" do
|
333
402
|
|
334
403
|
it "should return flat records" do
|
@@ -350,10 +419,10 @@ describe "HashModel" do
|
|
350
419
|
it "should set the flatten index properly if specified using parameter :flatten_index" do
|
351
420
|
@hm = HashModel.new(:raw_data=>@records, :flatten_index=>:parameter)
|
352
421
|
@hm.should == [
|
353
|
-
{:parameter=>{:type=>String}, :switch=>["-x", "--xtended"], :description=>"Xish stuff", :
|
354
|
-
{:parameter=>{:require=>true}, :switch=>["-x", "--xtended"], :description=>"Xish stuff", :
|
355
|
-
{:parameter=>nil, :switch=>["-y", "--why"], :description=>"lucky what?", :
|
356
|
-
{:parameter=>{:type=>String}, :switch=>"-z", :description=>"zee svitch zu moost calz", :
|
422
|
+
{:parameter=>{:type=>String}, :switch=>["-x", "--xtended"], :description=>"Xish stuff", :_id=>0, :_group_id=>0},
|
423
|
+
{:parameter=>{:require=>true}, :switch=>["-x", "--xtended"], :description=>"Xish stuff", :_id=>1, :_group_id=>0},
|
424
|
+
{:parameter=>nil, :switch=>["-y", "--why"], :description=>"lucky what?", :_id=>2, :_group_id=>1},
|
425
|
+
{:parameter=>{:type=>String}, :switch=>"-z", :description=>"zee svitch zu moost calz", :_id=>3, :_group_id=>2}
|
357
426
|
]
|
358
427
|
|
359
428
|
end
|
@@ -385,16 +454,16 @@ describe "HashModel" do
|
|
385
454
|
@hm = HashModel.new
|
386
455
|
@hm << @records[0]
|
387
456
|
@hm << {:foo=>"bar"}
|
388
|
-
@hm.last.should == {:switch=>nil, :foo=>"bar", :
|
457
|
+
@hm.last.should == {:switch=>nil, :foo=>"bar", :_id=>2, :_group_id=>1}
|
389
458
|
end
|
390
459
|
|
391
460
|
it "should change the flattened data when changing the flatten index" do
|
392
461
|
@hm = HashModel.new(:raw_data=>@records)
|
393
|
-
@hm.flatten_index = :
|
462
|
+
@hm.flatten_index = :parameter__type
|
394
463
|
@hm.should == [
|
395
|
-
{:
|
396
|
-
{:
|
397
|
-
{:
|
464
|
+
{:parameter__type=>String, :switch=>["-x", "--xtended"], :parameter__require=>true, :description=>"Xish stuff", :_id=>0, :_group_id=>0},
|
465
|
+
{:parameter__type=>nil, :switch=>["-y", "--why"], :description=>"lucky what?", :_id=>1, :_group_id=>1},
|
466
|
+
{:parameter__type=>String, :switch=>"-z", :description=>"zee svitch zu moost calz", :_id=>2, :_group_id=>2}
|
398
467
|
]
|
399
468
|
end
|
400
469
|
|
@@ -405,6 +474,36 @@ describe "HashModel" do
|
|
405
474
|
|
406
475
|
end # flattening behvior
|
407
476
|
|
477
|
+
describe "unflattening behavior" do
|
478
|
+
|
479
|
+
it "should allow access to the unflatten without an instance of HashModel" do
|
480
|
+
deep_hash = {
|
481
|
+
:parameter__type=>String,
|
482
|
+
:switch__deep1__deep3 => "deepTwo",
|
483
|
+
:parameter__type__ruby=>true,
|
484
|
+
:parameter => "glorp",
|
485
|
+
:parameter__require=>true,
|
486
|
+
:switch__deep2 => "deepTwo",
|
487
|
+
:description=>"Xish stuff",
|
488
|
+
:switch => "--xtend",
|
489
|
+
}
|
490
|
+
HashModel.unflatten(deep_hash).should == {
|
491
|
+
:parameter => [
|
492
|
+
{:type=>String},
|
493
|
+
"glorp",
|
494
|
+
{:require=>true}
|
495
|
+
],
|
496
|
+
:switch => [
|
497
|
+
{:deep1 => {:deep3=>"deepTwo"}},
|
498
|
+
{:deep2=>"deepTwo"},
|
499
|
+
"--xtend"
|
500
|
+
],
|
501
|
+
:description=>"Xish stuff"
|
502
|
+
}
|
503
|
+
end
|
504
|
+
|
505
|
+
end
|
506
|
+
|
408
507
|
describe "comparisons" do
|
409
508
|
|
410
509
|
it "should allow arrays to be compared to the HashModel" do
|
@@ -524,10 +623,10 @@ describe "HashModel" do
|
|
524
623
|
|
525
624
|
it "should search the flatten index if given a block" do
|
526
625
|
@hm = HashModel.new(:raw_data=>@records)
|
527
|
-
@hm.where{@
|
528
|
-
{:switch=>"-x", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :
|
529
|
-
{:switch=>"--xtended", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :
|
530
|
-
{:switch=>"-z", :parameter=>{:type=>String}, :description=>"zee svitch zu moost calz", :
|
626
|
+
@hm.where{@parameter__type == String}.should == [
|
627
|
+
{:switch=>"-x", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :_id=>0, :_group_id=>0},
|
628
|
+
{:switch=>"--xtended", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :_id=>1, :_group_id=>0},
|
629
|
+
{:switch=>"-z", :parameter=>{:type=>String}, :description=>"zee svitch zu moost calz", :_id=>4, :_group_id=>2}
|
531
630
|
]
|
532
631
|
end
|
533
632
|
|
@@ -537,11 +636,11 @@ describe "HashModel" do
|
|
537
636
|
@hm.include?([:switch=>"-x"]).should == false
|
538
637
|
end
|
539
638
|
|
540
|
-
it "should match flat data if search criteria includes an
|
639
|
+
it "should match flat data if search criteria includes an _group_id field" do
|
541
640
|
@hm.include?(@flat_records[2]).should == true
|
542
641
|
end
|
543
642
|
|
544
|
-
it "should search raw data if search criteria includes an
|
643
|
+
it "should search raw data if search criteria includes an _group_id field" do
|
545
644
|
@hm.include?(@records[2]).should == true
|
546
645
|
end
|
547
646
|
|
@@ -555,7 +654,7 @@ describe "HashModel" do
|
|
555
654
|
end
|
556
655
|
|
557
656
|
it "should return the first n flat records while block is true" do
|
558
|
-
@hm.take_while {|record| record[:
|
657
|
+
@hm.take_while {|record| record[:_id] < 4}.should == @flat_records[0..3]
|
559
658
|
end
|
560
659
|
|
561
660
|
end
|
@@ -596,10 +695,10 @@ describe "HashModel" do
|
|
596
695
|
end
|
597
696
|
|
598
697
|
it "should work across group_id's if searching for something that returns records from multiple groups" do
|
599
|
-
@hm.group{@
|
600
|
-
{:switch=>"-x", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :
|
601
|
-
{:switch=>"--xtended", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :
|
602
|
-
{:switch => "-z", :parameter => {:type => String}, :description => "zee svitch zu moost calz", :
|
698
|
+
@hm.group{@parameter__type == String}.should == [
|
699
|
+
{:switch=>"-x", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :_id=>0, :_group_id=>0},
|
700
|
+
{:switch=>"--xtended", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :_id=>1, :_group_id=>0},
|
701
|
+
{:switch => "-z", :parameter => {:type => String}, :description => "zee svitch zu moost calz", :_id=>4, :_group_id=>2}
|
603
702
|
]
|
604
703
|
end
|
605
704
|
|
@@ -625,16 +724,16 @@ describe "HashModel" do
|
|
625
724
|
|
626
725
|
it "should return the records in the same raw data record when using a block" do
|
627
726
|
@hm.group!{@switch == "-y"}.should == [
|
628
|
-
{:switch=>"-y", :description=>"lucky what?", :
|
629
|
-
{:switch=>"--why", :description=>"lucky what?", :
|
727
|
+
{:switch=>"-y", :description=>"lucky what?", :_id=>2, :_group_id=>1},
|
728
|
+
{:switch=>"--why", :description=>"lucky what?", :_id=>3, :_group_id=>1}
|
630
729
|
]
|
631
730
|
end
|
632
731
|
|
633
732
|
it "should work across group_id's if searching for something that returns records from multiple groups" do
|
634
|
-
@hm.group!{@
|
635
|
-
{:switch=>"-x", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :
|
636
|
-
{:switch=>"--xtended", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :
|
637
|
-
{:switch => "-z", :parameter => {:type => String}, :description => "zee svitch zu moost calz", :
|
733
|
+
@hm.group!{@parameter__type == String}.should == [
|
734
|
+
{:switch=>"-x", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :_id=>0, :_group_id=>0},
|
735
|
+
{:switch=>"--xtended", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :_id=>1, :_group_id=>0},
|
736
|
+
{:switch => "-z", :parameter => {:type => String}, :description => "zee svitch zu moost calz", :_id=>4, :_group_id=>2}
|
638
737
|
]
|
639
738
|
end
|
640
739
|
|
@@ -684,22 +783,22 @@ describe "HashModel" do
|
|
684
783
|
it "should collect across the flat data" do
|
685
784
|
extra = -1
|
686
785
|
@hm.collect {|record| record.merge!(:extra=>extra+=1)}.should == [
|
687
|
-
{:switch=>"-x", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :
|
688
|
-
{:switch=>"--xtended", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :
|
689
|
-
{:switch=>"-y", :description=>"lucky what?", :
|
690
|
-
{:switch=>"--why", :description=>"lucky what?", :
|
691
|
-
{:switch=>"-z", :parameter=>{:type=>String}, :description=>"zee svitch zu moost calz", :
|
786
|
+
{:switch=>"-x", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :_id=>0, :_group_id=>0, :extra=>0},
|
787
|
+
{:switch=>"--xtended", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :_id=>1, :_group_id=>0, :extra=>1},
|
788
|
+
{:switch=>"-y", :description=>"lucky what?", :_id=>2, :_group_id=>1, :extra=>2},
|
789
|
+
{:switch=>"--why", :description=>"lucky what?", :_id=>3, :_group_id=>1, :extra=>3},
|
790
|
+
{:switch=>"-z", :parameter=>{:type=>String}, :description=>"zee svitch zu moost calz", :_id=>4, :_group_id=>2, :extra=>4}
|
692
791
|
]
|
693
792
|
end
|
694
793
|
|
695
794
|
it "should map across the flat data" do
|
696
795
|
extra = -1
|
697
796
|
@hm.map {|record| record.merge!(:extra=>extra+=1)}.should == [
|
698
|
-
{:switch=>"-x", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :
|
699
|
-
{:switch=>"--xtended", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :
|
700
|
-
{:switch=>"-y", :description=>"lucky what?", :
|
701
|
-
{:switch=>"--why", :description=>"lucky what?", :
|
702
|
-
{:switch=>"-z", :parameter=>{:type=>String}, :description=>"zee svitch zu moost calz", :
|
797
|
+
{:switch=>"-x", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :_id=>0, :_group_id=>0, :extra=>0},
|
798
|
+
{:switch=>"--xtended", :parameter=>{:type=>String, :require=>true}, :description=>"Xish stuff", :_id=>1, :_group_id=>0, :extra=>1},
|
799
|
+
{:switch=>"-y", :description=>"lucky what?", :_id=>2, :_group_id=>1, :extra=>2},
|
800
|
+
{:switch=>"--why", :description=>"lucky what?", :_id=>3, :_group_id=>1, :extra=>3},
|
801
|
+
{:switch=>"-z", :parameter=>{:type=>String}, :description=>"zee svitch zu moost calz", :_id=>4, :_group_id=>2, :extra=>4}
|
703
802
|
]
|
704
803
|
end
|
705
804
|
|
@@ -768,7 +867,5 @@ describe "HashModel" do
|
|
768
867
|
end
|
769
868
|
|
770
869
|
end
|
771
|
-
|
772
|
-
end # describe "HashModel"
|
773
870
|
|
774
|
-
end #
|
871
|
+
end # describe "HashModel"
|