olfactory 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,11 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Olfactory::Template do
4
- before do
5
- Olfactory.reload
6
- end
7
4
  context "has_one" do
8
- before do
5
+ before(:context) do
9
6
  Olfactory.template :widget do |t|
10
7
  t.has_one :doodad
11
8
  end
@@ -14,7 +11,7 @@ describe Olfactory::Template do
14
11
 
15
12
  context "given a basic value" do
16
13
  subject do
17
- Olfactory.build_template :widget do |t|
14
+ Olfactory.build :widget do |t|
18
15
  t.doodad value
19
16
  end
20
17
  end
@@ -24,7 +21,7 @@ describe Olfactory::Template do
24
21
  end
25
22
  context "given a block value" do
26
23
  subject do
27
- Olfactory.build_template :widget do |t|
24
+ Olfactory.build :widget do |t|
28
25
  t.doodad { value }
29
26
  end
30
27
  end
@@ -33,14 +30,14 @@ describe Olfactory::Template do
33
30
  end
34
31
  end
35
32
  context "with an alias" do
36
- before do
33
+ before(:context) do
37
34
  Olfactory.template :widget do |t|
38
35
  t.has_one :doodad, :alias => :foo
39
36
  end
40
37
  end
41
38
  context "given a basic value" do
42
39
  subject do
43
- Olfactory.build_template :widget do |t|
40
+ Olfactory.build :widget do |t|
44
41
  t.foo value
45
42
  end
46
43
  end
@@ -50,7 +47,7 @@ describe Olfactory::Template do
50
47
  end
51
48
  context "given a block value" do
52
49
  subject do
53
- Olfactory.build_template :widget do |t|
50
+ Olfactory.build :widget do |t|
54
51
  t.foo { value }
55
52
  end
56
53
  end
@@ -61,7 +58,7 @@ describe Olfactory::Template do
61
58
  end
62
59
  end
63
60
  context "has_many" do
64
- before do
61
+ before(:context) do
65
62
  Olfactory.template :widget do |t|
66
63
  t.has_many :doodad
67
64
  end
@@ -69,7 +66,7 @@ describe Olfactory::Template do
69
66
  let(:value) { "doodad" }
70
67
 
71
68
  context "as generic collection" do
72
- before do
69
+ before(:context) do
73
70
  Olfactory.template :widget do |t|
74
71
  t.has_many :doodads, :singular => :doodad
75
72
  end
@@ -77,7 +74,7 @@ describe Olfactory::Template do
77
74
  context "invoked as singular" do
78
75
  context "given a basic value" do
79
76
  subject do
80
- Olfactory.build_template :widget do |t|
77
+ Olfactory.build :widget do |t|
81
78
  t.doodad value
82
79
  end
83
80
  end
@@ -87,7 +84,7 @@ describe Olfactory::Template do
87
84
  end
88
85
  context "given a block value" do
89
86
  subject do
90
- Olfactory.build_template :widget do |t|
87
+ Olfactory.build :widget do |t|
91
88
  t.doodad { value }
92
89
  end
93
90
  end
@@ -99,7 +96,7 @@ describe Olfactory::Template do
99
96
  context "invoked as plural" do
100
97
  context "given an integer & block value" do
101
98
  subject do
102
- Olfactory.build_template :widget do |t|
99
+ Olfactory.build :widget do |t|
103
100
  t.doodads 2 do
104
101
  value
105
102
  end
@@ -111,7 +108,7 @@ describe Olfactory::Template do
111
108
  end
112
109
  context "given an array" do
113
110
  subject do
114
- Olfactory.build_template :widget do |t|
111
+ Olfactory.build :widget do |t|
115
112
  t.doodads [value, value]
116
113
  end
117
114
  end
@@ -121,7 +118,7 @@ describe Olfactory::Template do
121
118
  end
122
119
  context "given a splattered set of arguments" do
123
120
  subject do
124
- Olfactory.build_template :widget do |t|
121
+ Olfactory.build :widget do |t|
125
122
  t.doodads value, value
126
123
  end
127
124
  end
@@ -132,7 +129,7 @@ describe Olfactory::Template do
132
129
  end
133
130
  context "given sequential invocations" do
134
131
  subject do
135
- Olfactory.build_template :widget do |t|
132
+ Olfactory.build :widget do |t|
136
133
  t.doodad value
137
134
  t.doodads value, value
138
135
  t.doodad value
@@ -143,14 +140,14 @@ describe Olfactory::Template do
143
140
  end
144
141
  end
145
142
  context "with an alias" do
146
- before do
143
+ before(:context) do
147
144
  Olfactory.template :widget do |t|
148
145
  t.has_many :doodads, :singular => :doodad, :alias => :foos
149
146
  end
150
147
  end
151
148
  context "given a basic value" do
152
149
  subject do
153
- Olfactory.build_template :widget do |t|
150
+ Olfactory.build :widget do |t|
154
151
  t.foos value
155
152
  end
156
153
  end
@@ -160,7 +157,7 @@ describe Olfactory::Template do
160
157
  end
161
158
  context "given a block value" do
162
159
  subject do
163
- Olfactory.build_template :widget do |t|
160
+ Olfactory.build :widget do |t|
164
161
  t.foos { value }
165
162
  end
166
163
  end
@@ -172,7 +169,7 @@ describe Olfactory::Template do
172
169
  end
173
170
  end
174
171
  context "as named collection" do
175
- before do
172
+ before(:context) do
176
173
  Olfactory.template :widget do |t|
177
174
  t.has_many :doodads, :singular => :doodad, :named => :true
178
175
  end
@@ -180,7 +177,7 @@ describe Olfactory::Template do
180
177
  context "invoked as singular" do
181
178
  context "given a name & basic value" do
182
179
  subject do
183
- Olfactory.build_template :widget do |t|
180
+ Olfactory.build :widget do |t|
184
181
  t.doodad :foo, value
185
182
  end
186
183
  end
@@ -190,7 +187,7 @@ describe Olfactory::Template do
190
187
  end
191
188
  context "given a name & block value" do
192
189
  subject do
193
- Olfactory.build_template :widget do |t|
190
+ Olfactory.build :widget do |t|
194
191
  t.doodad :foo do
195
192
  value
196
193
  end
@@ -202,7 +199,7 @@ describe Olfactory::Template do
202
199
  end
