Class: FService::Base Abstract
- Inherits:
-
Object
- Object
- FService::Base
- Defined in:
- lib/f_service/base.rb
Overview
Abstract base class for services. It provides the basic interface to return and handle results.
Class Method Summary collapse
-
.call(*args) ⇒ Result::Success, Result::Failure
Initializes and runs a new service.
-
.to_proc ⇒ Proc
Allows running a service without explicit giving params.
Instance Method Summary collapse
-
#Check(*types, data: nil) ⇒ Result::Success, Result::Failure
Converts a boolean to a Result.
-
#Failure(*types, data: nil) ⇒ Result::Failure
Returns a failed result.
-
#run ⇒ Result::Success, Result::Failure
This method is where the main work of your service must be.
-
#Success(*types, data: nil) ⇒ Result::Success
Returns a successful result.
-
#Try(*types, catch: StandardError) ⇒ Result::Success, Result::Failure
If the given block raises an exception, it wraps it in a Failure.
Class Method Details
.call(*args) ⇒ Result::Success, Result::Failure
this method shouldn't be overridden in the subclasses
Initializes and runs a new service.
23 24 25 26 27 28 |
# File 'lib/f_service/base.rb', line 23 def call(*args) result = new(*args).run raise(FService::Error, 'Services must return a Result') unless result.is_a? Result::Base result end |
.to_proc ⇒ Proc
Allows running a service without explicit giving params. This is useful when chaining services or mapping inputs to be processed.
48 49 50 |
# File 'lib/f_service/base.rb', line 48 def to_proc proc { |args| call(**args) } end |
Instance Method Details
#Check(*types, data: nil) ⇒ Result::Success, Result::Failure
Converts a boolean to a Result.
Truthy values map to Success, and falsey values map to Failures.
You can optionally provide a types for the result.
The result value defaults as the evaluated value of the given block.
If you want another value you can pass it through the data: argument.
161 162 163 164 165 166 167 |
# File 'lib/f_service/base.rb', line 161 def Check(*types, data: nil) res = yield final_data = data || res res ? Success(*types, data: final_data) : Failure(*types, data: final_data) end |
#Failure(*types, data: nil) ⇒ Result::Failure
Returns a failed result. You can optionally specify types and a value for your result. You'll probably want to return this inside #run.
131 132 133 |
# File 'lib/f_service/base.rb', line 131 def Failure(*types, data: nil) Result::Failure.new(data, types) end |
#run ⇒ Result::Success, Result::Failure
this method SHOULD be overridden in the subclasses
This method is where the main work of your service must be. It is called after initilizing the service and should return an FService::Result.
77 78 79 |
# File 'lib/f_service/base.rb', line 77 def run raise NotImplementedError, 'Services must implement #run' end |
#Success(*types, data: nil) ⇒ Result::Success
Returns a successful result. You can optionally specify a list of types and a value for your result. You'll probably want to return this inside #run.
104 105 106 |
# File 'lib/f_service/base.rb', line 104 def Success(*types, data: nil) Result::Success.new(data, types) end |
#Try(*types, catch: StandardError) ⇒ Result::Success, Result::Failure
If the given block raises an exception, it wraps it in a Failure. Otherwise, maps the block value in a Success object. You can specify which exceptions to watch for. It's possible to provide a types for the result too.
195 196 197 198 199 200 201 |
# File 'lib/f_service/base.rb', line 195 def Try(*types, catch: StandardError) res = yield Success(*types, data: res) rescue *catch => e Failure(*types, data: e) end |