jackbox 0.9.6.3 → 0.9.6.6

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.
@@ -2,12 +2,12 @@ require "spec_helper"
2
2
 
3
3
  =begin rdoc
4
4
  This file contains the specs for the GOF Decorator Pattern and
5
- Strategy Pattern use cases for injectors.
5
+ Strategy Pattern use cases for traits.
6
6
  =end
7
7
 
8
8
  include Injectors
9
9
 
10
- describe 'some use cases', :injectors do
10
+ describe 'some use cases', :traits do
11
11
 
12
12
  describe 'GOF Decortors is one use of this codebase. Traditionally this is only partially solved in Ruby through PORO
13
13
  decorators or the use of modules with the problems of loss of class identity for the former
@@ -22,12 +22,12 @@ describe 'some use cases', :injectors do
22
22
  end
23
23
 
24
24
  # debugger
25
- injector :milk do
25
+ trait :milk do
26
26
  def cost
27
27
  super() + 0.30
28
28
  end
29
29
  end
30
- injector :vanilla do
30
+ trait :vanilla do
31
31
  def cost
32
32
  super() + 0.15
33
33
  end
@@ -59,7 +59,7 @@ describe 'some use cases', :injectors do
59
59
  end
60
60
 
61
61
  cup = Coffee.new.enrich(milk).enrich(vanilla).enrich(vanilla)
62
- cup.injectors.sym_list.should == [:vanilla, :vanilla, :milk]
62
+ cup.traits.sym_list.should == [:vanilla, :vanilla, :milk]
63
63
  cup.cost.should == 2.10
64
64
  cup.should be_instance_of(Coffee)
65
65
 
@@ -96,13 +96,13 @@ describe 'some use cases', :injectors do
96
96
  end
97
97
 
98
98
  cup.enrich(vanilla)
99
- cup.injectors.sym_list.should == [:vanilla, :vanilla, :milk]
99
+ cup.traits.sym_list.should == [:vanilla, :vanilla, :milk]
100
100
  cup.cost.should == 2.10
101
101
  cup.appearance.should == 'extra red vanilla'
102
102
 
103
103
  end
104
104
 
105
- a 'bigger example using normal Injector inheritance' do
105
+ a 'bigger example using normal Injector inheritance on web rendering' do
106
106
 
107
107
  # some data
108
108
 
@@ -122,7 +122,7 @@ describe 'some use cases', :injectors do
122
122
  end
123
123
  end
124
124
 
125
- injector :WidgetDecorator do
125
+ trait :WidgetDecorator do
126
126
  attr_reader :width, :height
127
127
 
128
128
  def dim(width, heigth)
@@ -216,7 +216,7 @@ describe 'some use cases', :injectors do
216
216
  end
217
217
 
218
218
 
219
- MainDecorator = injector :WidgetDecorator do
219
+ MainDecorator = trait :WidgetDecorator do
220
220
 
221
221
  attr_accessor :font, :width, :height
222
222
 
@@ -268,7 +268,7 @@ describe 'some use cases', :injectors do
268
268
  end)
269
269
  end
270
270
 
271
- expect(WidgetDecorator().ancestors).to eq([WidgetDecorator(), MainDecorator])
271
+ # expect(WidgetDecorator().ancestors).to eq([WidgetDecorator(), MainDecorator])
272
272
 
273
273
  expect(
274
274
  my_widget.render.split.join).to eq( # split.join used for comparison
@@ -285,45 +285,45 @@ describe 'some use cases', :injectors do
285
285
  )
286
286
 
287
287
 
288
- # browser = 'mobile'
289
- # @content = database_content
290
- #
291
- # my_widget = case browser
292
- # when match(/Safari|Firefox|IE/)
293
- # # debugger
294
- # MyWidgetClass.new(@content).enrich(WidgetDecorator() do
295
- #
296
- # def render
297
- # dim '600px', '200px'
298
- # @font ='helvetica'
299
- #
300
- # super()
301
- # end
302
- # end)
303
- # else
304
- # MyWidgetClass.new(@content).enrich(WidgetDecorator() do
305
- # def render
306
- # dim '200px', '600px'
307
- # @font ='arial'
308
- #
309
- # super()
310
- # end
311
- # end)
312
- # end
313
- # expect(
314
- #
315
- # my_widget.render.split.join).to eq( # split.join used for comparison
316
- # %{
317
- # <style>
318
- # #MyWidget {
319
- # font: 14px, arial;
320
- # width:200px;
321
- # height:600px
322
- # }
323
- # </style>
324
- # <div id='MyWidget'>car truck airplane boat</div>
325
- # }.split.join
326
- # )
288
+ browser = 'mobile'
289
+ @content = database_content
290
+
291
+ my_widget = case browser
292
+ when match(/Safari|Firefox|IE/)
293
+ # debugger
294
+ MyWidgetClass.new(@content).enrich(WidgetDecorator() do
295
+
296
+ def render
297
+ dim '600px', '200px'
298
+ @font ='helvetica'
299
+
300
+ super()
301
+ end
302
+ end)
303
+ else
304
+ MyWidgetClass.new(@content).enrich(WidgetDecorator() do
305
+ def render
306
+ dim '200px', '600px'
307
+ @font ='arial'
308
+
309
+ super()
310
+ end
311
+ end)
312
+ end
313
+ expect(
314
+
315
+ my_widget.render.split.join).to eq( # split.join used for comparison
316
+ %{
317
+ <style>
318
+ #MyWidget {
319
+ font: 14px, arial;
320
+ width:200px;
321
+ height:600px
322
+ }
323
+ </style>
324
+ <div id='MyWidget'>car truck airplane boat</div>
325
+ }.split.join
326
+ )
327
327
 