203
200
  context "given no name" do
204
201
  subject do
205
- Olfactory.build_template :widget do |t|
202
+ Olfactory.build :widget do |t|
206
203
  t.doodad
207
204
  end
208
205
  end
@@ -212,7 +209,7 @@ describe Olfactory::Template do
212
209
  end
213
210
  context "given no name & numeric value" do
214
211
  subject do
215
- Olfactory.build_template :widget do |t|
212
+ Olfactory.build :widget do |t|
216
213
  t.doodad 1
217
214
  end
218
215
  end
@@ -226,7 +223,7 @@ describe Olfactory::Template do
226
223
  context "invoked as plural" do
227
224
  context "given a hash value" do
228
225
  subject do
229
- Olfactory.build_template :widget do |t|
226
+ Olfactory.build :widget do |t|
230
227
  t.doodads :a => value
231
228
  end
232
229
  end
@@ -236,7 +233,7 @@ describe Olfactory::Template do
236
233
  end
237
234
  context "given no name or value" do
238
235
  subject do
239
- Olfactory.build_template :widget do |t|
236
+ Olfactory.build :widget do |t|
240
237
  t.doodads
241
238
  end
242
239
  end
@@ -247,7 +244,7 @@ describe Olfactory::Template do
247
244
  end
248
245
  context "given sequential invocations" do
249
246
  subject do
250
- Olfactory.build_template :widget do |t|
247
+ Olfactory.build :widget do |t|
251
248
  t.doodad :a, value
252
249
  t.doodads :b => value, :c => value
253
250
  t.doodad :d, value
@@ -261,7 +258,7 @@ describe Olfactory::Template do
261
258
  end
262
259
  end
263
260
  context "with an alias" do
264
- before do
261
+ before(:context) do
265
262
  Olfactory.template :widget do |t|
266
263
  t.has_many :doodads, :singular => :doodad,
267
264
  :named => :true,
@@ -270,7 +267,7 @@ describe Olfactory::Template do
270
267
  end
271
268
  context "given a hash" do
272
269
  subject do
273
- Olfactory.build_template :widget do |t|
270
+ Olfactory.build :widget do |t|
274
271
  t.foos :a => value
275
272
  end
276
273
  end
@@ -280,7 +277,7 @@ describe Olfactory::Template do
280
277
  end
281
278
  context "given a name and basic value" do
282
279
  subject do
283
- Olfactory.build_template :widget do |t|
280
+ Olfactory.build :widget do |t|
284
281
  t.foos :a, value
285
282
  end
286
283
  end
@@ -293,7 +290,7 @@ describe Olfactory::Template do
293
290
  end
294
291
  end
295
292
  context "embeds_one" do
296
- before do
293
+ before(:context) do
297
294
  Olfactory.template :widget do |t|
298
295
  t.embeds_one :doodad
299
296
  end
@@ -305,7 +302,7 @@ describe Olfactory::Template do
305
302
 
306
303
  context "given no value" do
307
304
  subject do
308
- Olfactory.build_template :widget do |t|
305
+ Olfactory.build :widget do |t|
309
306
  t.doodad
310
307
  end
311
308
  end
@@ -314,7 +311,7 @@ describe Olfactory::Template do
314
311
  end
315
312
  end
316
313
  context "given a symbol value" do
317
- before do
314
+ before(:context) do
318
315
  Olfactory.template :widget do |t|
319
316
  t.embeds_one :doodad
320
317
  end
@@ -327,7 +324,7 @@ describe Olfactory::Template do
327
324
  end
328
325
  context "that matches a defined preset" do
329
326
  subject do
330
- Olfactory.build_template :widget do |t|
327
+ Olfactory.build :widget do |t|
331
328
  t.doodad :shiny
332
329
  end
333
330
  end
@@ -338,7 +335,7 @@ describe Olfactory::Template do
338
335
  end
339
336
  context "that does not match a defined preset" do
340
337
  subject do
341
- Olfactory.build_template :widget do |t|
338
+ Olfactory.build :widget do |t|
342
339
  t.doodad :rusty
343
340
  end
344
341
  end
@@ -350,7 +347,7 @@ describe Olfactory::Template do
350
347
  end
351
348
  context "given a block value" do
352
349
  subject do
353
- Olfactory.build_template :widget do |t|
350
+ Olfactory.build :widget do |t|
354
351
  t.doodad { |d| d.gizmo value }
355
352
  end
356
353
  end
@@ -359,7 +356,7 @@ describe Olfactory::Template do
359
356
  end
360
357
  end
361
358
  context "with an alias" do
362
- before do
359
+ before(:context) do
363
360
  Olfactory.template :widget do |t|
364
361
  t.embeds_one :doodad, :alias => :foo
365
362
  end
@@ -369,7 +366,7 @@ describe Olfactory::Template do
369
366
  end
370
367
  context "given no value" do
371
368
  subject do
372
- Olfactory.build_template :widget do |t|
369
+ Olfactory.build :widget do |t|
373
370
  t.foo
374
371
  end
375
372
  end
@@ -379,7 +376,7 @@ describe Olfactory::Template do
379
376
  end
380
377
  end
381
378
  context "with template name" do
382
- before do
379
+ before(:context) do
383
380
  Olfactory.template :widget do |t|
384
381
  t.embeds_one :doodad, :template => :thingamabob
385
382
  end
@@ -389,7 +386,7 @@ describe Olfactory::Template do
389
386
  end
390
387
  context "given no value" do
391
388
  subject do
392
- Olfactory.build_template :widget do |t|
389
+ Olfactory.build :widget do |t|
393
390
  t.doodad
394
391
  end
395
392
  end
@@ -400,7 +397,7 @@ describe Olfactory::Template do
400
397
  end
401
398
  end
402
399
  context "embeds_many" do
403
- before do
400
+ before(:context) do
404
401
  Olfactory.template :widget do |t|
405
402
  t.embeds_many :doodads
406
403
  end
@@ -411,7 +408,7 @@ describe Olfactory::Template do
411
408
  let(:value) { "gizmo" }
412
409
 
413
410
  context "as generic collection" do
414
- before do
411
+ before(:context) do
415
412
  Olfactory.template :widget do |t|
416
413
  t.embeds_many :doodads, :singular => :doodad
417
414
  end
