class FlowMachine::Callback

Attributes

method[RW]
options[RW]

Public Class Methods

new(*args, &block) click to toggle source
# File lib/flow_machine/callback.rb, line 7
def initialize(*args, &block)
  @options = args.extract_options!
  @method = args.shift unless block
  @block = block
end

Public Instance Methods

call(target, changes = {}) click to toggle source
# File lib/flow_machine/callback.rb, line 13
def call(target, changes = {})
  return unless will_run? target, changes

  call!(target)
end
call!(target) click to toggle source

Runs the callback without any validations

# File lib/flow_machine/callback.rb, line 20
def call!(target)
  run_method_or_lambda(target, method.presence || @block)
end
run_method(target, method) click to toggle source
# File lib/flow_machine/callback.rb, line 32
def run_method(target, method)
  target.send(method)
end
run_method_or_lambda(target, method) click to toggle source
# File lib/flow_machine/callback.rb, line 24
def run_method_or_lambda(target, method)
  if method.respond_to? :call # is it a lambda
    target.instance_exec(&method)
  else
    run_method(target, method)
  end
end
will_run?(target, _changes = {}) click to toggle source
# File lib/flow_machine/callback.rb, line 36
def will_run?(target, _changes = {})
  if options[:if]
    [*options[:if]].all? { |m| run_method_or_lambda(target, m) }
  elsif options[:unless]
    [*options[:unless]].none? { |m| run_method_or_lambda(target, m) }
  else
    true
  end
end