Class: NxtStateMachine::Callable

Inherits:
Object
  • Object
show all
Defined in:
lib/nxt_state_machine/callable.rb

Instance Method Summary collapse

Constructor Details

#initialize(callee) ⇒ Callable

Returns a new instance of Callable.



3
4
5
6
7
8
9
10
11
12
13
14
# 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

Instance Method Details

#arityObject



37
38
39
40
41
42
43
44
45
46
# 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) ⇒ Object



16
17
18
19
20
# 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) ⇒ Object

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!



25
26
27
28
29
30
31
32
33
34
35
# 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