flag_shih_tzu 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/CHANGELOG +5 -0
- data/README.rdoc +19 -1
- data/lib/flag_shih_tzu.rb +58 -0
- data/lib/flag_shih_tzu/version.rb +1 -1
- data/test/flag_shih_tzu_test.rb +228 -0
- data/test/test_helper.rb +13 -0
- metadata +1 -1
data/.gitignore
CHANGED
data/CHANGELOG
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
Version 0.3.1 - NOV.06.2012
|
2
|
+
|
3
|
+
* Adds new methods (for a flag column named 'bar', with many individual flags within) - from ddidier
|
4
|
+
- all_bar, selected_bar, select_all_bar, unselect_all_bar, selected_bar=(selected_flags), has_bar?
|
5
|
+
|
1
6
|
Version 0.3.0 - NOV.05.2012 - first version maintained by Peter Boling
|
2
7
|
|
3
8
|
* ClassWithHasFlags.set_#{flag_name}_sql # Returns the sql string for setting a flag for use in customized SQL
|
data/README.rdoc
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
== Change of Ownership and 0.3.
|
1
|
+
== Change of Ownership and 0.3.X Release Notes
|
2
2
|
|
3
3
|
FlagShihTzu was originally a {XING AG}[http://www.xing.com/] project. {Peter Boling}[http://peterboling.com] was a long time contributor and watcher of the project.
|
4
4
|
In September 2012 XING transferred ownership of the project to Peter Boling. Peter Boling had been maintaining a
|
@@ -171,16 +171,34 @@ on Spaceship:
|
|
171
171
|
Spaceship#warpdrive?
|
172
172
|
Spaceship#warpdrive=
|
173
173
|
Spaceship#warpdrive_changed?
|
174
|
+
Spaceship#all_warpdrives
|
175
|
+
Spaceship#selected_warpdrives
|
176
|
+
Spaceship#select_all_warpdrives
|
177
|
+
Spaceship#unselect_all_warpdrives
|
178
|
+
Spaceship#selected_warpdrives=
|
179
|
+
Spaceship#has_warpdrive?
|
174
180
|
|
175
181
|
Spaceship#shields
|
176
182
|
Spaceship#shields?
|
177
183
|
Spaceship#shields=
|
178
184
|
Spaceship#shields_changed?
|
185
|
+
Spaceship#all_shields
|
186
|
+
Spaceship#selected_shields
|
187
|
+
Spaceship#select_all_shields
|
188
|
+
Spaceship#unselect_all_shields
|
189
|
+
Spaceship#selected_shields=
|
190
|
+
Spaceship#has_shield?
|
179
191
|
|
180
192
|
Spaceship#electrolytes
|
181
193
|
Spaceship#electrolytes?
|
182
194
|
Spaceship#electrolytes=
|
183
195
|
Spaceship#electrolytes_changed?
|
196
|
+
Spaceship#all_electrolytes
|
197
|
+
Spaceship#selected_electrolytes
|
198
|
+
Spaceship#select_all_electrolytes
|
199
|
+
Spaceship#unselect_all_electrolytes
|
200
|
+
Spaceship#selected_electrolytes=
|
201
|
+
Spaceship#has_electrolyte?
|
184
202
|
|
185
203
|
===Generated class methods
|
186
204
|
|
data/lib/flag_shih_tzu.rb
CHANGED
@@ -94,6 +94,40 @@ module FlagShihTzu
|
|
94
94
|
end
|
95
95
|
EVAL
|
96
96
|
|
97
|
+
if colmn != DEFAULT_COLUMN_NAME
|
98
|
+
class_eval <<-EVAL
|
99
|
+
|
100
|
+
def all_#{colmn}
|
101
|
+
all_flags('#{colmn}')
|
102
|
+
end
|
103
|
+
|
104
|
+
def selected_#{colmn}
|
105
|
+
selected_flags('#{colmn}')
|
106
|
+
end
|
107
|
+
|
108
|
+
def select_all_#{colmn}
|
109
|
+
select_all_flags('#{colmn}')
|
110
|
+
end
|
111
|
+
|
112
|
+
def unselect_all_#{colmn}
|
113
|
+
unselect_all_flags('#{colmn}')
|
114
|
+
end
|
115
|
+
|
116
|
+
# useful for a form builder
|
117
|
+
def selected_#{colmn}=(selected_flags)
|
118
|
+
unselect_all_flags('#{colmn}')
|
119
|
+
selected_flags.each do |selected_flag|
|
120
|
+
enable_flag(selected_flag.to_sym, '#{colmn}') if selected_flag.present?
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def has_#{colmn.singularize}?
|
125
|
+
not selected_#{colmn}.empty?
|
126
|
+
end
|
127
|
+
|
128
|
+
EVAL
|
129
|
+
end
|
130
|
+
|
97
131
|
# Define bancg methods when requested
|
98
132
|
if flag_options[colmn][:bang_methods]
|
99
133
|
class_eval <<-EVAL
|
@@ -246,6 +280,30 @@ module FlagShihTzu
|
|
246
280
|
self[colmn] = value
|
247
281
|
end
|
248
282
|
|
283
|
+
def all_flags(column)
|
284
|
+
flag_mapping[column].keys
|
285
|
+
end
|
286
|
+
|
287
|
+
def selected_flags(column)
|
288
|
+
all_flags(column).map { |flag_name| self.send(flag_name) ? flag_name : nil }.compact
|
289
|
+
end
|
290
|
+
|
291
|
+
def select_all_flags(column)
|
292
|
+
all_flags(column).each do |flag|
|
293
|
+
enable_flag(flag, column)
|
294
|
+
end
|
295
|
+
end
|
296
|
+
|
297
|
+
def unselect_all_flags(column)
|
298
|
+
all_flags(column).each do |flag|
|
299
|
+
disable_flag(flag, column)
|
300
|
+
end
|
301
|
+
end
|
302
|
+
|
303
|
+
def has_flag?(column = DEFAULT_COLUMN_NAME)
|
304
|
+
not selected_flags(column).empty?
|
305
|
+
end
|
306
|
+
|
249
307
|
private
|
250
308
|
|
251
309
|
def get_bit_for(flag, colmn)
|
data/test/flag_shih_tzu_test.rb
CHANGED
@@ -325,6 +325,7 @@ class FlagShihTzuInstanceMethodsTest < Test::Unit::TestCase
|
|
325
325
|
def setup
|
326
326
|
@spaceship = Spaceship.new
|
327
327
|
@big_spaceship = SpaceshipWith2CustomFlagsColumn.new
|
328
|
+
@small_spaceship = SpaceshipWithCustomFlagsColumn.new
|
328
329
|
end
|
329
330
|
|
330
331
|
def test_should_enable_flag
|
@@ -407,6 +408,233 @@ class FlagShihTzuInstanceMethodsTest < Test::Unit::TestCase
|
|
407
408
|
assert_equal false, @spaceship.warpdrive
|
408
409
|
end
|
409
410
|
|
411
|
+
# --------------------------------------------------
|
412
|
+
|
413
|
+
def test_should_define_an_all_flags_reader_method
|
414
|
+
assert_array_similarity [:electrolytes, :warpdrive, :shields], @spaceship.all_flags('flags')
|
415
|
+
end
|
416
|
+
|
417
|
+
def test_should_define_a_selected_flags_reader_method
|
418
|
+
assert_array_similarity [], @spaceship.selected_flags('flags')
|
419
|
+
|
420
|
+
@spaceship.warpdrive = true
|
421
|
+
assert_array_similarity [:warpdrive], @spaceship.selected_flags('flags')
|
422
|
+
|
423
|
+
@spaceship.electrolytes = true
|
424
|
+
assert_array_similarity [:electrolytes, :warpdrive], @spaceship.selected_flags('flags')
|
425
|
+
|
426
|
+
@spaceship.warpdrive = false
|
427
|
+
@spaceship.electrolytes = false
|
428
|
+
assert_array_similarity [], @spaceship.selected_flags('flags')
|
429
|
+
end
|
430
|
+
|
431
|
+
def test_should_define_a_select_all_flags_method
|
432
|
+
@spaceship.select_all_flags('flags')
|
433
|
+
assert @spaceship.warpdrive
|
434
|
+
assert @spaceship.shields
|
435
|
+
assert @spaceship.electrolytes
|
436
|
+
end
|
437
|
+
|
438
|
+
def test_should_define_an_unselect_all_flags_method
|
439
|
+
@spaceship.warpdrive = true
|
440
|
+
@spaceship.shields = true
|
441
|
+
@spaceship.electrolytes = true
|
442
|
+
|
443
|
+
@spaceship.unselect_all_flags('flags')
|
444
|
+
|
445
|
+
assert !@spaceship.warpdrive
|
446
|
+
assert !@spaceship.shields
|
447
|
+
assert !@spaceship.electrolytes
|
448
|
+
end
|
449
|
+
|
450
|
+
def test_should_define_an_has_flag_method
|
451
|
+
assert !@spaceship.has_flag?('flags')
|
452
|
+
|
453
|
+
@spaceship.warpdrive = true
|
454
|
+
assert @spaceship.has_flag?('flags')
|
455
|
+
|
456
|
+
@spaceship.shields = true
|
457
|
+
assert @spaceship.has_flag?('flags')
|
458
|
+
|
459
|
+
@spaceship.electrolytes = true
|
460
|
+
assert @spaceship.has_flag?('flags')
|
461
|
+
|
462
|
+
@spaceship.unselect_all_flags('flags')
|
463
|
+
assert !@spaceship.has_flag?('flags')
|
464
|
+
end
|
465
|
+
|
466
|
+
# --------------------------------------------------
|
467
|
+
|
468
|
+
def test_should_define_a_customized_all_flags_reader_method
|
469
|
+
assert_array_similarity [:hyperspace, :warpdrive], @small_spaceship.all_bits
|
470
|
+
end
|
471
|
+
|
472
|
+
def test_should_define_a_customized_selected_flags_reader_method
|
473
|
+
assert_array_similarity [], @small_spaceship.selected_bits
|
474
|
+
|
475
|
+
@small_spaceship.warpdrive = true
|
476
|
+
assert_array_similarity [:warpdrive], @small_spaceship.selected_bits
|
477
|
+
|
478
|
+
@small_spaceship.hyperspace = true
|
479
|
+
assert_array_similarity [:hyperspace, :warpdrive], @small_spaceship.selected_bits
|
480
|
+
|
481
|
+
@small_spaceship.warpdrive = false
|
482
|
+
@small_spaceship.hyperspace = false
|
483
|
+
assert_array_similarity [], @small_spaceship.selected_bits
|
484
|
+
end
|
485
|
+
|
486
|
+
def test_should_define_a_customized_select_all_flags_method
|
487
|
+
@small_spaceship.select_all_bits
|
488
|
+
assert @small_spaceship.warpdrive
|
489
|
+
assert @small_spaceship.hyperspace
|
490
|
+
end
|
491
|
+
|
492
|
+
def test_should_define_a_customized_unselect_all_flags_method
|
493
|
+
@small_spaceship.warpdrive = true
|
494
|
+
@small_spaceship.hyperspace = true
|
495
|
+
|
496
|
+
@small_spaceship.unselect_all_bits
|
497
|
+
|
498
|
+
assert !@small_spaceship.warpdrive
|
499
|
+
assert !@small_spaceship.hyperspace
|
500
|
+
end
|
501
|
+
|
502
|
+
def test_should_define_a_customized_selected_flags_writer_method
|
503
|
+
@small_spaceship.selected_bits = [:warpdrive]
|
504
|
+
assert @small_spaceship.warpdrive
|
505
|
+
assert !@small_spaceship.hyperspace
|
506
|
+
|
507
|
+
@small_spaceship.selected_bits = [:hyperspace]
|
508
|
+
assert !@small_spaceship.warpdrive
|
509
|
+
assert @small_spaceship.hyperspace
|
510
|
+
|
511
|
+
@small_spaceship.selected_bits = [:hyperspace, :warpdrive]
|
512
|
+
assert @small_spaceship.warpdrive
|
513
|
+
assert @small_spaceship.hyperspace
|
514
|
+
|
515
|
+
@small_spaceship.selected_bits = []
|
516
|
+
assert !@small_spaceship.warpdrive
|
517
|
+
assert !@small_spaceship.hyperspace
|
518
|
+
end
|
519
|
+
|
520
|
+
def test_should_define_a_customized_has_flag_method
|
521
|
+
assert !@small_spaceship.has_bit?
|
522
|
+
|
523
|
+
@small_spaceship.warpdrive = true
|
524
|
+
assert @small_spaceship.has_bit?
|
525
|
+
|
526
|
+
@small_spaceship.hyperspace = true
|
527
|
+
assert @small_spaceship.has_bit?
|
528
|
+
|
529
|
+
@small_spaceship.unselect_all_bits
|
530
|
+
assert !@small_spaceship.has_bit?
|
531
|
+
end
|
532
|
+
|
533
|
+
# --------------------------------------------------
|
534
|
+
|
535
|
+
def test_should_define_a_customized_all_flags_reader_method_with_2_columns
|
536
|
+
assert_array_similarity [:hyperspace, :warpdrive], @big_spaceship.all_bits
|
537
|
+
assert_array_similarity [:dajanatroj, :jeanlucpicard], @big_spaceship.all_commanders
|
538
|
+
end
|
539
|
+
|
540
|
+
def test_should_define_a_customized_selected_flags_reader_method_with_2_columns
|
541
|
+
assert_array_similarity [], @big_spaceship.selected_bits
|
542
|
+
assert_array_similarity [], @big_spaceship.selected_commanders
|
543
|
+
|
544
|
+
@big_spaceship.warpdrive = true
|
545
|
+
@big_spaceship.jeanlucpicard = true
|
546
|
+
assert_array_similarity [:warpdrive], @big_spaceship.selected_bits
|
547
|
+
assert_array_similarity [:jeanlucpicard], @big_spaceship.selected_commanders
|
548
|
+
|
549
|
+
@big_spaceship.hyperspace = true
|
550
|
+
@big_spaceship.hyperspace = true
|
551
|
+
@big_spaceship.jeanlucpicard = true
|
552
|
+
@big_spaceship.dajanatroj = true
|
553
|
+
assert_array_similarity [:hyperspace, :warpdrive], @big_spaceship.selected_bits
|
554
|
+
assert_array_similarity [:dajanatroj, :jeanlucpicard], @big_spaceship.selected_commanders
|
555
|
+
|
556
|
+
@big_spaceship.warpdrive = false
|
557
|
+
@big_spaceship.hyperspace = false
|
558
|
+
@big_spaceship.jeanlucpicard = false
|
559
|
+
@big_spaceship.dajanatroj = false
|
560
|
+
assert_array_similarity [], @big_spaceship.selected_bits
|
561
|
+
assert_array_similarity [], @big_spaceship.selected_commanders
|
562
|
+
end
|
563
|
+
|
564
|
+
def test_should_define_a_customized_select_all_flags_method_with_2_columns
|
565
|
+
@big_spaceship.select_all_bits
|
566
|
+
@big_spaceship.select_all_commanders
|
567
|
+
assert @big_spaceship.warpdrive
|
568
|
+
assert @big_spaceship.hyperspace
|
569
|
+
assert @big_spaceship.jeanlucpicard
|
570
|
+
assert @big_spaceship.dajanatroj
|
571
|
+
end
|
572
|
+
|
573
|
+
def test_should_define_a_customized_unselect_all_flags_method_with_2_columns
|
574
|
+
@big_spaceship.warpdrive = true
|
575
|
+
@big_spaceship.hyperspace = true
|
576
|
+
@big_spaceship.jeanlucpicard = true
|
577
|
+
@big_spaceship.dajanatroj = true
|
578
|
+
|
579
|
+
@big_spaceship.unselect_all_bits
|
580
|
+
@big_spaceship.unselect_all_commanders
|
581
|
+
|
582
|
+
assert !@big_spaceship.warpdrive
|
583
|
+
assert !@big_spaceship.hyperspace
|
584
|
+
assert !@big_spaceship.jeanlucpicard
|
585
|
+
assert !@big_spaceship.dajanatroj
|
586
|
+
end
|
587
|
+
|
588
|
+
def test_should_define_a_customized_selected_flags_writer_method_with_2_columns
|
589
|
+
@big_spaceship.selected_bits = [:warpdrive]
|
590
|
+
@big_spaceship.selected_commanders = [:jeanlucpicard]
|
591
|
+
assert @big_spaceship.warpdrive
|
592
|
+
assert !@big_spaceship.hyperspace
|
593
|
+
assert @big_spaceship.jeanlucpicard
|
594
|
+
assert !@big_spaceship.dajanatroj
|
595
|
+
|
596
|
+
@big_spaceship.selected_bits = [:hyperspace]
|
597
|
+
@big_spaceship.selected_commanders = [:dajanatroj]
|
598
|
+
assert !@big_spaceship.warpdrive
|
599
|
+
assert @big_spaceship.hyperspace
|
600
|
+
assert !@big_spaceship.jeanlucpicard
|
601
|
+
assert @big_spaceship.dajanatroj
|
602
|
+
|
603
|
+
@big_spaceship.selected_bits = [:hyperspace, :warpdrive]
|
604
|
+
@big_spaceship.selected_commanders = [:dajanatroj, :jeanlucpicard]
|
605
|
+
assert @big_spaceship.warpdrive
|
606
|
+
assert @big_spaceship.hyperspace
|
607
|
+
assert @big_spaceship.jeanlucpicard
|
608
|
+
assert @big_spaceship.dajanatroj
|
609
|
+
|
610
|
+
@big_spaceship.selected_bits = []
|
611
|
+
@big_spaceship.selected_commanders = []
|
612
|
+
assert !@big_spaceship.warpdrive
|
613
|
+
assert !@big_spaceship.hyperspace
|
614
|
+
assert !@big_spaceship.jeanlucpicard
|
615
|
+
assert !@big_spaceship.dajanatroj
|
616
|
+
end
|
617
|
+
|
618
|
+
def test_should_define_a_customized_has_flag_method_with_2_columns
|
619
|
+
assert !@big_spaceship.has_bit?
|
620
|
+
assert !@big_spaceship.has_commander?
|
621
|
+
|
622
|
+
@big_spaceship.warpdrive = true
|
623
|
+
@big_spaceship.jeanlucpicard = true
|
624
|
+
assert @big_spaceship.has_bit?
|
625
|
+
assert @big_spaceship.has_commander?
|
626
|
+
|
627
|
+
@big_spaceship.hyperspace = true
|
628
|
+
@big_spaceship.dajanatroj = true
|
629
|
+
assert @big_spaceship.has_bit?
|
630
|
+
|
631
|
+
@big_spaceship.unselect_all_bits
|
632
|
+
@big_spaceship.unselect_all_commanders
|
633
|
+
assert !@big_spaceship.has_bit?
|
634
|
+
end
|
635
|
+
|
636
|
+
# --------------------------------------------------
|
637
|
+
|
410
638
|
def test_should_define_an_attribute_reader_predicate_method
|
411
639
|
assert_equal false, @spaceship.warpdrive?
|
412
640
|
end
|
data/test/test_helper.rb
CHANGED
@@ -13,3 +13,16 @@ db_name = ENV["DB"] || "sqlite"
|
|
13
13
|
ActiveRecord::Base.establish_connection(db_name)
|
14
14
|
|
15
15
|
load(File.dirname(__FILE__) + "/schema.rb")
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
class Test::Unit::TestCase
|
22
|
+
|
23
|
+
def assert_array_similarity(expected, actual, message=nil)
|
24
|
+
full_message = build_message(message, "<?> expected but was\n<?>.\n", expected, actual)
|
25
|
+
assert_block(full_message) { (expected.size == actual.size) && (expected - actual == []) }
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|