@@ -422,7 +419,7 @@ describe Olfactory::Template do
422
419
  context "invoked as singular" do
423
420
  context "given no value" do
424
421
  subject do
425
- Olfactory.build_template :widget do |t|
422
+ Olfactory.build :widget do |t|
426
423
  t.doodad
427
424
  end
428
425
  end
@@ -431,7 +428,7 @@ describe Olfactory::Template do
431
428
  end
432
429
  end
433
430
  context "given a symbol value" do
434
- before do
431
+ before(:context) do
435
432
  Olfactory.template :widget do |t|
436
433
  t.embeds_many :doodads, :singular => :doodad
437
434
  end
@@ -444,7 +441,7 @@ describe Olfactory::Template do
444
441
  end
445
442
  context "that matches a defined preset" do
446
443
  subject do
447
- Olfactory.build_template :widget do |t|
444
+ Olfactory.build :widget do |t|
448
445
  t.doodad :shiny
449
446
  end
450
447
  end
@@ -455,7 +452,7 @@ describe Olfactory::Template do
455
452
  end
456
453
  context "that does not match a defined preset" do
457
454
  subject do
458
- Olfactory.build_template :widget do |t|
455
+ Olfactory.build :widget do |t|
459
456
  t.doodad :rusty
460
457
  end
461
458
  end
@@ -467,7 +464,7 @@ describe Olfactory::Template do
467
464
  end
468
465
  context "given a block value" do
469
466
  subject do
470
- Olfactory.build_template :widget do |t|
467
+ Olfactory.build :widget do |t|
471
468
  t.doodad { |d| d.gizmo value }
472
469
  end
473
470
  end
@@ -479,7 +476,7 @@ describe Olfactory::Template do
479
476
  context "invoked as plural" do
480
477
  context "given an integer value" do
481
478
  subject do
482
- Olfactory.build_template :widget do |t|
479
+ Olfactory.build :widget do |t|
483
480
  t.doodads 2
484
481
  end
485
482
  end
@@ -488,7 +485,7 @@ describe Olfactory::Template do
488
485
  end
489
486
  end
490
487
  context "given a symbol & integer value" do
491
- before do
488
+ before(:context) do
492
489
  Olfactory.template :widget do |t|
493
490
  t.embeds_many :doodads, :singular => :doodad
494
491
  end
@@ -501,7 +498,7 @@ describe Olfactory::Template do
501
498
  end
502
499
  context "that matches a defined preset" do
503
500
  subject do
504
- Olfactory.build_template :widget do |t|
501
+ Olfactory.build :widget do |t|
505
502
  t.doodads :shiny, 2
506
503
  end
507
504
  end
@@ -512,7 +509,7 @@ describe Olfactory::Template do
512
509
  end
513
510
  context "that does not match a defined preset" do
514
511
  subject do
515
- Olfactory.build_template :widget do |t|
512
+ Olfactory.build :widget do |t|
516
513
  t.doodads :rusty, 2
517
514
  end
518
515
  end
@@ -523,7 +520,7 @@ describe Olfactory::Template do
523
520
  end
524
521
  end
525
522
  context "given an integer & symbol value" do
526
- before do
523
+ before(:context) do
527
524
  Olfactory.template :widget do |t|
528
525
  t.embeds_many :doodads, :singular => :doodad
529
526
  end
@@ -536,7 +533,7 @@ describe Olfactory::Template do
536
533
  end
537
534
  context "that matches a defined preset" do
538
535
  subject do
539
- Olfactory.build_template :widget do |t|
536
+ Olfactory.build :widget do |t|
540
537
  t.doodads 2, :shiny
541
538
  end
542
539
  end
@@ -547,7 +544,7 @@ describe Olfactory::Template do
547
544
  end
548
545
  context "that does not match a defined preset" do
549
546
  subject do
550
- Olfactory.build_template :widget do |t|
547
+ Olfactory.build :widget do |t|
551
548
  t.doodads 2, :rusty
552
549
  end
553
550
  end
@@ -559,7 +556,7 @@ describe Olfactory::Template do
559
556
  end
560
557
  context "given an integer and block value" do
561
558
  subject do
562
- Olfactory.build_template :widget do |t|
559
+ Olfactory.build :widget do |t|
563
560
  t.doodads 2 do |d|
564
561
  d.gizmo value
565
562
  end
@@ -572,7 +569,7 @@ describe Olfactory::Template do
572
569
  end
573
570
  context "given sequential invocations" do
574
571
  subject do
575
- Olfactory.build_template :widget do |t|
572
+ Olfactory.build :widget do |t|
576
573
  t.doodad
577
574
  t.doodads 2
578
575
  t.doodad
@@ -583,7 +580,7 @@ describe Olfactory::Template do
583
580
  end
584
581
  end
585
582
  context "with an alias" do
586
- before do
583
+ before(:context) do
587
584
  Olfactory.template :widget do |t|
588
585
  t.embeds_many :doodads, :singular => :doodad, :alias => :foos
589
586
  end
@@ -593,7 +590,7 @@ describe Olfactory::Template do
593
590
  end
594
591
  context "given an integer value" do
595
592
  subject do
596
- Olfactory.build_template :widget do |t|
593
+ Olfactory.build :widget do |t|
597
594
  t.foos 2
598
595
  end
599
596
  end
@@ -603,7 +600,7 @@ describe Olfactory::Template do
603
600
  end
604
601
  context "given a block value" do
605
602
  subject do
606
- Olfactory.build_template :widget do |t|
603
+ Olfactory.build :widget do |t|
607
604
  t.foos { |f| f.gizmo value }
608
605
  end
609
606
  end
@@ -615,7 +612,7 @@ describe Olfactory::Template do
615
612
  end
616
613
  end
617
614
  context "as named collection" do
618
- before do
615
+ before(:context) do
619
616
  Olfactory.template :widget do |t|
620
617
  t.embeds_many :doodads, :singular => :doodad,
621
618
  :named => true
@@ -627,7 +624,7 @@ describe Olfactory::Template do
627
624
  context "invoked as singular" do
628
625
  context "given a name & no value" do
629
626
  subject do
630
- Olfactory.build_template :widget do |t|
627
+ Olfactory.build :widget do |t|
631
628
  t.doodad :a
632
629
  end
633
630
  end
@@ -636,7 +633,7 @@ describe Olfactory::Template do
636
633
  end
