aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorJoshua Peek <josh@joshpeek.com>2008-04-17 23:30:01 -0500
committerJoshua Peek <josh@joshpeek.com>2008-04-17 23:30:01 -0500
commitcf04e621270bb2e5e9e7971d2c59e73d6797482d (patch)
treeaf14a01cb712d6ade17f39da3a8103d7bf3bfd8c /activesupport
parent82b4faf81218bbd8916ab559590db236c7f80e46 (diff)
downloadrails-cf04e621270bb2e5e9e7971d2c59e73d6797482d.tar.gz
rails-cf04e621270bb2e5e9e7971d2c59e73d6797482d.tar.bz2
rails-cf04e621270bb2e5e9e7971d2c59e73d6797482d.zip
Tidy up ActiveSupport::Callbacks::CallbackChain instance API.
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/callbacks.rb25
-rw-r--r--activesupport/test/callbacks_test.rb31
2 files changed, 48 insertions, 8 deletions
diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb
index 329cc2fdc7..282b6cbcbc 100644
--- a/activesupport/lib/active_support/callbacks.rb
+++ b/activesupport/lib/active_support/callbacks.rb
@@ -96,17 +96,26 @@ module ActiveSupport
end
end
- def find_callback(callback, &block)
+ def |(chain)
+ if chain.is_a?(Callback)
+ if found_callback = find(chain)
+ index = index(found_callback)
+ self[index] = chain
+ else
+ self << chain
+ end
+ else
+ chain.each { |callback| self | callback }
+ end
+ self
+ end
+
+ def find(callback, &block)
select { |c| c == callback && (!block_given? || yield(c)) }.first
end
- def replace_or_append_callback(callback)
- if found_callback = find_callback(callback)
- index = index(found_callback)
- self[index] = callback
- else
- self << callback
- end
+ def delete(callback)
+ super(find(callback))
end
private
diff --git a/activesupport/test/callbacks_test.rb b/activesupport/test/callbacks_test.rb
index 3f8cb7f01a..7f71ca2262 100644
--- a/activesupport/test/callbacks_test.rb
+++ b/activesupport/test/callbacks_test.rb
@@ -96,6 +96,8 @@ class ConditionalCallbackTest < Test::Unit::TestCase
end
class CallbackTest < Test::Unit::TestCase
+ include ActiveSupport::Callbacks
+
def test_eql
callback = Callback.new(:before, :save, :identifier => :lifesaver)
assert callback.eql?(Callback.new(:before, :save, :identifier => :lifesaver))
@@ -115,3 +117,32 @@ class CallbackTest < Test::Unit::TestCase
assert_equal({}, a.options)
end
end
+
+class CallbackChainTest < Test::Unit::TestCase
+ include ActiveSupport::Callbacks
+
+ def setup
+ @chain = CallbackChain.build(:make, :bacon, :lettuce, :tomato)
+ end
+
+ def test_build
+ assert_equal 3, @chain.size
+ assert_equal [:bacon, :lettuce, :tomato], @chain.map(&:method)
+ end
+
+ def test_find
+ assert_equal :bacon, @chain.find(:bacon).method
+ end
+
+ def test_union
+ assert_equal [:bacon, :lettuce, :tomato], (@chain | Callback.new(:make, :bacon)).map(&:method)
+ assert_equal [:bacon, :lettuce, :tomato, :turkey], (@chain | CallbackChain.build(:make, :bacon, :lettuce, :tomato, :turkey)).map(&:method)
+ assert_equal [:bacon, :lettuce, :tomato, :turkey, :mayo], (@chain | Callback.new(:make, :mayo)).map(&:method)
+ end
+
+ def test_delete
+ assert_equal [:bacon, :lettuce, :tomato], @chain.map(&:method)
+ @chain.delete(:bacon)
+ assert_equal [:lettuce, :tomato], @chain.map(&:method)
+ end
+end