module Volt::LifecycleCallbacks
Public Class Methods
included(base)
click to toggle source
# File lib/volt/utils/lifecycle_callbacks.rb, line 90 def self.included(base) base.send :extend, ClassMethods end
Public Instance Methods
run_callbacks(callback_name, action=nil)
click to toggle source
To run the callbacks on a class, call run_callbacks
passing in the callback_name and the action it runs with. If the callback chain was stopped with stop_chain
, it will return true, otherwise false.
# File lib/volt/utils/lifecycle_callbacks.rb, line 59 def run_callbacks(callback_name, action=nil) callbacks = self.class.send(:"#{callback_name}_callbacks") callbacks ||= [] if action callbacks = filter_actions_by_only_exclude(callbacks || [], action) end begin callbacks.map { |v| v[0] }.each do |callback| case callback when Symbol send(callback) when Proc instance_eval(&callback) end end return false rescue StopChainException => e return true end end
stop_chain()
click to toggle source
The stop chain method can be called inside of a callback and it will raise an exception under the hood which will stop the chain and evaluation from where stop_chain
is called.
# File lib/volt/utils/lifecycle_callbacks.rb, line 86 def stop_chain fail StopChainException end
Private Instance Methods
filter_actions_by_only_exclude(callbacks, action)
click to toggle source
TODO: currently we filter during the call, we could maybe improve performance here by storing by action and having an all category as well.
# File lib/volt/utils/lifecycle_callbacks.rb, line 98 def filter_actions_by_only_exclude(callbacks, action) callbacks.select do |callback, options| if options && (only = options[:only]) # If there is an only, make sure the action is in the list. [only].flatten.include?(action.to_sym) else # If no only, include it true end end end