637
634
  end
638
635
  context "given a name & symbol value" do
639
- before do
636
+ before(:context) do
640
637
  Olfactory.template :widget do |t|
641
638
  t.embeds_many :doodads, :singular => :doodad,
642
639
  :named => true
@@ -650,7 +647,7 @@ describe Olfactory::Template do
650
647
  end
651
648
  context "that matches a defined preset" do
652
649
  subject do
653
- Olfactory.build_template :widget do |t|
650
+ Olfactory.build :widget do |t|
654
651
  t.doodad :a, :shiny
655
652
  end
656
653
  end
@@ -661,7 +658,7 @@ describe Olfactory::Template do
661
658
  end
662
659
  context "that does not match a defined preset" do
663
660
  subject do
664
- Olfactory.build_template :widget do |t|
661
+ Olfactory.build :widget do |t|
665
662
  t.doodad :a, :rusty
666
663
  end
667
664
  end
@@ -673,7 +670,7 @@ describe Olfactory::Template do
673
670
  end
674
671
  context "given a name & block value" do
675
672
  subject do
676
- Olfactory.build_template :widget do |t|
673
+ Olfactory.build :widget do |t|
677
674
  t.doodad :a do |d|
678
675
  d.gizmo value
679
676
  end
@@ -685,7 +682,7 @@ describe Olfactory::Template do
685
682
  end
686
683
  context "given a no name" do
687
684
  subject do
688
- Olfactory.build_template :widget do |t|
685
+ Olfactory.build :widget do |t|
689
686
  t.doodad
690
687
  end
691
688
  end
@@ -695,7 +692,7 @@ describe Olfactory::Template do
695
692
  end
696
693
  context "given a no name & numeric value" do
697
694
  subject do
698
- Olfactory.build_template :widget do |t|
695
+ Olfactory.build :widget do |t|
699
696
  t.doodad 1
700
697
  end
701
698
  end
@@ -707,7 +704,7 @@ describe Olfactory::Template do
707
704
  context "invoked as plural" do
708
705
  context "given a hash value" do
709
706
  subject do
710
- Olfactory.build_template :widget do |t|
707
+ Olfactory.build :widget do |t|
711
708
  t.doodads :a => value
712
709
  end
713
710
  end
@@ -718,7 +715,7 @@ describe Olfactory::Template do
718
715
  end
719
716
  end
720
717
  context "with template name" do
721
- before do
718
+ before(:context) do
722
719
  Olfactory.template :widget do |t|
723
720
  t.embeds_many :doodads, :singular => :doodad,
724
721
  :template => :thingamabob
@@ -729,7 +726,7 @@ describe Olfactory::Template do
729
726
  end
730
727
  context "given no value" do
731
728
  subject do
732
- Olfactory.build_template :widget do |t|
729
+ Olfactory.build :widget do |t|
733
730
  t.doodad
734
731
  end
735
732
  end
@@ -739,8 +736,408 @@ describe Olfactory::Template do
739
736
  end
740
737
  end
741
738
  end
