aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2013-05-10 11:00:25 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2013-05-10 11:00:25 -0700
commitade7d365e434b586a4c1747d917beb986cb35fc7 (patch)
treebd3355496fec42521c8fbd6956dc9ad8ac9fce25 /activesupport
parent3aee9126aa6309538ee64064dcabcd34d7cc7d26 (diff)
downloadrails-ade7d365e434b586a4c1747d917beb986cb35fc7.tar.gz
rails-ade7d365e434b586a4c1747d917beb986cb35fc7.tar.bz2
rails-ade7d365e434b586a4c1747d917beb986cb35fc7.zip
cache compiled callbacks
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/callbacks.rb34
1 files changed, 27 insertions, 7 deletions
diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb
index 13dccf328c..32234431a6 100644
--- a/activesupport/lib/active_support/callbacks.rb
+++ b/activesupport/lib/active_support/callbacks.rb
@@ -350,29 +350,46 @@ module ActiveSupport
:scope => [ :kind ]
}.merge!(config)
@chain = []
+ @callbacks = nil
end
def each(&block); @chain.each(&block); end
def index(o); @chain.index(o); end
- def insert(index, o); @chain.insert(index, o); end
- def delete(o); @chain.delete(o); end
- def clear; @chain.clear; self; end
def empty?; @chain.empty?; end
+ def insert(index, o)
+ @callbacks = nil
+ @chain.insert(index, o)
+ end
+
+ def delete(o)
+ @callbacks = nil
+ @chain.delete(o)
+ end
+
+ def clear
+ @callbacks = nil
+ @chain.clear
+ self
+ end
+
def initialize_copy(other)
- @chain = other.chain.dup
+ @callbacks = nil
+ @chain = other.chain.dup
end
def compile
- callbacks = lambda { |env|
+ return @callbacks if @callbacks
+
+ @callbacks = lambda { |env|
block = env.run_block
env.value = !env.halted && (!block || block.call)
env
}
@chain.reverse_each do |callback|
- callbacks = callback.apply(callbacks)
+ @callbacks = callback.apply(@callbacks)
end
- callbacks
+ @callbacks
end
def append(*callbacks)
@@ -389,16 +406,19 @@ module ActiveSupport
private
def append_one(callback)
+ @callbacks = nil
remove_duplicates(callback)
@chain.push(callback)
end
def prepend_one(callback)
+ @callbacks = nil
remove_duplicates(callback)
@chain.unshift(callback)
end
def remove_duplicates(callback)
+ @callbacks = nil
@chain.delete_if { |c| callback.duplicates?(c) }
end