Class: Workflow::Callbacks::Callback

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/workflow/callbacks/callback.rb

Overview

Receives an expression and generates a lambda that can be called against an object, for use in callback logic.

Adapted from ActiveSupport::Callbacks https://github.com/rails/rails/blob/bca2e69b785fa3cdbe148b0d2dd5d3b58f6daf53/activesupport/lib/active_support/callbacks.rb#L296

Direct Known Subclasses

MethodCallback, ProcCallback, StringCallback

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expression, inverted = false) ⇒ Callback

Returns a new instance of Callback.



14
15
16
17
18
# File 'lib/workflow/callbacks/callback.rb', line 14

def initialize(expression, inverted = false)
  @expression = expression
  @callback = make_lambda(expression)
  @callback = invert_lambda(@callback) if inverted
end

Instance Attribute Details

#callbackObject (readonly)

Returns the value of attribute callback.



13
14
15
# File 'lib/workflow/callbacks/callback.rb', line 13

def callback
  @callback
end

#expressionObject (readonly)

Returns the value of attribute expression.



13
14
15
# File 'lib/workflow/callbacks/callback.rb', line 13

def expression
  @expression
end

Class Method Details

.build(expression, inverted = false) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/workflow/callbacks/callback.rb', line 28

def self.build(expression, inverted = false)
  case expression
  when Symbol
    MethodCallback.new(expression, inverted)
  when String
    StringCallback.new(expression, inverted)
  when Proc
    ProcCallback.new(expression, inverted)
  end
end

.inverted(expression) ⇒ Object



24
25
26
# File 'lib/workflow/callbacks/callback.rb', line 24

def self.inverted(expression)
  build(expression, true)
end

Instance Method Details

#call(target) ⇒ Object



20
21
22
# File 'lib/workflow/callbacks/callback.rb', line 20

def call(target)
  callback.call(target, -> {})
end