739
+ context "sequence" do
740
+ context "with a scope" do
741
+ context "bound to the template" do
742
+ context do
743
+ before(:example) do
744
+ Olfactory.template :building do |t|
745
+ t.has_one :address
746
+ t.sequence :address, :scope => :template do |n, options|
747
+ "#{(n + (n % 2)) + 2} #{"#{options[:prefix]} " if options[:prefix]}BROADWAY"
748
+ end
749
+ end
750
+ end
751
+ context "given nothing" do
752
+ subject do
753
+ Olfactory.build :building do |building|
754
+ building.address building.generate(:address)
755
+ end
756
+ end
757
+ it { expect(subject[:address]).to eq("2 BROADWAY") }
758
+ end
759
+ context "given options" do
760
+ subject do
761
+ Olfactory.build :building do |building|
762
+ building.address building.generate(:address, :prefix => "WEST")
763
+ end
764
+ end
765
+ it { expect(subject[:address]).to eq("2 WEST BROADWAY") }
766
+ end
767
+ context "given a block" do
768
+ subject do
769
+ Olfactory.build :building do |building|
770
+ building.address do
771
+ building.generate(:address) do |n|
772
+ "#{(n + (n % 2)) + 2} JOHN STREET"
773
+ end
774
+ end
775
+ end
776
+ end
777
+ it { expect(subject[:address]).to eq("2 JOHN STREET") }
778
+ end
779
+ context "given options and a block" do
780
+ subject do
781
+ Olfactory.build :building do |building|
782
+ building.address do
783
+ building.generate(:address, :suffix => "ROAD") do |n, options|
784
+ "#{(n + (n % 2)) + 2} JOHN#{options[:suffix] ? " #{options[:suffix]}" : " STREET"}"
785
+ end
786
+ end
787
+ end
788
+ end
789
+ it { expect(subject[:address]).to eq("2 JOHN ROAD") }
790
+ end
791
+ end
792
+ context "given sequential invocations" do
793
+ before(:context) do
794
+ Olfactory.template :building do |t|
795
+ t.has_one :address
796
+ t.sequence :address, :scope => :template do |n, options|
797
+ "#{(n + (n % 2)) + 2} #{"#{options[:prefix]} " if options[:prefix]}BROADWAY"
798
+ end
799
+ end
800
+ end
801
+ let(:building_one) do
802
+ Olfactory.build :building do |building|
803
+ building.address { building.generate(:address) }
804
+ end
805
+ end
806
+ it { expect(building_one[:address]).to eq("2 BROADWAY") }
807
+ let(:building_two) do
808
+ Olfactory.build :building do |building|
809
+ building.address { building.generate(:address) }
810
+ end
811
+ end
812
+ it { expect(building_two[:address]).to eq("4 BROADWAY") }
813
+ end
814
+ end
815
+ context "bound to the instance" do
816
+ context do
817
+ before(:example) do
818
+ Olfactory.template :building do |t|
819
+ t.has_one :address
820
+ t.sequence :address, :scope => :instance do |n, options|
821
+ "#{(n + (n % 2)) + 2} #{"#{options[:prefix]} " if options[:prefix]}BROADWAY"
822
+ end
823
+ end
824
+ end
825
+ context "given nothing" do
826
+ subject do
827
+ Olfactory.build :building do |building|
828
+ building.address building.generate(:address)
829
+ end
830
+ end
831
+ it { expect(subject[:address]).to eq("2 BROADWAY") }
832
+ end
833
+ context "given options" do
834
+ subject do
835
+ Olfactory.build :building do |building|
836
+ building.address building.generate(:address, :prefix => "WEST")
837
+ end
838
+ end
839
+ it { expect(subject[:address]).to eq("2 WEST BROADWAY") }
840
+ end
841
+ context "given a block" do
842
+ subject do
843
+ Olfactory.build :building do |building|
844
+ building.address do
845
+ building.generate(:address) do |n|
846
+ "#{(n + (n % 2)) + 2} JOHN STREET"
847
+ end
848
+ end
849
+ end
850
+ end
851
+ it { expect(subject[:address]).to eq("2 JOHN STREET") }
852
+ end
853
+ context "given options and a block" do
854
+ subject do
855
+ Olfactory.build :building do |building|
856
+ building.address do
857
+ building.generate(:address, :suffix => "ROAD") do |n, options|
858
+ "#{(n + (n % 2)) + 2} JOHN#{options[:suffix] ? " #{options[:suffix]}" : " STREET"}"
859
+ end
860
+ end
861
+ end
862
+ end
863
+ it { expect(subject[:address]).to eq("2 JOHN ROAD") }
864
+ end
865
+ end
866
+ context "given sequential invocations" do
867
+ before(:context) do
868
+ Olfactory.template :building do |t|
869
+ t.has_one :address
870
+ t.has_one :other_address
871
+ t.sequence :address, :scope => :instance do |n, options|
872
+ "#{(n + (n % 2)) + 2} #{"#{options[:prefix]} " if options[:prefix]}BROADWAY"
873
+ end
874
+ end
875
+ end
876
+ let(:building_one) do
877
+ Olfactory.build :building do |building|
878
+ building.address building.generate(:address)
879
+ building.other_address building.generate(:address)
880
+ end
881
+ end
882
+ it { expect(building_one[:address]).to eq("2 BROADWAY") }
883
+ it { expect(building_one[:other_address]).to eq("4 BROADWAY") }
884
+ let(:building_two) do
885
+ Olfactory.build :building do |building|
886
+ building.address building.generate(:address)
887
+ building.other_address building.generate(:address)
888
+ end
889
+ end
890
+ it { expect(building_two[:address]).to eq("2 BROADWAY") }
891
+ it { expect(building_two[:other_address]).to eq("4 BROADWAY") }
892
+ end
893
+ end
894
+ end
895
+ context "after being invoked" do
896
+ before(:context) do
897
+ Olfactory.template :numbers do |t|
898
+ t.has_one :even_count
899
+ t.has_one :odd_count
900
+ t.sequence :even, :scope => :template do |n|
901
+ (n + (n % 2)) + 2
902
+ end
903
+ t.sequence :odd, :scope => :template do |n|
904
+ (n + (n % 2)) + 1
905
+ end
906
+ end
907
+ end
908
+ let(:first_set) do
909
+ Olfactory.build :numbers do |n|
910
+ n.even_count n.generate(:even)
911
+ n.even_count n.generate(:even)
912
+ n.odd_count n.generate(:odd)
913
+ n.odd_count n.generate(:odd)
914
+ end
915
+ end
916
+ it do
917
+ expect(first_set[:even_count]).to eq(4)
918
+ expect(first_set[:odd_count]).to eq(3)
919
+ end
920
+ context "and called to reset" do
921
+ before(:context) do
922
+ Olfactory.reset_template_sequences(:numbers)
923
+ end
924
+ let(:second_set) do
925
+ Olfactory.build :numbers do |n|
926
+ n.even_count n.generate(:even)
927
+ n.even_count n.generate(:even)
928
+ n.odd_count n.generate(:odd)
929
+ n.odd_count n.generate(:odd)
930
+ end
931
+ end
932
+ it do
933
+ expect(second_set[:even_count]).to eq(4)
934
+ expect(second_set[:odd_count]).to eq(3)
935
+ end
936
+ end
937
+ end
938
+ end
939
+ context "dictionary" do
940
+ context "with a scope" do
941
+ context "bound to the template" do
942
+ context do
943
+ before(:context) do
944
+ Olfactory.template :address do |t|
945
+ t.has_one :street_code
946
+ t.dictionary :street_codes, :scope => :template
947
+ end
948
+ end
949
+ subject do
950
+ Olfactory.build :address do |address|
951
+ address.street_code do
952
+ address.street_codes["BROADWAY"] ||= 10001
953
+ end
954
+ end
955
+ end
956
+ it { expect(subject[:street_code]).to eq(10001) }
957
+ end
958
+ context "given sequential invocations" do
959
+ before(:context) do
960
+ Olfactory.template :address do |t|
961
+ t.has_one :street_code
962
+ t.dictionary :street_codes, :scope => :template
963
+ t.sequence :street_code, :scope => :template, :seed => 1 do |n|
964
+ (10000 + n)
965
+ end
966
+ end
967
+ end
968
+ let!(:address_one) do
969
+ Olfactory.build :address do |address|
970
+ address.street_code { address.street_codes["BROADWAY"] ||= address.generate(:street_code) }
971
+ end
972
+ end
973
+ let!(:address_two) do
974
+ Olfactory.build :address do |address|
975
+ address.street_code { address.street_codes["BROADWAY"] ||= address.generate(:street_code) }
976
+ end
977
+ end
978
+ let!(:address_three) do
979
+ Olfactory.build :address do |address|
980
+ address.street_code { address.street_codes["JOHN STREET"] ||= address.generate(:street_code) }
981
+ end
982
+ end
983
+
984
+ it { expect(address_one[:street_code]).to eq(10001) }
985
+ it { expect(address_two[:street_code]).to eq(10001) }
986
+ it { expect(address_three[:street_code]).to eq(10002) }
987
+ end
988
+ end
989
+ context "bound to the instance" do
990
+ context do
991
+ before(:context) do
992
+ Olfactory.template :address do |t|
993
+ t.has_one :street_code
994
+ t.has_one :other_street_code
995
+ t.has_one :another_street_code
996
+ t.dictionary :street_codes, :scope => :instance
997
+ end
998
+ end
999
+ subject do
1000
+ Olfactory.build :address do |address|
1001
+ address.street_code { address.street_codes["BROADWAY"] ||= 10001 }
1002
+ address.other_street_code { address.street_codes["BROADWAY"] ||= 10002 }
1003
+ address.another_street_code { address.street_codes["JOHN STREET"] ||= 10003 }
1004
+ end
1005
+ end
1006
+ it { expect(subject[:street_code]).to eq(10001) }
1007
+ it { expect(subject[:other_street_code]).to eq(10001) }
1008
+ it { expect(subject[:another_street_code]).to eq(10003) }
1009
+ end
1010
+ context "given sequential invocations" do
1011
+ before(:context) do
1012
+ Olfactory.template :address do |t|
1013
+ t.has_one :street_code
1014
+ t.has_one :other_street_code
1015
+ t.has_one :another_street_code
1016
+ t.dictionary :street_codes, :scope => :instance
1017
+ t.sequence :street_code, :scope => :instance, :seed => 1 do |n|
1018
+ (10000 + n)
1019
+ end
1020
+ end
1021
+ end
1022
+ let!(:address_one) do
1023
+ Olfactory.build :address do |address|
1024
+ address.street_code { address.street_codes["BROADWAY"] ||= address.generate(:street_code) }
1025
+ address.other_street_code { address.street_codes["BROADWAY"] ||= address.generate(:street_code) }
1026
+ address.another_street_code { address.street_codes["JOHN STREET"] ||= address.generate(:street_code) }
1027
+ end
1028
+ end
1029
+ let!(:address_two) do
1030
+ Olfactory.build :address do |address|
1031
+ address.street_code { address.street_codes["BROADWAY"] ||= address.generate(:street_code) }
1032
+ address.other_street_code { address.street_codes["BROADWAY"] ||= address.generate(:street_code) }
1033
+ address.another_street_code { address.street_codes["JOHN STREET"] ||= address.generate(:street_code) }
1034
+ end
1035
+ end
1036
+ it { expect(address_one[:street_code]).to eq(10001) }
1037
+ it { expect(address_one[:other_street_code]).to eq(10001) }
1038
+ it { expect(address_one[:another_street_code]).to eq(10002) }
1039
+ it { expect(address_two[:street_code]).to eq(10001) }
1040
+ it { expect(address_two[:other_street_code]).to eq(10001) }
1041
+ it { expect(address_two[:another_street_code]).to eq(10002) }
1042
+ end
1043
+ end
1044
+ end
1045
+ context "after being invoked" do
1046
+ before(:context) do
1047
+ Olfactory.template :stuff do |t|
1048
+ t.has_one :widget
1049
+ t.has_one :another_widget
1050
+ t.has_one :doodad
1051
+ t.has_one :another_doodad
1052
+ t.dictionary :widget_catalog, :scope => :template
1053
+ t.dictionary :doodad_catalog, :scope => :template
1054
+ end
1055
+ end
1056
+ let(:first_set) do
1057
+ Olfactory.build :stuff do |n|
1058
+ n.widget do
1059
+ if n.widget_catalog["Widget"]
1060
+ "Existing widget!"
1061
+ else
1062
+ n.widget_catalog["Widget"] = "New widget!"
1063
+ end
1064
+ end
1065
+ n.another_widget do
1066
+ if n.widget_catalog["Widget"]
1067
+ "Existing widget!"
1068
+ else
1069
+ n.widget_catalog["Widget"] = "New widget!"
1070
+ end
1071
+ end
1072
+ n.doodad do
1073
+ if n.doodad_catalog["Doodad"]
1074
+ "Existing doodad!"
1075
+ else
1076
+ n.doodad_catalog["Doodad"] = "New doodad!"
1077
+ end
1078
+ end
1079
+ n.another_doodad do
1080
+ if n.doodad_catalog["Doodad"]
1081
+ "Existing doodad!"
1082
+ else
1083
+ n.doodad_catalog["Doodad"] = "New doodad!"
1084
+ end
1085
+ end
1086
+ end
1087
+ end
1088
+ it do
1089
+ expect(first_set[:widget]).to eq("New widget!")
1090
+ expect(first_set[:another_widget]).to eq("Existing widget!")
1091
+ expect(first_set[:doodad]).to eq("New doodad!")
1092
+ expect(first_set[:another_doodad]).to eq("Existing doodad!")
1093
+ end
1094
+ context "and called to reset" do
1095
+ before(:context) do
1096
+ Olfactory.reset_template_dictionaries(:stuff)
1097
+ end
1098
+ let(:second_set) do
1099
+ Olfactory.build :stuff do |n|
1100
+ n.widget do
1101
+ if n.widget_catalog["Widget"]
1102
+ "Existing widget!"
1103
+ else
1104
+ n.widget_catalog["Widget"] = "New widget!"
1105
+ end
1106
+ end
1107
+ n.another_widget do
1108
+ if n.widget_catalog["Widget"]
1109
+ "Existing widget!"
1110
+ else
1111
+ n.widget_catalog["Widget"] = "New widget!"
1112
+ end
1113
+ end
1114
+ n.doodad do
1115
+ if n.doodad_catalog["Doodad"]
1116
+ "Existing doodad!"
1117
+ else
1118
+ n.doodad_catalog["Doodad"] = "New doodad!"
1119
+ end
1120
+ end
1121
+ n.another_doodad do
1122
+ if n.doodad_catalog["Doodad"]
1123
+ "Existing doodad!"
1124
+ else
1125
+ n.doodad_catalog["Doodad"] = "New doodad!"
1126
+ end
1127
+ end
1128
+ end
1129
+ end
1130
+ it do
1131
+ expect(second_set[:widget]).to eq("New widget!")
1132
+ expect(second_set[:another_widget]).to eq("Existing widget!")
1133
+ expect(second_set[:doodad]).to eq("New doodad!")
1134
+ expect(second_set[:another_doodad]).to eq("Existing doodad!")
1135
+ end
1136
+ end
1137
+ end
1138
+ end
742
1139
  context "macros" do
