Class: ServiceRecord::Base
- Inherits:
-
Object
- Object
- ServiceRecord::Base
- 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
-
#result ⇒ Object
Returns the value of attribute result.
Class Method Summary collapse
-
.perform(args = {}) ⇒ Object
Wrapper around the perform instance method that runs all the validations and callbacks before eventually calling perform.
-
.perform!(args = {}) ⇒ Object
Wapper around the perform class method that raises exception if service fails.
Instance Method Summary collapse
-
#perform ⇒ Object
Each subclass must define the perform method.
Instance Attribute Details
#result ⇒ Object
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
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
#perform ⇒ Object
Each subclass must define the perform method
47 48 49 |
# File 'lib/service_record/base.rb', line 47 def perform raise NotImplementedError end |