28
loading...
This website collects cookies to deliver better user experience
result = CheckoutService.new(item: item, user: user).call
if result.successful?
...
result = CheckoutService.(item: item, user: user)
if result.successful?
...
#call
method".class ApplicationService
def self.call(...)
new(...).()
end
def call
raise "Not Implemented"
end
end
def self.call(...)
new(...).()
end
self#call
. It doesn't care what arguments it receives (see this footnote if you're wondering what (...)
does), it just forwards them to new()
and then immediately invokes #call
by chaining .()
behind it.def call
raise "Not Implemented"
end
#call
- we raise an error if it isn't implemented.class FooService < ApplicationService
def initialize(name: )
@name = name
end
def call
"Hello #{@name}"
end
end
ApplicationService
needs to adhere to the #call
class signature, and can be invoked with the shorter method.FooService.(name: "Dan")
=> "Hello Dan"
module FunctionalObject
module ClassMethods
def call(...)
new(...).()
end
end
def self.included(klass)
klass.extend(ClassMethods)
end
def call
raise NotImplementedError
end
end
class ApplicationService
includes FunctionalObject
end
(...)
operator can be used to forward all arguments from a forwarding method to a concrete method.def forwarding_method(...)
concrete_method(...)
end