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

Instance Method Details

#after_callbacksObject

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)

Examples:

Attach an after_callback to the OrderState class:


Parameters:

  • event (Symbol)

    the event defined in the workflow to watch.

Yield Parameters:

  • callback

    the callback to execute after an event occurs.

Raises:

  • (States::AddCallbackError)

    if the event is not part of the workflow.



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)>]

Parameters:

  • callbacks (Object)

    the callbacks to attach. NOTE: the expected format is



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)

Examples:

Attach a callback to the OrderState class:


Parameters:

  • event (Symbol)

    the event defined in the workflow to watch.

Yield Parameters:

  • callback

    the callback to execute after an event occurs.

Raises:

  • (States::AddCallbackError)

    if the event is not part of the workflow.



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)>]

Parameters:

  • callbacks (Object)

    the callbacks to attach. NOTE: the expected format is



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)

Examples:

Attach a validator that blocks a state transition when the


Parameters:

  • event (Symbol)

    the event defined in the workflow to watch.

  • reason (String)

    the reason for the error.

Yield Parameters:

  • validator

    the validator to execute before an event occurs.

Raises:

  • (States::AddValidationError)

    if the event is not part of the workflow.



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_callbacksObject

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_callbacksObject

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_callbacksObject

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_validatorsObject

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

#validatorsObject

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