/
aux_code_spec.rb
405 lines (321 loc) · 14.3 KB
/
aux_code_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
require File.dirname(__FILE__) + '/spec_helper'
describe AuxCode do
it 'should require a name' do
AuxCode.create( :name => nil ).should_not be_valid
AuxCode.create( :name => ' ' ).should_not be_valid
AuxCode.create( :name => 'foo' ).should be_valid
end
it 'should have many aux codes (if category)' do
code = AuxCode.create! :name => 'foo'
code.codes.should be_empty
w00t = code.codes.create :name => 'w00t'
code.codes.should include(w00t)
end
it 'should have code names (if category)' do
code = AuxCode.create! :name => 'foo'
code.codes.create :name => 'chunky'
code.codes.create :name => 'bacon'
code.codes.length.should == 2
code.code_names.length.should == 2
code.code_names.should include('chunky')
code.code_names.should include('bacon')
end
it "should know whether it's a category or a category value / code" do
code = AuxCode.create! :name => 'foo'
w00t = code.codes.create :name => 'w00t'
w00t.is_a_category?.should be_false
code.is_a_category?.should be_true
end
it 'should belong to a category (if not a category)' do
code = AuxCode.create! :name => 'foo'
w00t = code.codes.create! :name => 'w00t'
w00t.aux_code.should == code
w00t.code.should == code
w00t.category.should == code
w00t.category.codes.should include(w00t)
w00t.codes.should be_empty
code.category.should be_nil
end
it 'should require a name unique to category' do
first_category = AuxCode.create! :name => 'foo'
second_category = AuxCode.create! :name => 'bar'
first_category.aux_codes.create( :name => 'w00t' ).should be_valid
first_category.aux_codes.create( :name => 'w00t' ).should_not be_valid
second_category.aux_codes.create( :name => 'w00t' ).should be_valid
end
it 'should be able to easily get all categories' do
AuxCode.categories.should be_empty
first_category = AuxCode.create! :name => 'foo'
second_category = AuxCode.create! :name => 'bar'
code_1 = first_category.codes.create :name => 'w00t'
code_2 = first_category.codes.create :name => 'w00t'
AuxCode.categories.should include(first_category)
AuxCode.categories.should include(second_category)
AuxCode.categories.should_not include(code_1)
AuxCode.categories.should_not include(code_2)
end
it 'should be able to easily get all category names' do
AuxCode.category_names.should be_empty
first_category = AuxCode.create! :name => 'foo'
second_category = AuxCode.create! :name => 'bar'
code_1 = first_category.codes.create :name => 'w00t'
AuxCode.category_names.should include('foo')
AuxCode.category_names.should include('bar')
AuxCode.category_names.should_not include('w00t')
end
it 'should be able to fetch a category give the category, its name, or its id' do
cat = AuxCode.create! :name => 'foo'
AuxCode.category( cat ).should == cat
AuxCode.category( cat.id ).should == cat
AuxCode.category( cat.name ).should == cat
AuxCode.category( cat.name.to_sym ).should == cat
end
it 'should be able to easily get all values (codes) for a category' do
first_category = AuxCode.create! :name => 'foo'
code_1 = first_category.codes.create :name => 'w00t'
code_2 = first_category.codes.create :name => 'chunky'
code_3 = first_category.codes.create :name => 'bacon'
[ AuxCode.category_values( first_category ), AuxCode.category_values( 'foo' ) ].each do |codes|
codes.should include(code_1)
codes.should include(code_2)
codes.should include(code_3)
codes.should_not include(first_category)
end
AuxCode.category_values( first_category ).should == AuxCode.category_codes( first_category )
end
it 'should be able to easily get all value names (code names) for a category' do
first_category = AuxCode.create! :name => 'foo'
code_1 = first_category.codes.create :name => 'w00t'
code_2 = first_category.codes.create :name => 'chunky'
code_3 = first_category.codes.create :name => 'bacon'
[ AuxCode.category_code_names( first_category ), AuxCode.category_code_names( 'foo' ) ].each do |codes|
codes.should include('w00t')
codes.should include('chunky')
codes.should include('bacon')
codes.should_not include('foo')
end
end
it 'should be able to get a Class for each category' do
random_category = AuxCode.create! :name => 'random-category-name'
foo_category = AuxCode.create! :name => 'foo'
foo_class = foo_category.aux_code_class
foo_class.count.should == 0
x = foo_category.codes.create :name => 'chunky'
foo_class.count.should == 1
foo_class.create :name => 'bacon'
foo_class.count.should == 2
foo = foo_class.create! :name => 'foo'
foo_class.count.should == 3
bar = foo_class.new :name => 'bar'
bar.save
foo_class.count.should == 4
w00t = foo_class.new :name => 'w00t'
w00t.save!
foo_class.count.should == 5
foo_class.find( :first, :conditions => ['name = ?', 'foo'] ).should == foo
foo_class.find( :first, :conditions => ['name = ?', 'foo'] ).should_not == foo_category
foo_class.find_by_name( 'foo' ).should == foo
foo_class.find_by_name( 'foo' ).should_not == foo_category
foo_class.find_all_by_name( 'foo' ).should include(foo)
foo_class.find_all_by_name( 'foo' ).should_not include(foo_category)
foo_class.all.should include(foo)
foo_class.all.should_not include(foo_category)
foo_class.find_by_name( 'neat' ).should be_nil
foo_class.find_or_create_by_name( 'neat' ).should_not be_nil
foo_class.find_by_name( 'neat' ).should_not be_nil
foo_class.code_names.should include('neat')
end
it 'should create good class names from category names for #create_classes!' do
AuxCode.new( :name => 'foos' ).class_name.should == 'Foo'
AuxCode.new( :name => 'foo Bars' ).class_name.should == 'FooBar'
AuxCode.new( :name => 'foo Bar lar Tars' ).class_name.should == 'FooBarLarTar'
AuxCode.new( :name => 'foo_Bar lar-Tars' ).class_name.should == 'FooBarLarTar'
AuxCode.new( :name => 'foo_Bar0lar1Tar5s' ).class_name.should == 'FooBarLarTar'
AuxCode.new( :name => 'Dog' ).class_name.should == 'Dog'
AuxCode.new( :name => 'Dogs' ).class_name.should == 'Dog'
end
it 'should be able to get categories and their values using Hash syntax' do
foo_category = AuxCode.create! :name => 'foo'
bar_category = AuxCode.create! :name => 'bar'
chunky = foo_category.codes.create :name => 'chunky'
bacon = foo_category.codes.create :name => 'bacon'
AuxCode['foo'].should == foo_category
AuxCode[:foo].should == foo_category
AuxCode['foo'][:chunky].should == chunky
AuxCode[:foo]['chunky'].should == chunky
end
it 'should be able to get categories and their values using indifferent Hash syntax' do
foo_category = AuxCode.create! :name => 'foo'
bar_category = AuxCode.create! :name => 'bar'
chunky = foo_category.codes.create :name => 'chunky'
bacon = foo_category.codes.create :name => 'bacon'
AuxCode.foo.should == foo_category
AuxCode.foo.chunky.should == chunky
end
it 'should be able to handle names with spaces (with Hash syntax)' do
foo_category = AuxCode.create! :name => 'foo'
bar_category = AuxCode.create! :name => 'bar'
chunky = foo_category.codes.create :name => 'I am Chunky'
bacon = foo_category.codes.create :name => 'Yay for Bacon'
foo_category.i_am_chunky.should == chunky
foo_category[:yay_for_bacon].should == bacon
end
it 'an aux_code_class should raise a NoMethodError, as per usual, if an undefined method is called' do
chunky = AuxCode.create!( :name => 'chunky' ).aux_code_class
lambda { chunky.new.bacon }.should raise_error(NoMethodError)
end
it 'should be able to pass a block to aux_code_class for quick and easy class customization' do
chunky = AuxCode.create!( :name => 'chunky' ).aux_code_class do
def bacon
"chunky bacon!"
end
end
lambda { chunky.new.bacon }.should_not raise_error(NoMethodError)
chunky.new.bacon.should == 'chunky bacon!'
end
it 'should be able to pass a block to aux_code_class for quick and easy eigenclass customization' do
chunky = AuxCode.create!( :name => 'chunky' ).aux_code_class do
def self.bacon
"chunky bacon!"
end
end
chunky.bacon.should == 'chunky bacon!'
lambda { chunky.new.bacon }.should raise_error(NoMethodError) # bacon is a class method
end
it 'should be able to define a meta attribute' do
breed = AuxCode.create!( :name => 'breed' ).aux_code_class do
attr_meta :acronym
end
apbt = breed.create :name => 'American Pit Bull Terrier', :acronym => 'APBT'
breed.find_by_name('American Pit Bull Terrier').should == apbt
breed.find_by_name('American Pit Bull Terrier').acronym.should == 'APBT'
end
it 'should be able to define multiple meta attributes' do
breed = AuxCode.create!( :name => 'breed' ).aux_code_class do
attr_meta :acronym, :foo
end
apbt = breed.create :name => 'American Pit Bull Terrier', :acronym => 'APBT', :foo => 'bar'
breed.find_by_name('American Pit Bull Terrier').should == apbt
breed.find_by_name('American Pit Bull Terrier').acronym.should == 'APBT'
breed.find_by_name('American Pit Bull Terrier').foo.should == 'bar'
end
it 'to_s should return the name of an aux_code' do
AuxCode.new( :name => 'i Am the Name' ).to_s.should == 'i Am the Name'
x = AuxCode.create :name => 'foo'
y = x.codes.create :name => 'bar'
x.to_s.should == 'foo'
y.to_s.should == 'bar'
end
it 'should be able to load AuxCodes from a Hash' do
AuxCode.count.should == 0
AuxCode.load({
# symbol => String Array
:days => %w( Monday Tuesday Wednesday ),
# symbol => Hash
:colors => {
# symbol => Hash options
:red => { :first_letter => 'r' },
:blue => { :first_letter => 'b' }
},
# symbol => Array of hashes
:foods => [
{ :name => 'Pizza', :taste => 'good' },
{ :name => 'Dirt', :taste => 'bad' }
],
'Snack Foods' => %w( Popcorn chips )
})
AuxCode.category_names.should include('days')
AuxCode.category_names.should include('colors')
AuxCode.category_names.should include('foods')
AuxCode[:days].codes.length.should == 3
AuxCode[:days].code_names.should include('Monday')
AuxCode[:days].monday.name.should == 'Monday'
AuxCode[:days][:monday].name.should == 'Monday'
AuxCode[:colors][:red].name.should == 'red'
AuxCode[:colors][:red].first_letter.should == 'r'
AuxCode[:colors].aux_code_class.red.name.should == 'red'
AuxCode[:colors].aux_code_class.red.first_letter.should == 'r'
AuxCode[:colors].aux_code_class[:red].first_letter.should == 'r'
AuxCode.foods.pizza.taste.should == 'good'
AuxCode.foods.dirt.taste.should == 'bad'
AuxCode['Snack Foods'].codes.length.should == 2
AuxCode.category('Snack Foods').codes.length.should == 2
AuxCode.snack_foods.codes.length.should == 2
AuxCode.snack_foods.code_names.should include('Popcorn')
AuxCode.snack_foods[:popcorn].name.should == 'Popcorn'
AuxCode[:snack_foods].popcorn.name.should == 'Popcorn'
AuxCode['Snack Foods'].popcorn.name.should == 'Popcorn'
end
it 'should be able to load AuxCodes from a Yaml string' do
require 'yaml'
AuxCode.count.should == 0
yaml = {
# symbol => String Array
:days => %w( Monday Tuesday Wednesday ),
# symbol => Hash
:colors => {
# symbol => Hash options
:red => { :first_letter => 'r' },
:blue => { :first_letter => 'b' }
},
# symbol => Array of hashes
:foods => [
{ :name => 'Pizza', :taste => 'good' },
{ :name => 'Dirt', :taste => 'bad' }
],
'Snack Foods' => %w( Popcorn chips )
}.to_yaml
AuxCode.load_yaml yaml
AuxCode.category_names.should include('days')
AuxCode[:days].code_names.should include('Monday')
AuxCode[:days][:monday].name.should == 'Monday'
AuxCode[:colors][:red].name.should == 'red'
AuxCode[:colors][:red].first_letter.should == 'r'
AuxCode[:colors].aux_code_class.red.first_letter.should == 'r'
AuxCode.foods.pizza.taste.should == 'good'
AuxCode.category('Snack Foods').codes.length.should == 2
AuxCode.snack_foods.code_names.should include('Popcorn')
AuxCode.snack_foods[:popcorn].name.should == 'Popcorn'
end
it 'should be able to load AuxCodes from a Yaml file' do
AuxCode.count.should == 0
AuxCode.load_file File.dirname(__FILE__) + '/example_aux_codes.yml'
AuxCode.category_names.should include('days')
AuxCode[:days].code_names.should include('Monday')
AuxCode[:days][:monday].name.should == 'Monday'
AuxCode[:colors][:red].name.should == 'red'
AuxCode[:colors][:red].first_letter.should == 'r'
AuxCode[:colors].aux_code_class.red.first_letter.should == 'r'
AuxCode.foods.pizza.taste.should == 'good'
AuxCode.category('Snack Foods').codes.length.should == 2
AuxCode.snack_foods.code_names.should include('Popcorn')
AuxCode.snack_foods[:popcorn].name.should == 'Popcorn'
end
it 'should be able to update meta attribute easily' do
AuxCode.load_file File.dirname(__FILE__) + '/more_example_aux_codes.yml'
AuxCode.foods.pizza.taste.should == 'good'
AuxCode.foods.pizza.taste = 'yummy'
AuxCode.foods.pizza.taste.should == 'yummy'
end
it 'should be able to load AuxCodes from a [slightly different] Yaml file' do
AuxCode.count.should == 0
AuxCode.load_file File.dirname(__FILE__) + '/more_example_aux_codes.yml'
AuxCode.category_names.should include('days')
AuxCode[:days].code_names.should include('Monday')
AuxCode[:days][:monday].name.should == 'Monday'
AuxCode[:colors][:red].name.should == 'red'
AuxCode[:colors][:red].first_letter.should == 'r'
AuxCode[:colors].aux_code_class.red.first_letter.should == 'r'
AuxCode.foods.pizza.taste.should == 'good'
AuxCode.category('Snack Foods').codes.length.should == 2
AuxCode.snack_foods.code_names.should include('Popcorn')
AuxCode.snack_foods[:popcorn].name.should == 'Popcorn'
# quick test - overridable ...
AuxCode.load({
:colors => {
:red => { :first_letter => 'CHANGED' }
}
})
AuxCode[:colors][:red].first_letter.should == 'CHANGED'
end
end