Module: Volt::LifecycleCallbacks

Included in:
HttpController, Model, ModelController
Defined in:
lib/volt/utils/lifecycle_callbacks.rb

Defined Under Namespace

Modules: ClassMethods Classes: StopChainException

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



90
91
92
# File 'lib/volt/utils/lifecycle_callbacks.rb', line 90

def self.included(base)
  base.send :extend, ClassMethods
end

Instance Method Details

#run_callbacks(callback_name, action = nil) ⇒ Object

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.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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_chainObject

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.



86
87
88
# File 'lib/volt/utils/lifecycle_callbacks.rb', line 86

def stop_chain
  fail StopChainException
end