Module: Invokable::ClassMethods

Defined in:
lib/invokable.rb

Overview

Note:

The module should not be used directly.

The methods that are mixed into any class at the class level that includes Invokable.

Instance Method Summary collapse

Instance Method Details

#arityInteger

Return the “total” arity of the class (i.e. the arity of the initializer and the arity of the call method)

Returns:

  • (Integer)

Version:

  • 0.6.0



52
53
54
# File 'lib/invokable.rb', line 52

def arity
  initializer_arity + instance_method(:call).arity
end

#call(*args) ⇒ Object

Handle automatic currying–will accept either the initializer arity or the total arity of the class. If the initializer arity is used return a class instance. If the total arity is used instantiate the class and return the results of the ‘call` method.

See Also:

Version:

  • 0.6.0



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/invokable.rb', line 63

def call(*args)
  return new.call   if arity == 0
  return new(*args) if args.length == initializer_arity

  if args.length == arity
    init_args = args.slice(0, initializer_arity)
    call_args = args.slice(initializer_arity, args.length)
    new(*init_args).call(*call_args)
  else
    raise ArgumentError, "wrong number of arguments (given #{args.length}, expected #{initializer_arity} or #{arity})"
  end
end