Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge from 'Xing/master'

  • Loading branch information...
commit fc8fd3104b31cff34cada63aa4bbd89e865ee8fc 2 parents 00bf266 + b95832d
@cinconnu cinconnu authored
View
1  .gitignore
@@ -9,3 +9,4 @@ Gemfile.lock
gemfiles/*.lock
pkg/*
rdoc/*
+/.rvmrc
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
7 Rakefile
@@ -32,11 +32,4 @@ namespace :test do
sh "BUNDLE_GEMFILE='gemfiles/Gemfile.activerecord-#{version}' bundle exec rake test"
end
end
-
- desc 'Measures test coverage'
- task :coverage do
- rm_f "coverage"
- system("rcov -Ilib test/*_test.rb")
- system("open coverage/index.html") if PLATFORM['darwin']
- end
end
View
1  flag_shih_tzu.gemspec
@@ -27,6 +27,5 @@ ActiveRecord object.
s.add_development_dependency "bundler"
s.add_development_dependency "rdoc", ">= 2.4.2"
s.add_development_dependency "rake"
- s.add_development_dependency "rcov"
s.add_development_dependency "sqlite3"
end
View
17 lib/flag_shih_tzu.rb
@@ -10,8 +10,8 @@ module FlagShihTzu
def self.included(base)
base.extend(ClassMethods)
- base.class_attribute :flag_options
- base.class_attribute :flag_mapping
+ base.class_attribute :flag_options unless defined?(base.flag_options)
+ base.class_attribute :flag_mapping unless defined?(base.flag_mapping)
end
class IncorrectFlagColumnException < Exception; end
@@ -107,6 +107,19 @@ def selected_#{colmn}=(selected_flags)
EVAL
end
+ # 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
55 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
@@ -380,21 +387,21 @@ def test_should_define_an_attribute_reader_method
# --------------------------------------------------
def test_should_define_an_all_flags_reader_method
- assert_equal [:electrolytes, :warpdrive, :shields], @spaceship.all_flags('flags')
+ assert_array_similarity [:electrolytes, :warpdrive, :shields], @spaceship.all_flags('flags')
end
def test_should_define_a_selected_flags_reader_method
- assert_equal [], @spaceship.selected_flags('flags')
+ assert_array_similarity [], @spaceship.selected_flags('flags')
@spaceship.warpdrive = true
- assert_equal [:warpdrive], @spaceship.selected_flags('flags')
+ assert_array_similarity [:warpdrive], @spaceship.selected_flags('flags')
@spaceship.electrolytes = true
- assert_equal [:electrolytes, :warpdrive], @spaceship.selected_flags('flags')
+ assert_array_similarity [:electrolytes, :warpdrive], @spaceship.selected_flags('flags')
@spaceship.warpdrive = false
@spaceship.electrolytes = false
- assert_equal [], @spaceship.selected_flags('flags')
+ assert_array_similarity [], @spaceship.selected_flags('flags')
end
def test_should_define_a_select_all_flags_method
@@ -419,21 +426,21 @@ def test_should_define_an_unselect_all_flags_method
# --------------------------------------------------
def test_should_define_a_customized_all_flags_reader_method
- assert_equal [:hyperspace, :warpdrive], @small_spaceship.all_bits
+ assert_array_similarity [:hyperspace, :warpdrive], @small_spaceship.all_bits
end
def test_should_define_a_customized_selected_flags_reader_method
- assert_equal [], @small_spaceship.selected_bits
+ assert_array_similarity [], @small_spaceship.selected_bits
@small_spaceship.warpdrive = true
- assert_equal [:warpdrive], @small_spaceship.selected_bits
+ assert_array_similarity [:warpdrive], @small_spaceship.selected_bits
@small_spaceship.hyperspace = true
- assert_equal [:hyperspace, :warpdrive], @small_spaceship.selected_bits
+ assert_array_similarity [:hyperspace, :warpdrive], @small_spaceship.selected_bits
@small_spaceship.warpdrive = false
@small_spaceship.hyperspace = false
- assert_equal [], @small_spaceship.selected_bits
+ assert_array_similarity [], @small_spaceship.selected_bits
end
def test_should_define_a_customized_select_all_flags_method
@@ -473,32 +480,32 @@ def test_should_define_a_customized_selected_flags_writer_method
# --------------------------------------------------
def test_should_define_a_customized_all_flags_reader_method_with_2_columns
- assert_equal [:hyperspace, :warpdrive], @big_spaceship.all_bits
- assert_equal [:dajanatroj, :jeanlucpicard], @big_spaceship.all_commanders
+ assert_array_similarity [:hyperspace, :warpdrive], @big_spaceship.all_bits
+ assert_array_similarity [:dajanatroj, :jeanlucpicard], @big_spaceship.all_commanders
end
def test_should_define_a_customized_selected_flags_reader_method_with_2_columns
- assert_equal [], @big_spaceship.selected_bits
- assert_equal [], @big_spaceship.selected_commanders
+ assert_array_similarity [], @big_spaceship.selected_bits
+ assert_array_similarity [], @big_spaceship.selected_commanders
@big_spaceship.warpdrive = true
@big_spaceship.jeanlucpicard = true
- assert_equal [:warpdrive], @big_spaceship.selected_bits
- assert_equal [:jeanlucpicard], @big_spaceship.selected_commanders
+ assert_array_similarity [:warpdrive], @big_spaceship.selected_bits
+ assert_array_similarity [:jeanlucpicard], @big_spaceship.selected_commanders
@big_spaceship.hyperspace = true
@big_spaceship.hyperspace = true
@big_spaceship.jeanlucpicard = true
@big_spaceship.dajanatroj = true
- assert_equal [:hyperspace, :warpdrive], @big_spaceship.selected_bits
- assert_equal [:dajanatroj, :jeanlucpicard], @big_spaceship.selected_commanders
+ assert_array_similarity [:hyperspace, :warpdrive], @big_spaceship.selected_bits
+ assert_array_similarity [:dajanatroj, :jeanlucpicard], @big_spaceship.selected_commanders
@big_spaceship.warpdrive = false
@big_spaceship.hyperspace = false
@big_spaceship.jeanlucpicard = false
@big_spaceship.dajanatroj = false
- assert_equal [], @big_spaceship.selected_bits
- assert_equal [], @big_spaceship.selected_commanders
+ assert_array_similarity [], @big_spaceship.selected_bits
+ assert_array_similarity [], @big_spaceship.selected_commanders
end
def test_should_define_a_customized_select_all_flags_method_with_2_columns
@@ -719,4 +726,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
View
13 test/test_helper.rb
@@ -13,3 +13,16 @@
ActiveRecord::Base.establish_connection(db_name)
load(File.dirname(__FILE__) + "/schema.rb")
+
+
+
+
+
+class Test::Unit::TestCase
+
+ def assert_array_similarity(expected, actual, message=nil)
+ full_message = build_message(message, "<?> expected but was\n<?>.\n", expected, actual)
+ assert_block(full_message) { (expected.size == actual.size) && (expected - actual == []) }
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.