328
328
  WidgetDecorator(:implode)
329
329
 
@@ -351,12 +351,12 @@ describe 'some use cases', :injectors do
351
351
  end
352
352
  end
353
353
 
354
- injector :Sweedish do
354
+ trait :Sweedish do
355
355
  def brew
356
356
  @strategy = 'sweedish-'
357
357
  end
358
358
  end
359
- injector :French do
359
+ trait :French do
360
360
  def brew
361
361
  @strategy ='french-'
362
362
  end
@@ -377,12 +377,12 @@ describe 'some use cases', :injectors do
377
377
 
378
378
  this 'can be further enhanced by mixing it with the above decorator pattern' do
379
379
 
380
- injector :Russian do
380
+ trait :Russian do
381
381
  def brew
382
382
  @strategy = super.to_s + 'vodka-'
383
383
  end
384
384
  end
385
- injector :Scotish do
385
+ trait :Scotish do
386
386
  def brew
387
387
  @strategy = super.to_s + 'wiskey-'
388
388
  end
@@ -400,7 +400,7 @@ describe 'some use cases', :injectors do
400
400
  a new one' do
401
401
 
402
402
  class Tea < Coffee # Tea is a type of coffee ;~Q)
403
- injector :SpecialStrategy do
403
+ trait :SpecialStrategy do
404
404
  def brew
405
405
  @strategy = 'special'
406
406
  end
@@ -436,107 +436,209 @@ describe 'some use cases', :injectors do
436
436
 
437
437
  describe "further Jackbox Injector workflows" do
438
438
 
439
- it "allows adding decorators with function to be defined at later statge" do
439
+ describe 'Delayed Decorator pattern'do
440
+ it "allows adding decorators with function to be defined at later statge" do
440
441
 
441
- class Widget
442
- def cost
443
- 1
442
+ class Widget
443
+ def cost
444
+ 1
445
+ end
444
446
  end
445
- end
446
- w = Widget.new
447
+ w = Widget.new
447
448
 
448
- injector :decorator
449
- w.enrich decorator, decorator, decorator, decorator
449
+ trait :decorator
450
+ w.enrich decorator, decorator, decorator, decorator
450
451
 
451
- # user input
452
- bid = 3.5
452
+ # user input
453
+ bid = 3.5
453
454
 
454
- # define function
455
- decorator do
456
- # Global define
457
- define_method :cost do
458
- super() + bid
455
+ # define function
456
+ decorator do
457
+ # Global define
458
+ define_method :cost do
459
+ super() + bid
460
+ end
459
461
  end
460
- end
461
462
 
462
- w.cost.should == 15
463
+ w.cost.should == 15
463
464
 
465
+ end
464
466
  end
465
467
 
466
- it "allows for the following workflow using super" do
468
+ describe 'Super pattern (no its not a superlative pattern)' do
469
+ it "allows self-terminating recursion workflow using super" do
467
470
 
468
- jack :Superb
471
+ jack :Superb
469
472
 
470
- Superb do
471
- def process string, additives, index
472
- str = string.gsub('o', additives.slice!(index))
473
- super(string, additives, index) + str rescue str
474
- end
475
- extend Superb(), Superb(), Superb()
476
- end
473
+ Superb do
474
+ def process string, additives, index
475
+ str = string.gsub('o', additives.slice!(index))
476
+ super(string, additives, index) + str rescue str
477
+ end
478
+ extend Superb(), Superb(), Superb()
479
+ end
477
480
 
478
- Superb().process( 'food ', 'aeiu', 0 ).should == 'fuud fiid feed faad '
479
- Superb(:implode)
481
+ Superb().process( 'food ', 'aeiu', 0 ).should == 'fuud fiid feed faad '
482
+ Superb(:implode)
480
483
 
484
+ end
481
485
  end
486
+
487
+ describe 'Solutions Pattern' do
488
+ it 'allows trying several solutions in workflow using soft tags' do
482
489
 
