class NxtStateMachine::Callable

Attributes

callee[RW]
context[RW]
type[RW]

Public Class Methods

new(callee) click to toggle source
# File lib/nxt_state_machine/callable.rb, line 3
def initialize(callee)
  @callee = callee

  if callee.is_a?(Symbol)
    self.type = :method
  elsif callee.respond_to?(:call)
    self.type = :proc
    self.context = callee.binding
  else
    raise ArgumentError, "Callee is nor symbol nor a proc: #{callee}"
  end
end

Public Instance Methods

arity() click to toggle source
# File lib/nxt_state_machine/callable.rb, line 37
def arity
  if proc?
    callee.arity
  elsif method?
    method = context.send(:method, callee)
    method.arity
  else
    raise ArgumentError, "Can't resolve arity from #{callee}"
  end
end
bind(execution_context = nil) click to toggle source
# File lib/nxt_state_machine/callable.rb, line 16
def bind(execution_context = nil)
  self.context = execution_context
  ensure_context_not_missing
  self
end
call(*args) click to toggle source

NOTE: Currentl we only allow arguments! Not keyword args or **options If we would allow **options and we would pass a hash as the only argument it would automatically be parsed as the options!

# File lib/nxt_state_machine/callable.rb, line 25
def call(*args)
  ensure_context_not_missing

  args = args.take(arity)

  if method?
    context.send(callee, *args)
  else
    context.instance_exec(*args, &callee)
  end
end

Private Instance Methods

ensure_context_not_missing() click to toggle source
# File lib/nxt_state_machine/callable.rb, line 58
def ensure_context_not_missing
  return if context
  raise ArgumentError, "Missing context: #{context}"
end
method?() click to toggle source
# File lib/nxt_state_machine/callable.rb, line 54
def method?
  type == :method
end
proc?() click to toggle source
# File lib/nxt_state_machine/callable.rb, line 50
def proc?
  type == :proc
end