Skip to content

Instantly share code, notes, and snippets.

@cainlevy
cainlevy / service_base.rb
Last active November 8, 2019 19:02
Simple base class for service objects that implement 1) an initializer and 2) a perform method that returns a Promise
# Service objects are classes that inherit from this base and implement two instance methods:
# * initialize(*args, **params) => void
# * perform() => Promise
class ServiceBase
include ActiveModel::Validations
# invokes the service and returns a Promise
def self.perform(*args, **params)
klass = params.any? ? new(*args, **params) : new(*args)
klass.perform_in_transaction