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.
-
#failure(data = nil) ⇒ Result::Failure
deprecated
Deprecated.
Use #Failure instead.
-
#result(condition, data = nil) ⇒ Result::Success, Result::Failure
deprecated
Deprecated.
Use #Check instead.
-
#run ⇒ Result::Success, Result::Failure
This method is where the main work of your service must be.
-
#success(data = nil) ⇒ Result::Success
deprecated
Deprecated.
Use #Success instead.
-
#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.
191 192 193 194 195 196 197 |
# File 'lib/f_service/base.rb', line 191 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.
161 162 163 |
# File 'lib/f_service/base.rb', line 161 def Failure(*types, data: nil) Result::Failure.new(data, types) end |
#failure(data = nil) ⇒ Result::Failure
251 252 253 254 255 256 257 258 259 |
# File 'lib/f_service/base.rb', line 251 def failure(data = nil) FService.deprecate!( name: "#{self.class}##{__method__}", alternative: '#Failure', from: caller[0] ) Result::Failure.new(data) end |
#result(condition, data = nil) ⇒ Result::Success, Result::Failure
285 286 287 288 289 290 291 292 293 |
# File 'lib/f_service/base.rb', line 285 def result(condition, data = nil) FService.deprecate!( name: "#{self.class}##{__method__}", alternative: '#Check', from: caller[0] ) condition ? success(data) : failure(data) 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(data = nil) ⇒ Result::Success
101 102 103 104 105 106 107 108 109 |
# File 'lib/f_service/base.rb', line 101 def success(data = nil) FService.deprecate!( name: "#{self.class}##{__method__}", alternative: '#Success', from: caller[0] ) Result::Success.new(data) 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.
134 135 136 |
# File 'lib/f_service/base.rb', line 134 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.
225 226 227 228 229 230 231 |
# File 'lib/f_service/base.rb', line 225 def Try(*types, catch: StandardError) res = yield Success(*types, data: res) rescue *catch => e Failure(*types, data: e) end |