743
- before do
1140
+ before(:context) do
744
1141
  Olfactory.template :widget do |t|
745
1142
  t.has_one :doodad
746
1143
  t.macro :make_shiny do |m, v|
@@ -751,7 +1148,7 @@ describe Olfactory::Template do
751
1148
  let(:value) { "doodad" }
752
1149
 
753
1150
  subject do
754
- Olfactory.build_template :widget do |t|
1151
+ Olfactory.build :widget do |t|
755
1152
  t.doodad value
756
1153
  t.make_shiny
757
1154
  end
@@ -761,7 +1158,7 @@ describe Olfactory::Template do
761
1158
  end
762
1159
 
763
1160
  context "with parameters" do
764
- before do
1161
+ before(:context) do
765
1162
  Olfactory.template :widget do |t|
766
1163
  t.has_one :doodad
767
1164
  t.macro :make do |m, adj, adj2|
@@ -770,7 +1167,7 @@ describe Olfactory::Template do
770
1167
  end
771
1168
  end
772
1169
  subject do
773
- Olfactory.build_template :widget do |t|
1170
+ Olfactory.build :widget do |t|
774
1171
  t.doodad value
775
1172
  t.make :very, :shiny
776
1173
  end
@@ -781,7 +1178,7 @@ describe Olfactory::Template do
781
1178
  end
