Module: Callable::ClassMethods
- Defined in:
- lib/callable/mixin.rb
Instance Method Summary collapse
-
#call(*args) {|optional| ... } ⇒ Object
Instantiate the class and immediately invoke its instance
#call. -
#to_proc ⇒ Proc
A proc that delegates to
.call, enabling ‘&MyService` shorthand.
Instance Method Details
#call(*args) {|optional| ... } ⇒ Object
Instantiate the class and immediately invoke its instance #call.
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/callable/mixin.rb', line 42 def call(*args, &block) inst = new(*args) rescue ArgumentError => e # Bubble up anything that isn't exactly ArgumentError (e.g., subclasses) raise unless e.instance_of?(ArgumentError) raise ConstructionError, "Cannot instantiate #{name}: #{e.}", e.backtrace else # Ensure the instance defines `#call` unless inst.respond_to?(:call) raise NotImplementedError, "#{name} must implement #call" end inst.call(&block) end |
#to_proc ⇒ Proc
Returns a proc that delegates to .call, enabling ‘&MyService` shorthand.
58 59 60 |
# File 'lib/callable/mixin.rb', line 58 def to_proc method(:call).to_proc end |