Skip to content

Commit

Permalink
Merge from 'Xing/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
cinconnu committed Aug 22, 2012
2 parents 00bf266 + b95832d commit 1307798
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ Gemfile.lock
gemfiles/*.lock
pkg/*
rdoc/*
/.rvmrc
7 changes: 7 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 0 additions & 7 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion flag_shih_tzu.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 15 additions & 2 deletions lib/flag_shih_tzu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
55 changes: 35 additions & 20 deletions test/flag_shih_tzu_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
13 changes: 13 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1307798

Please sign in to comment.