Class: SeaFood::Service
- Inherits:
-
Object
- Object
- SeaFood::Service
- Defined in:
- lib/sea_food/service.rb
Defined Under Namespace
Classes: ServiceError, ServiceResult
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Class Method Summary collapse
-
.call(params = {}) ⇒ ServiceResult
Calls the service and handles any exceptions.
- .call!(params = {}) ⇒ Object
Instance Method Summary collapse
-
#call ⇒ Object
The main method to be implemented by subclasses.
-
#initialize(params = {}) ⇒ Service
constructor
Initializes the service.
Constructor Details
#initialize(params = {}) ⇒ Service
Initializes the service.
9 10 11 12 13 14 15 16 17 |
# File 'lib/sea_food/service.rb', line 9 def initialize(params = {}) if SeaFood.configuration.enforce_interface raise NotImplementedError, 'Subclasses must implement the initialize method ' \ 'because `enforce_interface` is set to true' end @params = params @result = ServiceResult.new end |
Instance Attribute Details
#params ⇒ Object (readonly)
Returns the value of attribute params.
5 6 7 |
# File 'lib/sea_food/service.rb', line 5 def params @params end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
5 6 7 |
# File 'lib/sea_food/service.rb', line 5 def result @result end |
Class Method Details
.call(params = {}) ⇒ ServiceResult
Calls the service and handles any exceptions.
23 24 25 26 27 28 29 |
# File 'lib/sea_food/service.rb', line 23 def call(params = {}) service = new(**params) service.call service.result || ServiceResult.new rescue ServiceError => e service.result || e.try(:result) end |
.call!(params = {}) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/sea_food/service.rb', line 31 def call!(params = {}) result = call(params) return result || ServiceResult.new unless result.fail? raise ServiceError, result end |
Instance Method Details
#call ⇒ Object
The main method to be implemented by subclasses.
40 41 42 |
# File 'lib/sea_food/service.rb', line 40 def call raise NotImplementedError, 'Subclasses must implement the call method' end |