782
1179
  end
783
1180
  context "transients" do
784
- before do
1181
+ before(:context) do
785
1182
  Olfactory.template :widget do |t|
786
1183
  t.has_one :doodad
787
1184
  end
@@ -789,7 +1186,7 @@ describe Olfactory::Template do
789
1186
  let(:value) { "temporary value"}
790
1187
  context "when set" do
791
1188
  subject do
792
- Olfactory.build_template :widget do |t|
1189
+ Olfactory.build :widget do |t|
793
1190
  t.transient :foo, value
794
1191
  end
795
1192
  end
@@ -799,7 +1196,7 @@ describe Olfactory::Template do
799
1196
  end
800
1197
  context "when read" do
801
1198
  subject do
802
- Olfactory.build_template :widget do |t|
1199
+ Olfactory.build :widget do |t|
803
1200
  t.transient :foo, value
804
1201
  t.doodad "#{t.transients[:foo]}"
805
1202
  end
@@ -809,7 +1206,7 @@ describe Olfactory::Template do
809
1206
  end
810
1207
  end
811
1208
  context "when inherited" do
812
- before do
1209
+ before(:context) do
813
1210
  Olfactory.template :widget do |t|
814
1211
  t.embeds_one :doodad
815
1212
  end
@@ -818,7 +1215,7 @@ describe Olfactory::Template do
818
1215
  end
819
1216
  end
820
1217
  subject do
821
- Olfactory.build_template :widget do |t|
1218
+ Olfactory.build :widget do |t|
822
1219
  t.transient :foo, value
823
1220
  t.doodad
824
1221
  end
@@ -832,7 +1229,7 @@ describe Olfactory::Template do
832
1229
  let(:value) { "doodad" }
833
1230
  let(:default_value) { "default doodad" }
834
1231
  context "before" do
835
- before do
1232
+ before(:context) do
836
1233
  Olfactory.template :widget do |t|
837
1234
  t.has_one :doodad
838
1235
  t.has_one :other_doodad
@@ -843,7 +1240,7 @@ describe Olfactory::Template do
843
1240
  end
844
1241
  end
845
1242
  subject do
846
- Olfactory.build_template :widget do |t|
1243
+ Olfactory.build :widget do |t|
847
1244
  t.doodad value
848
1245
  end
849
1246
  end
@@ -855,7 +1252,7 @@ describe Olfactory::Template do
855
1252
  end
856
1253
  end
857
1254
  context "after" do
858
- before do
1255
+ before(:context) do
859
1256
  Olfactory.template :widget do |t|
860
1257
  t.has_one :doodad
861
1258
  t.has_one :other_doodad
@@ -866,7 +1263,7 @@ describe Olfactory::Template do
866
1263
  end
867
1264
  end
868
1265
  subject do
869
- Olfactory.build_template :widget do |t|
1266
+ Olfactory.build :widget do |t|
870
1267
  t.doodad value
871
1268
  end
872
1269
  end
@@ -881,14 +1278,14 @@ describe Olfactory::Template do
881
1278
  context "when created" do
882
1279
  let(:value) { "saveable string" }
883
1280
  context "containing a saveable object" do
884
- before do
1281
+ before(:context) do
885
1282
  Olfactory.template :widget do |t|
886
1283
  t.has_one :doodad
887
1284
  end
888
1285
  end
889
1286
 
890
1287
  subject do
891
- Olfactory.create_template :widget do |w|
1288
+ Olfactory.create :widget do |w|
892
1289
  w.doodad SaveableString.new(value)
893
1290
  end
894
1291
  end
@@ -897,13 +1294,13 @@ describe Olfactory::Template do
897
1294
  end
898
1295
  end
899
1296
  context "containing a generic collection of saveable objects" do
900
- before do
1297
+ before(:context) do
901
1298
  Olfactory.template :widget do |t|
902
1299
  t.has_many :doodads
903
1300
  end
904
1301
  end
905
1302
  subject do
906
- Olfactory.create_template :widget do |w|
1303
+ Olfactory.create :widget do |w|
907
1304
  w.doodads SaveableString.new(value), SaveableString.new(value)
908
1305
  end
909
1306
  end
@@ -911,13 +1308,13 @@ describe Olfactory::Template do
911
1308
  it { expect(subject[:doodads].last.saved?).to be true }
912
1309
  end
913
1310
  context "containing a named collection of saveable objects" do
914
- before do
1311
+ before(:context) do
915
1312
  Olfactory.template :widget do |t|
916
1313
  t.has_many :doodads, :named => true
917
1314
  end
918
1315
  end
919
1316
  subject do
920
- Olfactory.create_template :widget do |w|
1317
+ Olfactory.create :widget do |w|
921
1318
  w.doodads :a => SaveableString.new(value), :b => SaveableString.new(value)
922
1319
  end
923
1320
  end
@@ -925,32 +1322,32 @@ describe Olfactory::Template do
925
1322
  it { expect(subject[:doodads][:b].saved?).to be true }
926
1323
  end
927
1324
  context "with an embedded template" do
928
- before do
1325
+ before(:context) do
929
1326
  Olfactory.template :widget do |t|
930
1327
  t.embeds_one :doodad
931
1328
  end
932
1329
  end
933
1330
  context "containing a saveable object" do
934
- before do
1331
+ before(:context) do
935
1332
  Olfactory.template :doodad do |t|
