Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added :bang_methods option to enable bang methods for each flag

  • Loading branch information...
commit 7058407c85b05bdc2f8280b1cc80d7e8a4816234 1 parent 1d4acea
@miyagawa miyagawa authored
Showing with 35 additions and 0 deletions.
  1. +7 −0 README.rdoc
  2. +13 −0 lib/flag_shih_tzu.rb
  3. +15 −0 test/flag_shih_tzu_test.rb
View
7 README.rdoc
@@ -163,6 +163,13 @@ on Spaceship:
Spaceship#electrolytes=
Spaceship#electrolytes_changed?
+Opionally, you can set the <tt>:bang_methods</tt> option to true to enable the bang methods:
+
+ Spaceship#electrolytes!
+ Spaceship#not_electrolytes!
+
+which respectively enables or disables the electrolytes flag.
+
===Generated named scopes
View
13 lib/flag_shih_tzu.rb
@@ -77,6 +77,19 @@ def self.not_#{flag_name}_condition
end
EVAL
+ # Define bancg methods when requested
+ if flag_options[colmn][:bang_methods]
+ class_eval <<-EVAL
+ def #{flag_name}!
+ enable_flag(:#{flag_name}, '#{colmn}')
+ end
+
+ def not_#{flag_name}!
+ disable_flag(:#{flag_name}, '#{colmn}')
+ end
+ EVAL
+ end
+
# Define the named scopes if the user wants them and AR supports it
if flag_options[colmn][:named_scopes] && respond_to?(named_scope_method)
class_eval <<-EVAL
View
15 test/flag_shih_tzu_test.rb
@@ -52,6 +52,13 @@ class SpaceshipWithBitOperatorQueryMode < ActiveRecord::Base
has_flags(1 => :warpdrive, 2 => :shields, :flag_query_mode => :bit_operator)
end
+class SpaceshipWithBangMethods < ActiveRecord::Base
+ self.table_name = 'spaceships'
+ include FlagShihTzu
+
+ has_flags(1 => :warpdrive, 2 => :shields, :bang_methods => true)
+end
+
class SpaceCarrier < Spaceship
end
@@ -538,4 +545,12 @@ def test_should_respect_true_values_like_active_record
assert !@spaceship.warpdrive
end
end
+
+ def test_should_define_bang_methods
+ spaceship = SpaceshipWithBangMethods.new
+ spaceship.warpdrive!
+ assert spaceship.warpdrive
+ spaceship.not_warpdrive!
+ assert !spaceship.warpdrive
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.