483
- it 'allows for the following strategy workflow using soft tags' do
490
+ ###########################################################################
491
+ # For a specific example of what can be accomplished using this workflow #
492
+ # please refer to the examples directory under transformers spec #
493
+ # #
494
+ # #########################################################################
495
+
496
+ jack :Solution
484
497
 
485
- ###########################################################################
486
- # For a specific example of what can be accomplished using this workflow #
487
- # please refer to the examples directory under transformers spec #
488
- # #
489
- # #########################################################################
498
+ Solution( :tag ) do
499
+ def solution
500
+ 1
501
+ end
502
+ end
503
+ Solution( :tag ) do
504
+ def solution
505
+ 2
506
+ end
507
+ end
508
+ Solution( :tag ) do
509
+ def solution
510
+ 3
511
+ end
512
+ end
490
513
 
491
- jack :Solution
492
514
 
493
- Solution( :tag ) do
494
- def solution
495
- 1
515
+ class Client
516
+ inject Solution()
517
+
518
+ def self.solve
519
+ Solution().tags.each { |e|
520
+ update e
521
+ puts new.solution rescue nil
522
+ }
523
+
524
+ # or...
525
+
526
+ solutions = Solution().tags.each
527
+ begin
528
+ update solutions.next
529
+ puts solved = new().solution()
530
+ end until solved
531
+ solved
532
+ end
533
+
496
534
  end
535
+
536
+ $stdout.should_receive(:puts).with(1)
537
+ $stdout.should_receive(:puts).with(2)
538
+ $stdout.should_receive(:puts).with(3)
539
+ $stdout.should_receive(:puts).with(1)
540
+
541
+ Client.solve
542
+
497
543
  end
498
- Solution( :tag ) do
499
- def solution
500
- 2
544
+ end
545
+
546
+ describe "jiti as decorators with internal base" do
547
+ before do
548
+ JD1 = trait :jd do
549
+ def m1
550
+ 1
551
+ end
501
552
  end
502
- end
503
- Solution( :tag ) do
504
- def solution
505
- 3
553
+ JD2 = jd do
554
+ def m1
555
+ super + 2
556
+ end
506
557
  end
507
558
  end
508
-
559
+ it 'works like normal decorators' do
509
560
 
510
- class Client
511
- inject Solution()
561
+ o = Object.new
562
+ o.enrich JD2, JD1
563
+ o.m1.should == 3
512
564
 
513
- def self.solve
514
- Solution().tags.each { |e|
515
- update e
516
- puts new.solution rescue nil
517
- }
565
+ p = Object.new
566
+ p.enrich JD1, JD2
567
+ p.m1.should == 1
568
+
569
+ end
570
+
571
+ it 'raise errors on decorator collusions' do
572
+
573
+ expect{
518
574
 
519
- # or...
575
+ r = Object.new
576
+ r.enrich JD1, JD1
577
+ r.m1.should == 1
578
+
579
+ }.to raise_error(ArgumentError)
580
+ expect{
520
581
 
521
- solutions = Solution().tags.each
522
- begin
523
- update solutions.next
524
- puts solved = new().solution()
525
- end until solved
526
- solved
527
- end
582
+ q = Object.new
583
+ q.enrich JD2, JD2
584
+ q.m1.should == 5
585
+
586
+ }.to raise_error(ArgumentError)
528
587
 
529
588
  end
530
-
531
- $stdout.should_receive(:puts).with(1)
532
- $stdout.should_receive(:puts).with(2)
533
- $stdout.should_receive(:puts).with(3)
534
- $stdout.should_receive(:puts).with(1)
589
+ end
590
+
591
+ describe "jiti as decorators on external base" do
592
+ before do
593
+ JD1 = trait :jd do
594
+ def m1
595
+ super + 1
596
+ end
597
+ end
598
+ JD2 = jd do
599
+ def m1
600
+ super + 2
601
+ end
602
+ end
603
+ class JDClass
604
+ def m1
605
+ 1
606
+ end
607
+ end
608
+ end
535
609
 
536
- Client.solve
610
+ it 'can work like normal decorators' do
611
+
612
+ o = JDClass.new
613
+ o.enrich JD2, JD1
614
+ o.m1.should == 5
615
+
616
+ p = JDClass.new
617
+ p.enrich JD1, JD2
618
+ p.m1.should == 5
619
+
620
+ end
537
621
 
622
+ it 'raises errors on decorator collusions' do
623
+
624
+ expect{
625
+
626
+ r = JDClass.new
627
+ r.enrich JD1, JD1
628
+ r.m1.should == 3
629
+
630
+ }.to raise_error(ArgumentError)
631
+ expect{
632
+
633
+ q = JDClass.new
634
+ q.enrich JD2, JD2
635
+ q.m1.should == 6
636
+
637
+ }.to raise_error(ArgumentError)
638
+
639
+ end
538
640
  end
641
+
539
642
  end
540
-
541
643
  end
542
644