Module: ExMachina::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/ex_machina/util.rb

Defined Under Namespace

Classes: String

Instance Method Summary collapse

Instance Method Details

#invoke_method(context, meth, *args) ⇒ Object

Invoke method or lambda on given context passing correspondent args



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ex_machina/util.rb', line 21

def invoke_method(context, meth, *args)
  return if meth.nil?

  if meth.respond_to?(:call)
    all_args    = Array([context, *args])
    meth_params = meth.parameters
    meth_args   = *all_args.first(meth_params.size)

    meth.call(*meth_args)

  elsif context.respond_to?(meth)
    meth_params = context.method(meth).parameters
    meth_args   = *args.first(meth_params.size)

    context.send(meth, *meth_args)
  end
end