Module: Callbacker::InstanceMethods
- Defined in:
- lib/callbacker/callbackable.rb,
lib/callbacker/validatable.rb
Overview
The InstanceMethods module provides the methods that are included in the class that includes the Callbackable module.
Instance Method Summary collapse
-
#execute_after_callbacks(triggering_event:, **args) ⇒ Object
The execute_after_callbacks method is used to run any callbacks that have been attached to the class, which essentially enables observers to have access to the previous and current state, along with the event that triggered the state transition.
-
#execute_before_callbacks(triggering_event:, **args) ⇒ Object
The execute_before_callbacks method is used to run any callbacks that have been attached to the class, but these run before a state transition triggered by an event.
-
#execute_validators(triggering_event:, **args) ⇒ Boolean
The execute_validators method is used to run any validators that have been attached to the class, which essentially enables the validator to return false and halt a state transition, or return true to allow it to continue.
-
#to_event_args(from:, to:, triggering_event:, args:) ⇒ Hash
The to_event_args method is used to convert the arguments passed to the execute_before_callbacks and execute_after_callbacks methods into a hash that can be passed to the callback.
Instance Method Details
#execute_after_callbacks(triggering_event:, **args) ⇒ Object
The execute_after_callbacks method is used to run any callbacks that have been attached to the class, which essentially enables observers to have access to the previous and current state, along with the event that triggered the state transition. This is fired after a successful state transition has occurred. transition.
47 48 49 50 51 |
# File 'lib/callbacker/callbackable.rb', line 47 def execute_after_callbacks(triggering_event:, **args) self.class.after_callbacks[triggering_event].each do |callback| callback.call(**args) end end |
#execute_before_callbacks(triggering_event:, **args) ⇒ Object
The execute_before_callbacks method is used to run any callbacks that have been attached to the class, but these run before a state transition triggered by an event. trigger the state transition.
59 60 61 62 63 |
# File 'lib/callbacker/callbackable.rb', line 59 def execute_before_callbacks(triggering_event:, **args) self.class.before_callbacks[triggering_event].each do |callback| callback.call(**args) end end |
#execute_validators(triggering_event:, **args) ⇒ Boolean
The execute_validators method is used to run any validators that have been attached to the class, which essentially enables the validator to return false and halt a state transition, or return true to allow it to continue. transition.
23 24 25 26 27 28 29 30 |
# File 'lib/callbacker/validatable.rb', line 23 def execute_validators(triggering_event:, **args) self.class.validators[triggering_event].each do |validator| conditional = validator[:conditional] halt!(validator[:reason]) unless conditional.call(**args) end true end |
#to_event_args(from:, to:, triggering_event:, args:) ⇒ Hash
The to_event_args method is used to convert the arguments passed to the execute_before_callbacks and execute_after_callbacks methods into a hash that can be passed to the callback. transition.
29 30 31 32 33 34 35 36 37 |
# File 'lib/callbacker/callbackable.rb', line 29 def to_event_args(from:, to:, triggering_event:, args:) { instance: self, from: from, to: to, triggering_event: triggering_event, args: args } end |