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
-
#arity ⇒ Integer
Return the “total” arity of the class (i.e. the arity of the initializer and the arity of the call method).
-
#call(*args) ⇒ Object
Handle automatic currying–will accept either the initializer arity or the total arity of the class.
- #initializer_arity ⇒ Object
- #invoker_arity ⇒ Object
Instance Method Details
#arity ⇒ Integer
Return the “total” arity of the class (i.e. the arity of the initializer and the arity of the call method)
52 53 54 55 56 |
# File 'lib/invokable.rb', line 52 def arity return initializer_arity + invoker_arity if invoker_arity >= 0 (initializer_arity + invoker_arity.abs) * -1 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.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/invokable.rb', line 65 def call(*args) raise ArgumentError, "variable length initializer methods are not supported by Invokable" if initializer_arity < 0 return new.call if arity.zero? return new(*args).call if args.length == initializer_arity && (invoker_arity.zero? || invoker_arity == -1) return new(*args) if args.length == initializer_arity if args.length == arity || invoker_arity < 0 && (args.length - initializer_arity) >= (invoker_arity.abs - 1) 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 |
#initializer_arity ⇒ Object
81 82 83 |
# File 'lib/invokable.rb', line 81 def initializer_arity instance_method(:initialize).arity end |
#invoker_arity ⇒ Object
85 86 87 |
# File 'lib/invokable.rb', line 85 def invoker_arity instance_method(:call).arity end |