# Service Object Rails version (ActiveSupport)
require 'virtus'
# Service
module Service
extend ActiveSupport::Concern
included do
include Virtus.model strict: true
def self.call(*args)
new(*args).call
end
end
end
# Plain Ruby Service Object
require 'virtus'
# Service Module
module Service
def self.included(base)
base.include Virtus.model strict: true
base.extend ClassMethods
end
# ClassMethods
module ClassMethods
def call(*args)
new(*args).call
end
end
end
# Example Service Object
class Greeter
include Service
attribute :message, String
def call
"Hello, #{@message}!"
end
end
# Example usage
Greeter.call message: 'world' # Hello, world!
Very simple way of creating Service Objects for your (Rails) app powered by Virtus
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.