class Sebastian::Base

Provides service functionality. Subclass this to create an service.

Example:

class ExampleService < Sebastian::Base
  # Required
  attr_accessor :foo
  attr_accessor :bar

  def execute
    foo + bar
  end
end

result = ExampleService.perform(foo: 10, bar: 5)
if result.ok?
  result.value
else
  result.errors
end

Public Class Methods

new(attributes = {}) click to toggle source
# File lib/sebastian/base.rb, line 41
def initialize(attributes = {})
  assign_attributes(attributes)
end
perform(attributes = {}) click to toggle source
# File lib/sebastian/base.rb, line 29
def perform(attributes = {})
  new(attributes).perform
end
perform!(attributes = {}) click to toggle source
# File lib/sebastian/base.rb, line 33
def perform!(attributes = {})
  new(attributes).perform!
end

Public Instance Methods

perform() click to toggle source
# File lib/sebastian/base.rb, line 45
def perform
  Result.new(value: valid? ? execute : nil, errors: errors)
end
perform!() click to toggle source
# File lib/sebastian/base.rb, line 49
def perform!
  perform.value!
end

Protected Instance Methods

execute() click to toggle source
# File lib/sebastian/base.rb, line 55
def execute
  raise NotImplementedError, "Must implement #execute in #{self.class} to make the service work"
end
ok?() click to toggle source
# File lib/sebastian/base.rb, line 59
def ok?
  errors.empty?
end