Class: BBServices::Service
- Inherits:
-
Object
- Object
- BBServices::Service
- Defined in:
- lib/bbservices/service.rb
Overview
The lightweight service object provided by BBServices. The basic functionality includes:
-
Ability to access user defined initalization. E.g. initalize(one = 1, two = 2)
-
Ability to chain services via ‘.then’
-
Ability to check success / failure
-
Ability to store errors thrown via services
Class Method Summary collapse
-
.run(*args, **kwargs, &block) ⇒ BBServices.Service
Creates the service instances and calls run upon said instance.
-
.run!(*args, **kwargs, &block) ⇒ BBServices.Service
Creates the service instances and calls run! upon said instance.
Instance Method Summary collapse
- #error ⇒ Object
-
#error? ⇒ Boolean
Returns true / false if the service threw an error.
- #errors ⇒ Object
-
#failed? ⇒ Boolean
Returns true/false on if the service was unsuccessful.
-
#failure {|_self| ... } ⇒ Object
Calls the given block if the service failed.
-
#on(success: proc {}, failure: proc {}) ⇒ Boolean
Calls success on success?, failure on !success?.
-
#ran? ⇒ Boolean
Returns true/false on if the service has been ran.
-
#run(&block) ⇒ BBServices.Service
(also: #call)
Runs the service using ‘safe’ execution.
-
#run!(&block) ⇒ BBServices.Service
(also: #call!)
Runs the service using ‘unsafe’ execution.
- #run? ⇒ Boolean
-
#success {|_self| ... } ⇒ Object
Calls the given block if the service was successful.
-
#successful? ⇒ Boolean
(also: #succeeded?)
Returns true/false on if the service was successful.
-
#then(*args, &block) ⇒ Object
Returns a ServiceChain with the service as a registered service.
Class Method Details
.run(*args, **kwargs, &block) ⇒ BBServices.Service
Creates the service instances and calls run upon said instance
21 22 23 24 25 |
# File 'lib/bbservices/service.rb', line 21 def run(*args, **kwargs, &block) new(*args, **kwargs).tap do |service| service.run(&block) end end |
.run!(*args, **kwargs, &block) ⇒ BBServices.Service
Creates the service instances and calls run! upon said instance
31 32 33 34 35 |
# File 'lib/bbservices/service.rb', line 31 def run!(*args, **kwargs, &block) new(*args, **kwargs).tap do |service| service.run!(&block) end end |
Instance Method Details
#error ⇒ Object
130 131 132 |
# File 'lib/bbservices/service.rb', line 130 def error errors.first end |
#error? ⇒ Boolean
Returns true / false if the service threw an error.
126 127 128 |
# File 'lib/bbservices/service.rb', line 126 def error? errors.count > 0 end |
#errors ⇒ Object
134 135 136 |
# File 'lib/bbservices/service.rb', line 134 def errors @errors ||= [] end |
#failed? ⇒ Boolean
Returns true/false on if the service was unsuccessful. This will always be the inverse of successful?
98 99 100 |
# File 'lib/bbservices/service.rb', line 98 def failed? (@successful == nil ? false : !@successful) end |
#failure {|_self| ... } ⇒ Object
Calls the given block if the service failed
108 109 110 |
# File 'lib/bbservices/service.rb', line 108 def failure(&block) yield(self) if failed? end |
#on(success: proc {}, failure: proc {}) ⇒ Boolean
Calls success on success?, failure on !success?
116 117 118 119 120 121 122 |
# File 'lib/bbservices/service.rb', line 116 def on(success: proc {}, failure: proc {}) if successful? success.call elsif failed? failure.call end end |
#ran? ⇒ Boolean
Returns true/false on if the service has been ran
82 83 84 |
# File 'lib/bbservices/service.rb', line 82 def ran? !!@ran end |
#run(&block) ⇒ BBServices.Service Also known as: call
Runs the service using ‘safe’ execution. The @run variable will be set to true, then the run method will be called
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/bbservices/service.rb', line 42 def run(&block) begin @ran = true successful = on_run set_successful(successful == nil ? true : !!successful) rescue => e set_successful(false) register_error(e) ensure call_block(block) if block_given? end end |
#run!(&block) ⇒ BBServices.Service Also known as: call!
Runs the service using ‘unsafe’ execution. The @run variable will be set to true, then the run! method will be called
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/bbservices/service.rb', line 59 def run!(&block) begin @ran = true successful = on_run! set_successful(successful == nil ? true : !!successful) call_block(block) if block_given? rescue => e set_successful(false) register_error(e) raise e end end |
#run? ⇒ Boolean
86 87 88 |
# File 'lib/bbservices/service.rb', line 86 def run? !!@ran end |
#success {|_self| ... } ⇒ Object
Calls the given block if the service was successful
103 104 105 |
# File 'lib/bbservices/service.rb', line 103 def success(&block) yield(self) if succeeded? end |
#successful? ⇒ Boolean Also known as: succeeded?
Returns true/false on if the service was successful.
92 93 94 |
# File 'lib/bbservices/service.rb', line 92 def successful? (@successful == nil ? false : @successful) end |
#then(*args, &block) ⇒ Object
Returns a ServiceChain with the service as a registered service. The service must have been ran in order to call this chaining method.
74 75 76 77 78 |
# File 'lib/bbservices/service.rb', line 74 def then(*args, &block) raise BBServices::ServiceMustRunBeforeChainingError if !self.ran? BBServices::ServiceChain.new(self).then(block) end |