Module: Callbacker::ClassMethods
- Defined in:
- lib/callbacker/callbackable.rb,
lib/callbacker/validatable.rb
Overview
The ClassMethods module provides the methods that are extended into the class that includes the Callbackable module.
Instance Method Summary collapse
-
#after_callbacks ⇒ Object
after_callbacks are blocks that are executed upon successful state transition by an event.
-
#attach_after_callback(event) {|callback| ... } ⇒ Object
The attach_after_callback method is how we can fire off callbacks after an event has triggered a state change.
-
#attach_after_callbacks(callbacks) ⇒ Object
The attach_before_callbacks method enables attaching multiple after callbacks.
-
#attach_before_callback(event) {|callback| ... } ⇒ Object
The attach_before_callback method is how we can fire off callbacks before an event triggers a state change.
-
#attach_before_callbacks(callbacks) ⇒ Object
The attach_before_callbacks method enables attaching multiple after callbacks.
-
#attach_validator(event, reason) {|validator| ... } ⇒ Object
The attach_validator method is how validators can be called when an event attempts to transition states.
-
#attach_validators(validators) ⇒ Object
The attach_validators method enables attaching multiple validators.
-
#before_callbacks ⇒ Object
before_callbacks are blocks that are executed before a state transition triggered by an event.
-
#clear_all_after_callbacks ⇒ Object
This will remove all of the attached after callbacks.
-
#clear_all_before_callbacks ⇒ Object
This will remove all of the before callbacks attached to OrderState.
-
#clear_all_validators ⇒ Object
This will remove all of the validators attached to OrderState.
-
#validators ⇒ Object
validators are attached to an event, so that when the event attempts to transition states, if the validator returns false, they don’t happen.
Instance Method Details
#after_callbacks ⇒ Object
after_callbacks are blocks that are executed upon successful state transition by an event.
153 154 155 |
# File 'lib/callbacker/callbackable.rb', line 153 def after_callbacks @after_callbacks ||= Hash.new { |hash, key| hash[key] = [] } end |
#attach_after_callback(event) {|callback| ... } ⇒ Object
The attach_after_callback method is how we can fire off callbacks after an event has triggered a state change. callback = ->(id, from, to, triggering_event, args) { puts “callback!” } Orders::OrderState.attach_after_callback(:close_for_admin_changes, &callback)
78 79 80 81 82 |
# File 'lib/callbacker/callbackable.rb', line 78 def attach_after_callback(event, &callback) raise add_callback_error(event) unless callbackable_events.include?(event) after_callbacks[event] << callback end |
#attach_after_callbacks(callbacks) ⇒ Object
The attach_before_callbacks method enables attaching multiple after callbacks. See (Callbackable#attach_after_callback). Basically, you want to use this to reattach after callbacks you cleared. essentially what you get when you call, say, #after_callbacks, which looks like:
[#<Proc:0x00005576334144d0 (lambda)>,
#<Proc:0x00005576334144a8 (lambda)>]
107 108 109 110 111 112 113 114 115 |
# File 'lib/callbacker/callbackable.rb', line 107 def attach_after_callbacks(callbacks) return unless callbacks.present? callbacks.each do |event, callback_objects| callback_objects.each do |callback| attach_after_callback(event, &callback) end end end |
#attach_before_callback(event) {|callback| ... } ⇒ Object
The attach_before_callback method is how we can fire off callbacks before an event triggers a state change. callback = ->(id, from, to, triggering_event, args) { puts “callback!” } Orders::OrderState.attach_before_callback(:close_for_admin_changes, &callback)
93 94 95 96 97 |
# File 'lib/callbacker/callbackable.rb', line 93 def attach_before_callback(event, &callback) raise add_callback_error(event) unless callbackable_events.include?(event) before_callbacks[event] << callback end |
#attach_before_callbacks(callbacks) ⇒ Object
The attach_before_callbacks method enables attaching multiple after callbacks. See (Callbackable#attach_before_callback). Basically, you want to use this to reattach before callbacks you cleared. essentially what you get when you call, say, #after_callbacks, which looks like:
[#<Proc:0x00005576334144d0 (lambda)>,
#<Proc:0x00005576334144a8 (lambda)>]
125 126 127 128 129 130 131 132 133 |
# File 'lib/callbacker/callbackable.rb', line 125 def attach_before_callbacks(callbacks) return unless callbacks.present? callbacks.each do |event, callback_objects| callback_objects.each do |callback| attach_after_callback(event, &callback) end end end |
#attach_validator(event, reason) {|validator| ... } ⇒ Object
The attach_validator method is how validators can be called when an event attempts to transition states. close_for_admin_changes event is triggered: validator = ->(id, from, to, triggering_event, args) { false } Orders::OrderState.attach_validator(:close_for_admin_changes, &validator)
45 46 47 48 49 |
# File 'lib/callbacker/validatable.rb', line 45 def attach_validator(event, reason, &validator) raise add_validation_error(event) unless validatable_events.include?(event) validators[event] << { reason: reason, conditional: validator } end |
#attach_validators(validators) ⇒ Object
The attach_validators method enables attaching multiple validators. See (Validatable#attach_validators).
53 54 55 56 57 58 59 60 61 |
# File 'lib/callbacker/validatable.rb', line 53 def attach_validators(validators) return unless validators.present? validators.each do |event, validator_objects| validator_objects.each do |validator| attach_validator(event, validator[:reason], &validator[:conditional]) end end end |
#before_callbacks ⇒ Object
before_callbacks are blocks that are executed before a state transition triggered by an event.
147 148 149 |
# File 'lib/callbacker/callbackable.rb', line 147 def before_callbacks @before_callbacks ||= Hash.new { |hash, key| hash[key] = [] } end |
#clear_all_after_callbacks ⇒ Object
This will remove all of the attached after callbacks.
136 137 138 |
# File 'lib/callbacker/callbackable.rb', line 136 def clear_all_after_callbacks @after_callbacks = Hash.new { |hash, key| hash[key] = [] } end |
#clear_all_before_callbacks ⇒ Object
This will remove all of the before callbacks attached to OrderState.
141 142 143 |
# File 'lib/callbacker/callbackable.rb', line 141 def clear_all_before_callbacks @before_callbacks = Hash.new { |hash, key| hash[key] = [] } end |
#clear_all_validators ⇒ Object
This will remove all of the validators attached to OrderState.
64 65 66 |
# File 'lib/callbacker/validatable.rb', line 64 def clear_all_validators @validators = Hash.new { |hash, key| hash[key] = [] } end |
#validators ⇒ Object
validators are attached to an event, so that when the event attempts to transition states, if the validator returns false, they don’t happen.
70 71 72 |
# File 'lib/callbacker/validatable.rb', line 70 def validators @validators ||= Hash.new { |hash, key| hash[key] = [] } end |