936
1333
  t.has_one :gizmo
937
1334
  end
938
1335
  end
939
1336
  subject do
940
- Olfactory.create_template :widget do |w|
1337
+ Olfactory.create :widget do |w|
941
1338
  w.doodad { |d| d.gizmo SaveableString.new(value) }
942
1339
  end
943
1340
  end
944
1341
  it { expect(subject[:doodad][:gizmo].saved?).to be true }
945
1342
  end
946
1343
  context "containing a generic collection of saveable objects" do
947
- before do
1344
+ before(:context) do
948
1345
  Olfactory.template :doodad do |t|
949
1346
  t.has_many :gizmos
950
1347
  end
951
1348
  end
952
1349
  subject do
953
- Olfactory.create_template :widget do |w|
1350
+ Olfactory.create :widget do |w|
954
1351
  w.doodad { |d| d.gizmos SaveableString.new(value), SaveableString.new(value) }
955
1352
  end
956
1353
  end
@@ -958,13 +1355,13 @@ describe Olfactory::Template do
958
1355
  it { expect(subject[:doodad][:gizmos].last.saved?).to be true }
959
1356
  end
960
1357
  context "containing a named collection of saveable objects" do
961
- before do
1358
+ before(:context) do
962
1359
  Olfactory.template :doodad do |t|
963
1360
  t.has_many :gizmos, :named => true
964
1361
  end
965
1362
  end
966
1363
  subject do
967
- Olfactory.create_template :widget do |w|
1364
+ Olfactory.create :widget do |w|
968
1365
  w.doodad { |d| d.gizmos :a => SaveableString.new(value), :b => SaveableString.new(value) }
969
1366
  end
970
1367
  end
@@ -973,32 +1370,32 @@ describe Olfactory::Template do
973
1370
  end
974
1371
  end
975
1372
  context "with a generic collection of embedded templates" do
976
- before do
1373
+ before(:context) do
977
1374
  Olfactory.template :widget do |t|
978
1375
  t.embeds_many :doodads, :singular => :doodad
979
1376
  end
980
1377
  end
981
1378
  context "containing a saveable object" do
982
- before do
1379
+ before(:context) do
983
1380
  Olfactory.template :doodad do |t|
984
1381
  t.has_one :gizmo
985
1382
  end
986
1383
  end
987
1384
  subject do
988
- Olfactory.create_template :widget do |w|
1385
+ Olfactory.create :widget do |w|
989
1386
  w.doodad { |d| d.gizmo SaveableString.new(value) }
990
1387
  end
991
1388
  end
992
1389
  it { expect(subject[:doodads].first[:gizmo].saved?).to be true }
993
1390
  end
994
1391
  context "containing a generic collection of saveable objects" do
995
- before do
1392
+ before(:context) do
996
1393
  Olfactory.template :doodad do |t|
997
1394
  t.has_many :gizmos
998
1395
  end
999
1396
  end
1000
1397
  subject do
1001
- Olfactory.create_template :widget do |w|
1398
+ Olfactory.create :widget do |w|
1002
1399
  w.doodad { |d| d.gizmos SaveableString.new(value), SaveableString.new(value) }
1003
1400
  end
1004
1401
  end
@@ -1006,13 +1403,13 @@ describe Olfactory::Template do
1006
1403
  it { expect(subject[:doodads].first[:gizmos].last.saved?).to be true }
1007
1404
  end
1008
1405
  context "containing a named collection of saveable objects" do
1009
- before do
1406
+ before(:context) do
1010
1407
  Olfactory.template :doodad do |t|
1011
1408
  t.has_many :gizmos, :named => true
1012
1409
  end
1013
1410
  end
1014
1411
  subject do
1015
- Olfactory.create_template :widget do |w|
1412
+ Olfactory.create :widget do |w|
1016
1413
  w.doodad { |d| d.gizmos :a => SaveableString.new(value), :b => SaveableString.new(value) }
1017
1414
  end
1018
1415
  end
@@ -1021,19 +1418,19 @@ describe Olfactory::Template do
1021
1418
  end
1022
1419
  end
1023
1420
  context "with a named collection of embedded templates" do
1024
- before do
1421
+ before(:context) do
1025
1422
  Olfactory.template :widget do |t|
1026
1423
  t.embeds_many :doodads, :singular => :doodad, :named => true
1027
1424
  end
1028
1425
  end
1029
1426
  context "containing a saveable object" do
1030
- before do
1427
+ before(:context) do
1031
1428
  Olfactory.template :doodad do |t|
1032
1429
  t.has_one :gizmo
1033
1430
  end
1034
1431
  end
1035
1432
  subject do
1036
- Olfactory.create_template :widget do |w|
1433
+ Olfactory.create :widget do |w|
1037
1434
  w.doodad :one do |d|
1038
1435
  d.gizmo SaveableString.new(value)
1039
1436
  end
@@ -1042,13 +1439,13 @@ describe Olfactory::Template do
1042
1439
  it { expect(subject[:doodads][:one][:gizmo].saved?).to be true }
1043
1440
  end
1044
1441
  context "containing a generic collection of saveable objects" do
1045
- before do
1442
+ before(:context) do
1046
1443
  Olfactory.template :doodad do |t|
1047
1444
  t.has_many :gizmos
1048
1445
  end
1049
1446
  end
1050
1447
  subject do
1051
- Olfactory.create_template :widget do |w|
1448
+ Olfactory.create :widget do |w|
1052
1449
  w.doodad :one do |d|
1053
1450
  d.gizmos SaveableString.new(value), SaveableString.new(value)
1054
1451
  end
@@ -1058,13 +1455,13 @@ describe Olfactory::Template do
1058
1455
  it { expect(subject[:doodads][:one][:gizmos].last.saved?).to be true }
1059
1456
  end
1060
1457
  context "containing a named collection of saveable objects" do
1061
- before do
1458
+ before(:context) do
1062
1459
  Olfactory.template :doodad do |t|
1063
1460
  t.has_many :gizmos, :named => true
1064
1461
  end
1065
1462
  end
1066
1463
  subject do
1067
- Olfactory.create_template :widget do |w|
1464
+ Olfactory.create :widget do |w|
1068
1465
  w.doodad :one do |d|
1069
1466
  d.gizmos :a => SaveableString.new(value), :b => SaveableString.new(value)
1070
1467
  end