olfactory 0.0.1 → 0.1.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/.rspec +2 -0
- data/README.md +432 -11
- data/lib/olfactory/template.rb +200 -42
- data/lib/olfactory/template_definition.rb +93 -28
- data/lib/olfactory/version.rb +1 -1
- data/lib/olfactory.rb +5 -0
- data/olfactory.gemspec +4 -4
- data/spec/olfactory/template_spec.rb +1080 -0
- data/spec/spec_helper.rb +8 -3
- data/spec/support/saveable.rb +14 -0
- metadata +21 -16
@@ -0,0 +1,1080 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Olfactory::Template do
|
4
|
+
before do
|
5
|
+
Olfactory.reload
|
6
|
+
end
|
7
|
+
context "has_one" do
|
8
|
+
before do
|
9
|
+
Olfactory.template :widget do |t|
|
10
|
+
t.has_one :doodad
|
11
|
+
end
|
12
|
+
end
|
13
|
+
let(:value) { "doodad" }
|
14
|
+
|
15
|
+
context "given a basic value" do
|
16
|
+
subject do
|
17
|
+
Olfactory.build_template :widget do |t|
|
18
|
+
t.doodad value
|
19
|
+
end
|
20
|
+
end
|
21
|
+
it do
|
22
|
+
expect(subject[:doodad]).to eq(value)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
context "given a block value" do
|
26
|
+
subject do
|
27
|
+
Olfactory.build_template :widget do |t|
|
28
|
+
t.doodad { value }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
it do
|
32
|
+
expect(subject[:doodad]).to eq(value)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
context "with an alias" do
|
36
|
+
before do
|
37
|
+
Olfactory.template :widget do |t|
|
38
|
+
t.has_one :doodad, :alias => :foo
|
39
|
+
end
|
40
|
+
end
|
41
|
+
context "given a basic value" do
|
42
|
+
subject do
|
43
|
+
Olfactory.build_template :widget do |t|
|
44
|
+
t.foo value
|
45
|
+
end
|
46
|
+
end
|
47
|
+
it do
|
48
|
+
expect(subject[:doodad]).to eq(value)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
context "given a block value" do
|
52
|
+
subject do
|
53
|
+
Olfactory.build_template :widget do |t|
|
54
|
+
t.foo { value }
|
55
|
+
end
|
56
|
+
end
|
57
|
+
it do
|
58
|
+
expect(subject[:doodad]).to eq(value)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
context "has_many" do
|
64
|
+
before do
|
65
|
+
Olfactory.template :widget do |t|
|
66
|
+
t.has_many :doodad
|
67
|
+
end
|
68
|
+
end
|
69
|
+
let(:value) { "doodad" }
|
70
|
+
|
71
|
+
context "as generic collection" do
|
72
|
+
before do
|
73
|
+
Olfactory.template :widget do |t|
|
74
|
+
t.has_many :doodads, :singular => :doodad
|
75
|
+
end
|
76
|
+
end
|
77
|
+
context "invoked as singular" do
|
78
|
+
context "given a basic value" do
|
79
|
+
subject do
|
80
|
+
Olfactory.build_template :widget do |t|
|
81
|
+
t.doodad value
|
82
|
+
end
|
83
|
+
end
|
84
|
+
it do
|
85
|
+
expect(subject[:doodads]).to eq([value])
|
86
|
+
end
|
87
|
+
end
|
88
|
+
context "given a block value" do
|
89
|
+
subject do
|
90
|
+
Olfactory.build_template :widget do |t|
|
91
|
+
t.doodad { value }
|
92
|
+
end
|
93
|
+
end
|
94
|
+
it do
|
95
|
+
expect(subject[:doodads]).to eq([value])
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
context "invoked as plural" do
|
100
|
+
context "given an integer & block value" do
|
101
|
+
subject do
|
102
|
+
Olfactory.build_template :widget do |t|
|
103
|
+
t.doodads 2 do
|
104
|
+
value
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
it do
|
109
|
+
expect(subject[:doodads]).to eq([value, value])
|
110
|
+
end
|
111
|
+
end
|
112
|
+
context "given an array" do
|
113
|
+
subject do
|
114
|
+
Olfactory.build_template :widget do |t|
|
115
|
+
t.doodads [value, value]
|
116
|
+
end
|
117
|
+
end
|
118
|
+
it do
|
119
|
+
expect(subject[:doodads]).to eq([value, value])
|
120
|
+
end
|
121
|
+
end
|
122
|
+
context "given a splattered set of arguments" do
|
123
|
+
subject do
|
124
|
+
Olfactory.build_template :widget do |t|
|
125
|
+
t.doodads value, value
|
126
|
+
end
|
127
|
+
end
|
128
|
+
it do
|
129
|
+
expect(subject[:doodads]).to eq([value, value])
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
context "given sequential invocations" do
|
134
|
+
subject do
|
135
|
+
Olfactory.build_template :widget do |t|
|
136
|
+
t.doodad value
|
137
|
+
t.doodads value, value
|
138
|
+
t.doodad value
|
139
|
+
end
|
140
|
+
end
|
141
|
+
it do
|
142
|
+
expect(subject[:doodads]).to eq([value, value, value, value])
|
143
|
+
end
|
144
|
+
end
|
145
|
+
context "with an alias" do
|
146
|
+
before do
|
147
|
+
Olfactory.template :widget do |t|
|
148
|
+
t.has_many :doodads, :singular => :doodad, :alias => :foos
|
149
|
+
end
|
150
|
+
end
|
151
|
+
context "given a basic value" do
|
152
|
+
subject do
|
153
|
+
Olfactory.build_template :widget do |t|
|
154
|
+
t.foos value
|
155
|
+
end
|
156
|
+
end
|
157
|
+
it do
|
158
|
+
expect(subject[:doodads]).to eq([value])
|
159
|
+
end
|
160
|
+
end
|
161
|
+
context "given a block value" do
|
162
|
+
subject do
|
163
|
+
Olfactory.build_template :widget do |t|
|
164
|
+
t.foos { value }
|
165
|
+
end
|
166
|
+
end
|
167
|
+
it do
|
168
|
+
# Alias only applies to the plural context, not singular.
|
169
|
+
expect(subject[:doodads]).to eq(nil)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
context "as named collection" do
|
175
|
+
before do
|
176
|
+
Olfactory.template :widget do |t|
|
177
|
+
t.has_many :doodads, :singular => :doodad, :named => :true
|
178
|
+
end
|
179
|
+
end
|
180
|
+
context "invoked as singular" do
|
181
|
+
context "given a name & basic value" do
|
182
|
+
subject do
|
183
|
+
Olfactory.build_template :widget do |t|
|
184
|
+
t.doodad :foo, value
|
185
|
+
end
|
186
|
+
end
|
187
|
+
it do
|
188
|
+
expect(subject[:doodads][:foo]).to eq(value)
|
189
|
+
end
|
190
|
+
end
|
191
|
+
context "given a name & block value" do
|
192
|
+
subject do
|
193
|
+
Olfactory.build_template :widget do |t|
|
194
|
+
t.doodad :foo do
|
195
|
+
value
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
it do
|
200
|
+
expect(subject[:doodads][:foo]).to eq(value)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
context "given no name" do
|
204
|
+
subject do
|
205
|
+
Olfactory.build_template :widget do |t|
|
206
|
+
t.doodad
|
207
|
+
end
|
208
|
+
end
|
209
|
+
it do
|
210
|
+
expect { subject }.to raise_error
|
211
|
+
end
|
212
|
+
end
|
213
|
+
context "given no name & numeric value" do
|
214
|
+
subject do
|
215
|
+
Olfactory.build_template :widget do |t|
|
216
|
+
t.doodad 1
|
217
|
+
end
|
218
|
+
end
|
219
|
+
it do
|
220
|
+
# It would have take "1" as the name, but the object would be nil.
|
221
|
+
# Thus, not added.
|
222
|
+
expect(subject[:doodads]).to eq(nil)
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
226
|
+
context "invoked as plural" do
|
227
|
+
context "given a hash value" do
|
228
|
+
subject do
|
229
|
+
Olfactory.build_template :widget do |t|
|
230
|
+
t.doodads :a => value
|
231
|
+
end
|
232
|
+
end
|
233
|
+
it do
|
234
|
+
expect(subject[:doodads]).to eq({ :a => value })
|
235
|
+
end
|
236
|
+
end
|
237
|
+
context "given no name or value" do
|
238
|
+
subject do
|
239
|
+
Olfactory.build_template :widget do |t|
|
240
|
+
t.doodads
|
241
|
+
end
|
242
|
+
end
|
243
|
+
it do
|
244
|
+
expect(subject).to eq({})
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
248
|
+
context "given sequential invocations" do
|
249
|
+
subject do
|
250
|
+
Olfactory.build_template :widget do |t|
|
251
|
+
t.doodad :a, value
|
252
|
+
t.doodads :b => value, :c => value
|
253
|
+
t.doodad :d, value
|
254
|
+
end
|
255
|
+
end
|
256
|
+
it do
|
257
|
+
expect(subject[:doodads]).to eq({ :a => value,
|
258
|
+
:b => value,
|
259
|
+
:c => value,
|
260
|
+
:d => value })
|
261
|
+
end
|
262
|
+
end
|
263
|
+
context "with an alias" do
|
264
|
+
before do
|
265
|
+
Olfactory.template :widget do |t|
|
266
|
+
t.has_many :doodads, :singular => :doodad,
|
267
|
+
:named => :true,
|
268
|
+
:alias => :foos
|
269
|
+
end
|
270
|
+
end
|
271
|
+
context "given a hash" do
|
272
|
+
subject do
|
273
|
+
Olfactory.build_template :widget do |t|
|
274
|
+
t.foos :a => value
|
275
|
+
end
|
276
|
+
end
|
277
|
+
it do
|
278
|
+
expect(subject[:doodads]).to eq({:a => value})
|
279
|
+
end
|
280
|
+
end
|
281
|
+
context "given a name and basic value" do
|
282
|
+
subject do
|
283
|
+
Olfactory.build_template :widget do |t|
|
284
|
+
t.foos :a, value
|
285
|
+
end
|
286
|
+
end
|
287
|
+
it do
|
288
|
+
# Alias only applies to the plural context, not singular.
|
289
|
+
expect(subject[:doodads]).to eq(nil)
|
290
|
+
end
|
291
|
+
end
|
292
|
+
end
|
293
|
+
end
|
294
|
+
end
|
295
|
+
context "embeds_one" do
|
296
|
+
before do
|
297
|
+
Olfactory.template :widget do |t|
|
298
|
+
t.embeds_one :doodad
|
299
|
+
end
|
300
|
+
Olfactory.template :doodad do |t|
|
301
|
+
t.has_one :gizmo
|
302
|
+
end
|
303
|
+
end
|
304
|
+
let(:value) { "gizmo" }
|
305
|
+
|
306
|
+
context "given no value" do
|
307
|
+
subject do
|
308
|
+
Olfactory.build_template :widget do |t|
|
309
|
+
t.doodad
|
310
|
+
end
|
311
|
+
end
|
312
|
+
it do
|
313
|
+
expect(subject[:doodad]).to eq({})
|
314
|
+
end
|
315
|
+
end
|
316
|
+
context "given a symbol value" do
|
317
|
+
before do
|
318
|
+
Olfactory.template :widget do |t|
|
319
|
+
t.embeds_one :doodad
|
320
|
+
end
|
321
|
+
Olfactory.template :doodad do |t|
|
322
|
+
t.has_one :gizmo
|
323
|
+
t.preset :shiny do |p|
|
324
|
+
p.gizmo "shiny #{value}"
|
325
|
+
end
|
326
|
+
end
|
327
|
+
end
|
328
|
+
context "that matches a defined preset" do
|
329
|
+
subject do
|
330
|
+
Olfactory.build_template :widget do |t|
|
331
|
+
t.doodad :shiny
|
332
|
+
end
|
333
|
+
end
|
334
|
+
it do
|
335
|
+
# Expect it to invoke a preset on the subtemplate
|
336
|
+
expect(subject[:doodad][:gizmo]).to eq("shiny #{value}")
|
337
|
+
end
|
338
|
+
end
|
339
|
+
context "that does not match a defined preset" do
|
340
|
+
subject do
|
341
|
+
Olfactory.build_template :widget do |t|
|
342
|
+
t.doodad :rusty
|
343
|
+
end
|
344
|
+
end
|
345
|
+
it do
|
346
|
+
# Expect it to invoke a preset on the subtemplate
|
347
|
+
expect{ subject }.to raise_error
|
348
|
+
end
|
349
|
+
end
|
350
|
+
end
|
351
|
+
context "given a block value" do
|
352
|
+
subject do
|
353
|
+
Olfactory.build_template :widget do |t|
|
354
|
+
t.doodad { |d| d.gizmo value }
|
355
|
+
end
|
356
|
+
end
|
357
|
+
it do
|
358
|
+
expect(subject[:doodad]).to eq({ :gizmo => value })
|
359
|
+
end
|
360
|
+
end
|
361
|
+
context "with an alias" do
|
362
|
+
before do
|
363
|
+
Olfactory.template :widget do |t|
|
364
|
+
t.embeds_one :doodad, :alias => :foo
|
365
|
+
end
|
366
|
+
Olfactory.template :doodad do |t|
|
367
|
+
t.has_one :gizmo
|
368
|
+
end
|
369
|
+
end
|
370
|
+
context "given no value" do
|
371
|
+
subject do
|
372
|
+
Olfactory.build_template :widget do |t|
|
373
|
+
t.foo
|
374
|
+
end
|
375
|
+
end
|
376
|
+
it do
|
377
|
+
expect(subject[:doodad]).to eq({})
|
378
|
+
end
|
379
|
+
end
|
380
|
+
end
|
381
|
+
context "with template name" do
|
382
|
+
before do
|
383
|
+
Olfactory.template :widget do |t|
|
384
|
+
t.embeds_one :doodad, :template => :thingamabob
|
385
|
+
end
|
386
|
+
Olfactory.template :thingamabob do |t|
|
387
|
+
t.has_one :gizmo
|
388
|
+
end
|
389
|
+
end
|
390
|
+
context "given no value" do
|
391
|
+
subject do
|
392
|
+
Olfactory.build_template :widget do |t|
|
393
|
+
t.doodad
|
394
|
+
end
|
395
|
+
end
|
396
|
+
it do
|
397
|
+
expect(subject[:doodad]).to eq({})
|
398
|
+
end
|
399
|
+
end
|
400
|
+
end
|
401
|
+
end
|
402
|
+
context "embeds_many" do
|
403
|
+
before do
|
404
|
+
Olfactory.template :widget do |t|
|
405
|
+
t.embeds_many :doodads
|
406
|
+
end
|
407
|
+
Olfactory.template :doodad do |t|
|
408
|
+
t.has_one :gizmo
|
409
|
+
end
|
410
|
+
end
|
411
|
+
let(:value) { "gizmo" }
|
412
|
+
|
413
|
+
context "as generic collection" do
|
414
|
+
before do
|
415
|
+
Olfactory.template :widget do |t|
|
416
|
+
t.embeds_many :doodads, :singular => :doodad
|
417
|
+
end
|
418
|
+
Olfactory.template :doodad do |t|
|
419
|
+
t.has_one :gizmo
|
420
|
+
end
|
421
|
+
end
|
422
|
+
context "invoked as singular" do
|
423
|
+
context "given no value" do
|
424
|
+
subject do
|
425
|
+
Olfactory.build_template :widget do |t|
|
426
|
+
t.doodad
|
427
|
+
end
|
428
|
+
end
|
429
|
+
it do
|
430
|
+
expect(subject[:doodads]).to eq([{}])
|
431
|
+
end
|
432
|
+
end
|
433
|
+
context "given a symbol value" do
|
434
|
+
before do
|
435
|
+
Olfactory.template :widget do |t|
|
436
|
+
t.embeds_many :doodads, :singular => :doodad
|
437
|
+
end
|
438
|
+
Olfactory.template :doodad do |t|
|
439
|
+
t.has_one :gizmo
|
440
|
+
t.preset :shiny do |p|
|
441
|
+
p.gizmo "shiny #{value}"
|
442
|
+
end
|
443
|
+
end
|
444
|
+
end
|
445
|
+
context "that matches a defined preset" do
|
446
|
+
subject do
|
447
|
+
Olfactory.build_template :widget do |t|
|
448
|
+
t.doodad :shiny
|
449
|
+
end
|
450
|
+
end
|
451
|
+
it do
|
452
|
+
# Expect it to invoke a preset on the subtemplate
|
453
|
+
expect(subject[:doodads]).to eq([{ :gizmo => "shiny #{value}" }])
|
454
|
+
end
|
455
|
+
end
|
456
|
+
context "that does not match a defined preset" do
|
457
|
+
subject do
|
458
|
+
Olfactory.build_template :widget do |t|
|
459
|
+
t.doodad :rusty
|
460
|
+
end
|
461
|
+
end
|
462
|
+
it do
|
463
|
+
# Expect it to invoke a preset on the subtemplate
|
464
|
+
expect{ subject }.to raise_error
|
465
|
+
end
|
466
|
+
end
|
467
|
+
end
|
468
|
+
context "given a block value" do
|
469
|
+
subject do
|
470
|
+
Olfactory.build_template :widget do |t|
|
471
|
+
t.doodad { |d| d.gizmo value }
|
472
|
+
end
|
473
|
+
end
|
474
|
+
it do
|
475
|
+
expect(subject[:doodads]).to eq([{ :gizmo => value }])
|
476
|
+
end
|
477
|
+
end
|
478
|
+
end
|
479
|
+
context "invoked as plural" do
|
480
|
+
context "given an integer value" do
|
481
|
+
subject do
|
482
|
+
Olfactory.build_template :widget do |t|
|
483
|
+
t.doodads 2
|
484
|
+
end
|
485
|
+
end
|
486
|
+
it do
|
487
|
+
expect(subject[:doodads]).to eq([{}, {}])
|
488
|
+
end
|
489
|
+
end
|
490
|
+
context "given a symbol & integer value" do
|
491
|
+
before do
|
492
|
+
Olfactory.template :widget do |t|
|
493
|
+
t.embeds_many :doodads, :singular => :doodad
|
494
|
+
end
|
495
|
+
Olfactory.template :doodad do |t|
|
496
|
+
t.has_one :gizmo
|
497
|
+
t.preset :shiny do |p|
|
498
|
+
p.gizmo "shiny #{value}"
|
499
|
+
end
|
500
|
+
end
|
501
|
+
end
|
502
|
+
context "that matches a defined preset" do
|
503
|
+
subject do
|
504
|
+
Olfactory.build_template :widget do |t|
|
505
|
+
t.doodads :shiny, 2
|
506
|
+
end
|
507
|
+
end
|
508
|
+
it do
|
509
|
+
# Expect it to invoke a preset on the subtemplate
|
510
|
+
expect(subject[:doodads]).to eq([{ :gizmo => "shiny #{value}" }, { :gizmo => "shiny #{value}" }])
|
511
|
+
end
|
512
|
+
end
|
513
|
+
context "that does not match a defined preset" do
|
514
|
+
subject do
|
515
|
+
Olfactory.build_template :widget do |t|
|
516
|
+
t.doodads :rusty, 2
|
517
|
+
end
|
518
|
+
end
|
519
|
+
it do
|
520
|
+
# Expect it to invoke a preset on the subtemplate
|
521
|
+
expect{ subject }.to raise_error
|
522
|
+
end
|
523
|
+
end
|
524
|
+
end
|
525
|
+
context "given an integer & symbol value" do
|
526
|
+
before do
|
527
|
+
Olfactory.template :widget do |t|
|
528
|
+
t.embeds_many :doodads, :singular => :doodad
|
529
|
+
end
|
530
|
+
Olfactory.template :doodad do |t|
|
531
|
+
t.has_one :gizmo
|
532
|
+
t.preset :shiny do |p|
|
533
|
+
p.gizmo "shiny #{value}"
|
534
|
+
end
|
535
|
+
end
|
536
|
+
end
|
537
|
+
context "that matches a defined preset" do
|
538
|
+
subject do
|
539
|
+
Olfactory.build_template :widget do |t|
|
540
|
+
t.doodads 2, :shiny
|
541
|
+
end
|
542
|
+
end
|
543
|
+
it do
|
544
|
+
# Expect it to invoke a preset on the subtemplate
|
545
|
+
expect(subject[:doodads]).to eq([{ :gizmo => "shiny #{value}" }, { :gizmo => "shiny #{value}" }])
|
546
|
+
end
|
547
|
+
end
|
548
|
+
context "that does not match a defined preset" do
|
549
|
+
subject do
|
550
|
+
Olfactory.build_template :widget do |t|
|
551
|
+
t.doodads 2, :rusty
|
552
|
+
end
|
553
|
+
end
|
554
|
+
it do
|
555
|
+
# Expect it to invoke a preset on the subtemplate
|
556
|
+
expect{ subject }.to raise_error
|
557
|
+
end
|
558
|
+
end
|
559
|
+
end
|
560
|
+
context "given an integer and block value" do
|
561
|
+
subject do
|
562
|
+
Olfactory.build_template :widget do |t|
|
563
|
+
t.doodads 2 do |d|
|
564
|
+
d.gizmo value
|
565
|
+
end
|
566
|
+
end
|
567
|
+
end
|
568
|
+
it do
|
569
|
+
expect(subject[:doodads]).to eq([{ :gizmo => value }, { :gizmo => value }])
|
570
|
+
end
|
571
|
+
end
|
572
|
+
end
|
573
|
+
context "given sequential invocations" do
|
574
|
+
subject do
|
575
|
+
Olfactory.build_template :widget do |t|
|
576
|
+
t.doodad
|
577
|
+
t.doodads 2
|
578
|
+
t.doodad
|
579
|
+
end
|
580
|
+
end
|
581
|
+
it do
|
582
|
+
expect(subject[:doodads]).to eq([{}, {}, {}, {}])
|
583
|
+
end
|
584
|
+
end
|
585
|
+
context "with an alias" do
|
586
|
+
before do
|
587
|
+
Olfactory.template :widget do |t|
|
588
|
+
t.embeds_many :doodads, :singular => :doodad, :alias => :foos
|
589
|
+
end
|
590
|
+
Olfactory.template :doodad do |t|
|
591
|
+
t.has_one :gizmo
|
592
|
+
end
|
593
|
+
end
|
594
|
+
context "given an integer value" do
|
595
|
+
subject do
|
596
|
+
Olfactory.build_template :widget do |t|
|
597
|
+
t.foos 2
|
598
|
+
end
|
599
|
+
end
|
600
|
+
it do
|
601
|
+
expect(subject[:doodads]).to eq([{},{}])
|
602
|
+
end
|
603
|
+
end
|
604
|
+
context "given a block value" do
|
605
|
+
subject do
|
606
|
+
Olfactory.build_template :widget do |t|
|
607
|
+
t.foos { |f| f.gizmo value }
|
608
|
+
end
|
609
|
+
end
|
610
|
+
it do
|
611
|
+
# Alias only applies to the plural context, not singular.
|
612
|
+
expect { subject[:doodads] }.to raise_error
|
613
|
+
end
|
614
|
+
end
|
615
|
+
end
|
616
|
+
end
|
617
|
+
context "as named collection" do
|
618
|
+
before do
|
619
|
+
Olfactory.template :widget do |t|
|
620
|
+
t.embeds_many :doodads, :singular => :doodad,
|
621
|
+
:named => true
|
622
|
+
end
|
623
|
+
Olfactory.template :doodad do |t|
|
624
|
+
t.has_one :gizmo
|
625
|
+
end
|
626
|
+
end
|
627
|
+
context "invoked as singular" do
|
628
|
+
context "given a name & no value" do
|
629
|
+
subject do
|
630
|
+
Olfactory.build_template :widget do |t|
|
631
|
+
t.doodad :a
|
632
|
+
end
|
633
|
+
end
|
634
|
+
it do
|
635
|
+
expect(subject[:doodads][:a]).to eq({})
|
636
|
+
end
|
637
|
+
end
|
638
|
+
context "given a name & symbol value" do
|
639
|
+
before do
|
640
|
+
Olfactory.template :widget do |t|
|
641
|
+
t.embeds_many :doodads, :singular => :doodad,
|
642
|
+
:named => true
|
643
|
+
end
|
644
|
+
Olfactory.template :doodad do |t|
|
645
|
+
t.has_one :gizmo
|
646
|
+
t.preset :shiny do |p|
|
647
|
+
p.gizmo "shiny #{value}"
|
648
|
+
end
|
649
|
+
end
|
650
|
+
end
|
651
|
+
context "that matches a defined preset" do
|
652
|
+
subject do
|
653
|
+
Olfactory.build_template :widget do |t|
|
654
|
+
t.doodad :a, :shiny
|
655
|
+
end
|
656
|
+
end
|
657
|
+
it do
|
658
|
+
# Expect it to invoke a preset on the subtemplate
|
659
|
+
expect(subject[:doodads]).to eq({ :a => { :gizmo => "shiny #{value}" }})
|
660
|
+
end
|
661
|
+
end
|
662
|
+
context "that does not match a defined preset" do
|
663
|
+
subject do
|
664
|
+
Olfactory.build_template :widget do |t|
|
665
|
+
t.doodad :a, :rusty
|
666
|
+
end
|
667
|
+
end
|
668
|
+
it do
|
669
|
+
# Expect it to invoke a preset on the subtemplate
|
670
|
+
expect{ subject }.to raise_error
|
671
|
+
end
|
672
|
+
end
|
673
|
+
end
|
674
|
+
context "given a name & block value" do
|
675
|
+
subject do
|
676
|
+
Olfactory.build_template :widget do |t|
|
677
|
+
t.doodad :a do |d|
|
678
|
+
d.gizmo value
|
679
|
+
end
|
680
|
+
end
|
681
|
+
end
|
682
|
+
it do
|
683
|
+
expect(subject[:doodads]).to eq({ :a => { :gizmo => value }})
|
684
|
+
end
|
685
|
+
end
|
686
|
+
context "given a no name" do
|
687
|
+
subject do
|
688
|
+
Olfactory.build_template :widget do |t|
|
689
|
+
t.doodad
|
690
|
+
end
|
691
|
+
end
|
692
|
+
it do
|
693
|
+
expect { subject }.to raise_error
|
694
|
+
end
|
695
|
+
end
|
696
|
+
context "given a no name & numeric value" do
|
697
|
+
subject do
|
698
|
+
Olfactory.build_template :widget do |t|
|
699
|
+
t.doodad 1
|
700
|
+
end
|
701
|
+
end
|
702
|
+
it do
|
703
|
+
expect(subject[:doodads]).to eq({ 1 => {} })
|
704
|
+
end
|
705
|
+
end
|
706
|
+
end
|
707
|
+
context "invoked as plural" do
|
708
|
+
context "given a hash value" do
|
709
|
+
subject do
|
710
|
+
Olfactory.build_template :widget do |t|
|
711
|
+
t.doodads :a => value
|
712
|
+
end
|
713
|
+
end
|
714
|
+
it do
|
715
|
+
expect(subject[:doodads]).to eq(nil)
|
716
|
+
end
|
717
|
+
end
|
718
|
+
end
|
719
|
+
end
|
720
|
+
context "with template name" do
|
721
|
+
before do
|
722
|
+
Olfactory.template :widget do |t|
|
723
|
+
t.embeds_many :doodads, :singular => :doodad,
|
724
|
+
:template => :thingamabob
|
725
|
+
end
|
726
|
+
Olfactory.template :thingamabob do |t|
|
727
|
+
t.has_one :gizmo
|
728
|
+
end
|
729
|
+
end
|
730
|
+
context "given no value" do
|
731
|
+
subject do
|
732
|
+
Olfactory.build_template :widget do |t|
|
733
|
+
t.doodad
|
734
|
+
end
|
735
|
+
end
|
736
|
+
it do
|
737
|
+
expect(subject[:doodads]).to eq([{}])
|
738
|
+
end
|
739
|
+
end
|
740
|
+
end
|
741
|
+
end
|
742
|
+
context "macros" do
|
743
|
+
before do
|
744
|
+
Olfactory.template :widget do |t|
|
745
|
+
t.has_one :doodad
|
746
|
+
t.macro :make_shiny do |m, v|
|
747
|
+
m.doodad "shiny #{m[:doodad]}"
|
748
|
+
end
|
749
|
+
end
|
750
|
+
end
|
751
|
+
let(:value) { "doodad" }
|
752
|
+
|
753
|
+
subject do
|
754
|
+
Olfactory.build_template :widget do |t|
|
755
|
+
t.doodad value
|
756
|
+
t.make_shiny
|
757
|
+
end
|
758
|
+
end
|
759
|
+
it "should execute its block" do
|
760
|
+
expect(subject[:doodad]).to eq("shiny #{value}")
|
761
|
+
end
|
762
|
+
|
763
|
+
context "with parameters" do
|
764
|
+
before do
|
765
|
+
Olfactory.template :widget do |t|
|
766
|
+
t.has_one :doodad
|
767
|
+
t.macro :make do |m, adj, adj2|
|
768
|
+
m.doodad "#{adj.to_s} #{adj2.to_s} #{m[:doodad]}"
|
769
|
+
end
|
770
|
+
end
|
771
|
+
end
|
772
|
+
subject do
|
773
|
+
Olfactory.build_template :widget do |t|
|
774
|
+
t.doodad value
|
775
|
+
t.make :very, :shiny
|
776
|
+
end
|
777
|
+
end
|
778
|
+
it "receives them on invocation" do
|
779
|
+
expect(subject[:doodad]).to eq("very shiny #{value}")
|
780
|
+
end
|
781
|
+
end
|
782
|
+
end
|
783
|
+
context "transients" do
|
784
|
+
before do
|
785
|
+
Olfactory.template :widget do |t|
|
786
|
+
t.has_one :doodad
|
787
|
+
end
|
788
|
+
end
|
789
|
+
let(:value) { "temporary value"}
|
790
|
+
context "when set" do
|
791
|
+
subject do
|
792
|
+
Olfactory.build_template :widget do |t|
|
793
|
+
t.transient :foo, value
|
794
|
+
end
|
795
|
+
end
|
796
|
+
it do
|
797
|
+
expect(subject.transients[:foo]).to eq(value)
|
798
|
+
end
|
799
|
+
end
|
800
|
+
context "when read" do
|
801
|
+
subject do
|
802
|
+
Olfactory.build_template :widget do |t|
|
803
|
+
t.transient :foo, value
|
804
|
+
t.doodad "#{t.transients[:foo]}"
|
805
|
+
end
|
806
|
+
end
|
807
|
+
it do
|
808
|
+
expect(subject[:doodad]).to eq(value)
|
809
|
+
end
|
810
|
+
end
|
811
|
+
context "when inherited" do
|
812
|
+
before do
|
813
|
+
Olfactory.template :widget do |t|
|
814
|
+
t.embeds_one :doodad
|
815
|
+
end
|
816
|
+
Olfactory.template :doodad do |t|
|
817
|
+
t.has_one :gizmo
|
818
|
+
end
|
819
|
+
end
|
820
|
+
subject do
|
821
|
+
Olfactory.build_template :widget do |t|
|
822
|
+
t.transient :foo, value
|
823
|
+
t.doodad
|
824
|
+
end
|
825
|
+
end
|
826
|
+
it do
|
827
|
+
expect(subject[:doodad].transients[:foo]).to eq(value)
|
828
|
+
end
|
829
|
+
end
|
830
|
+
end
|
831
|
+
context "defaults" do
|
832
|
+
let(:value) { "doodad" }
|
833
|
+
let(:default_value) { "default doodad" }
|
834
|
+
context "before" do
|
835
|
+
before do
|
836
|
+
Olfactory.template :widget do |t|
|
837
|
+
t.has_one :doodad
|
838
|
+
t.has_one :other_doodad
|
839
|
+
t.before do |d|
|
840
|
+
d.doodad default_value
|
841
|
+
d.other_doodad default_value
|
842
|
+
end
|
843
|
+
end
|
844
|
+
end
|
845
|
+
subject do
|
846
|
+
Olfactory.build_template :widget do |t|
|
847
|
+
t.doodad value
|
848
|
+
end
|
849
|
+
end
|
850
|
+
it "values can be overriden" do
|
851
|
+
expect(subject[:doodad]).to eq(value)
|
852
|
+
end
|
853
|
+
it "values can be set"do
|
854
|
+
expect(subject[:other_doodad]).to eq(default_value)
|
855
|
+
end
|
856
|
+
end
|
857
|
+
context "after" do
|
858
|
+
before do
|
859
|
+
Olfactory.template :widget do |t|
|
860
|
+
t.has_one :doodad
|
861
|
+
t.has_one :other_doodad
|
862
|
+
t.after do |d|
|
863
|
+
d.doodad default_value
|
864
|
+
d.other_doodad default_value
|
865
|
+
end
|
866
|
+
end
|
867
|
+
end
|
868
|
+
subject do
|
869
|
+
Olfactory.build_template :widget do |t|
|
870
|
+
t.doodad value
|
871
|
+
end
|
872
|
+
end
|
873
|
+
it "values will not override existing ones" do
|
874
|
+
expect(subject[:doodad]).to eq(value)
|
875
|
+
end
|
876
|
+
it "values will fill in missing ones"do
|
877
|
+
expect(subject[:other_doodad]).to eq(default_value)
|
878
|
+
end
|
879
|
+
end
|
880
|
+
end
|
881
|
+
context "when created" do
|
882
|
+
let(:value) { "saveable string" }
|
883
|
+
context "containing a saveable object" do
|
884
|
+
before do
|
885
|
+
Olfactory.template :widget do |t|
|
886
|
+
t.has_one :doodad
|
887
|
+
end
|
888
|
+
end
|
889
|
+
|
890
|
+
subject do
|
891
|
+
Olfactory.create_template :widget do |w|
|
892
|
+
w.doodad SaveableString.new(value)
|
893
|
+
end
|
894
|
+
end
|
895
|
+
it do
|
896
|
+
expect(subject[:doodad].saved?).to be true
|
897
|
+
end
|
898
|
+
end
|
899
|
+
context "containing a generic collection of saveable objects" do
|
900
|
+
before do
|
901
|
+
Olfactory.template :widget do |t|
|
902
|
+
t.has_many :doodads
|
903
|
+
end
|
904
|
+
end
|
905
|
+
subject do
|
906
|
+
Olfactory.create_template :widget do |w|
|
907
|
+
w.doodads SaveableString.new(value), SaveableString.new(value)
|
908
|
+
end
|
909
|
+
end
|
910
|
+
it { expect(subject[:doodads].first.saved?).to be true }
|
911
|
+
it { expect(subject[:doodads].last.saved?).to be true }
|
912
|
+
end
|
913
|
+
context "containing a named collection of saveable objects" do
|
914
|
+
before do
|
915
|
+
Olfactory.template :widget do |t|
|
916
|
+
t.has_many :doodads, :named => true
|
917
|
+
end
|
918
|
+
end
|
919
|
+
subject do
|
920
|
+
Olfactory.create_template :widget do |w|
|
921
|
+
w.doodads :a => SaveableString.new(value), :b => SaveableString.new(value)
|
922
|
+
end
|
923
|
+
end
|
924
|
+
it { expect(subject[:doodads][:a].saved?).to be true }
|
925
|
+
it { expect(subject[:doodads][:b].saved?).to be true }
|
926
|
+
end
|
927
|
+
context "with an embedded template" do
|
928
|
+
before do
|
929
|
+
Olfactory.template :widget do |t|
|
930
|
+
t.embeds_one :doodad
|
931
|
+
end
|
932
|
+
end
|
933
|
+
context "containing a saveable object" do
|
934
|
+
before do
|
935
|
+
Olfactory.template :doodad do |t|
|
936
|
+
t.has_one :gizmo
|
937
|
+
end
|
938
|
+
end
|
939
|
+
subject do
|
940
|
+
Olfactory.create_template :widget do |w|
|
941
|
+
w.doodad { |d| d.gizmo SaveableString.new(value) }
|
942
|
+
end
|
943
|
+
end
|
944
|
+
it { expect(subject[:doodad][:gizmo].saved?).to be true }
|
945
|
+
end
|
946
|
+
context "containing a generic collection of saveable objects" do
|
947
|
+
before do
|
948
|
+
Olfactory.template :doodad do |t|
|
949
|
+
t.has_many :gizmos
|
950
|
+
end
|
951
|
+
end
|
952
|
+
subject do
|
953
|
+
Olfactory.create_template :widget do |w|
|
954
|
+
w.doodad { |d| d.gizmos SaveableString.new(value), SaveableString.new(value) }
|
955
|
+
end
|
956
|
+
end
|
957
|
+
it { expect(subject[:doodad][:gizmos].first.saved?).to be true }
|
958
|
+
it { expect(subject[:doodad][:gizmos].last.saved?).to be true }
|
959
|
+
end
|
960
|
+
context "containing a named collection of saveable objects" do
|
961
|
+
before do
|
962
|
+
Olfactory.template :doodad do |t|
|
963
|
+
t.has_many :gizmos, :named => true
|
964
|
+
end
|
965
|
+
end
|
966
|
+
subject do
|
967
|
+
Olfactory.create_template :widget do |w|
|
968
|
+
w.doodad { |d| d.gizmos :a => SaveableString.new(value), :b => SaveableString.new(value) }
|
969
|
+
end
|
970
|
+
end
|
971
|
+
it { expect(subject[:doodad][:gizmos][:a].saved?).to be true }
|
972
|
+
it { expect(subject[:doodad][:gizmos][:b].saved?).to be true }
|
973
|
+
end
|
974
|
+
end
|
975
|
+
context "with a generic collection of embedded templates" do
|
976
|
+
before do
|
977
|
+
Olfactory.template :widget do |t|
|
978
|
+
t.embeds_many :doodads, :singular => :doodad
|
979
|
+
end
|
980
|
+
end
|
981
|
+
context "containing a saveable object" do
|
982
|
+
before do
|
983
|
+
Olfactory.template :doodad do |t|
|
984
|
+
t.has_one :gizmo
|
985
|
+
end
|
986
|
+
end
|
987
|
+
subject do
|
988
|
+
Olfactory.create_template :widget do |w|
|
989
|
+
w.doodad { |d| d.gizmo SaveableString.new(value) }
|
990
|
+
end
|
991
|
+
end
|
992
|
+
it { expect(subject[:doodads].first[:gizmo].saved?).to be true }
|
993
|
+
end
|
994
|
+
context "containing a generic collection of saveable objects" do
|
995
|
+
before do
|
996
|
+
Olfactory.template :doodad do |t|
|
997
|
+
t.has_many :gizmos
|
998
|
+
end
|
999
|
+
end
|
1000
|
+
subject do
|
1001
|
+
Olfactory.create_template :widget do |w|
|
1002
|
+
w.doodad { |d| d.gizmos SaveableString.new(value), SaveableString.new(value) }
|
1003
|
+
end
|
1004
|
+
end
|
1005
|
+
it { expect(subject[:doodads].first[:gizmos].first.saved?).to be true }
|
1006
|
+
it { expect(subject[:doodads].first[:gizmos].last.saved?).to be true }
|
1007
|
+
end
|
1008
|
+
context "containing a named collection of saveable objects" do
|
1009
|
+
before do
|
1010
|
+
Olfactory.template :doodad do |t|
|
1011
|
+
t.has_many :gizmos, :named => true
|
1012
|
+
end
|
1013
|
+
end
|
1014
|
+
subject do
|
1015
|
+
Olfactory.create_template :widget do |w|
|
1016
|
+
w.doodad { |d| d.gizmos :a => SaveableString.new(value), :b => SaveableString.new(value) }
|
1017
|
+
end
|
1018
|
+
end
|
1019
|
+
it { expect(subject[:doodads].first[:gizmos][:a].saved?).to be true }
|
1020
|
+
it { expect(subject[:doodads].first[:gizmos][:b].saved?).to be true }
|
1021
|
+
end
|
1022
|
+
end
|
1023
|
+
context "with a named collection of embedded templates" do
|
1024
|
+
before do
|
1025
|
+
Olfactory.template :widget do |t|
|
1026
|
+
t.embeds_many :doodads, :singular => :doodad, :named => true
|
1027
|
+
end
|
1028
|
+
end
|
1029
|
+
context "containing a saveable object" do
|
1030
|
+
before do
|
1031
|
+
Olfactory.template :doodad do |t|
|
1032
|
+
t.has_one :gizmo
|
1033
|
+
end
|
1034
|
+
end
|
1035
|
+
subject do
|
1036
|
+
Olfactory.create_template :widget do |w|
|
1037
|
+
w.doodad :one do |d|
|
1038
|
+
d.gizmo SaveableString.new(value)
|
1039
|
+
end
|
1040
|
+
end
|
1041
|
+
end
|
1042
|
+
it { expect(subject[:doodads][:one][:gizmo].saved?).to be true }
|
1043
|
+
end
|
1044
|
+
context "containing a generic collection of saveable objects" do
|
1045
|
+
before do
|
1046
|
+
Olfactory.template :doodad do |t|
|
1047
|
+
t.has_many :gizmos
|
1048
|
+
end
|
1049
|
+
end
|
1050
|
+
subject do
|
1051
|
+
Olfactory.create_template :widget do |w|
|
1052
|
+
w.doodad :one do |d|
|
1053
|
+
d.gizmos SaveableString.new(value), SaveableString.new(value)
|
1054
|
+
end
|
1055
|
+
end
|
1056
|
+
end
|
1057
|
+
it { expect(subject[:doodads][:one][:gizmos].first.saved?).to be true }
|
1058
|
+
it { expect(subject[:doodads][:one][:gizmos].last.saved?).to be true }
|
1059
|
+
end
|
1060
|
+
context "containing a named collection of saveable objects" do
|
1061
|
+
before do
|
1062
|
+
Olfactory.template :doodad do |t|
|
1063
|
+
t.has_many :gizmos, :named => true
|
1064
|
+
end
|
1065
|
+
end
|
1066
|
+
subject do
|
1067
|
+
Olfactory.create_template :widget do |w|
|
1068
|
+
w.doodad :one do |d|
|
1069
|
+
d.gizmos :a => SaveableString.new(value), :b => SaveableString.new(value)
|
1070
|
+
end
|
1071
|
+
end
|
1072
|
+
end
|
1073
|
+
it { expect(subject[:doodads][:one][:gizmos][:a].saved?).to be true }
|
1074
|
+
it { expect(subject[:doodads][:one][:gizmos][:b].saved?).to be true }
|
1075
|
+
end
|
1076
|
+
end
|
1077
|
+
end
|
1078
|
+
end
|
1079
|
+
|
1080
|
+
|