Class: ServiceRecord::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::AttributeAssignment, ActiveModel::Attributes, ActiveModel::Validations, Callbacks
Defined in:
lib/service_record/base.rb

Overview

Base class to be extended by all service classes

class MyService < ServiceRecord end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#resultObject

Returns the value of attribute result.



20
21
22
# File 'lib/service_record/base.rb', line 20

def result
  @result
end

Class Method Details

.perform(args = {}) ⇒ Object

Wrapper around the perform instance method that runs all the validations and callbacks before eventually calling perform.



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/service_record/base.rb', line 24

def self.perform(args = {})
  new.tap do |service|
    service.attributes = args

    if service.valid?
      service.run_callbacks :perform do
        service.result = service.perform
      end
    end

    return Response.new(service.result, service.errors)
  end
end

.perform!(args = {}) ⇒ Object

Wapper around the perform class method that raises exception if service fails

Raises:



39
40
41
42
43
44
# File 'lib/service_record/base.rb', line 39

def self.perform!(args = {})
  service = perform(args)
  return service if service.success?

  raise Failure, service
end

Instance Method Details

#performObject

Each subclass must define the perform method

Raises:

  • (NotImplementedError)


47
48
49
# File 'lib/service_record/base.rb', line 47

def perform
  raise NotImplementedError
end