Class: BBServices::Service
- Inherits:
-
Object
- Object
- BBServices::Service
- Defined in:
- lib/bbservices/service.rb
Overview
The lightweight service object provided by BBServices.
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#params ⇒ Object
Returns the value of attribute params.
Class Method Summary collapse
-
.call ⇒ BBServices.Service
Creates the service instances and calls run upon said instance An alias to Service‘s run method.
-
.call! ⇒ BBServices.Service
Creates the service instances and calls run! upon said instance An alias to Service‘s run! method.
-
.internal_service_class ⇒ Class
Gets the current service class.
-
.run(params = {}, &block) ⇒ BBServices.Service
Creates the service instances and calls run upon said instance.
-
.run!(params = {}, &block) ⇒ BBServices.Service
Creates the service instances and calls run! upon said instance.
-
.service_class(klass) ⇒ BBServices.Service
Sets the service class on the Class.
Instance Method Summary collapse
-
#error? ⇒ Boolean
Returns true / false if the service threw an error.
-
#failed? ⇒ Boolean
Returns true/false on if the service was unsuccessful.
-
#failure {|_self| ... } ⇒ Object
Calls the given block if the service failed.
-
#initialize(params = {}) ⇒ Service
constructor
Initializes the service with a hash of params.
-
#number_of_params ⇒ Number
Gets the number of params.
-
#on(success: proc {}, failure: proc {}) ⇒ Boolean
Calls success on success?, failure on !success?.
-
#param(key) ⇒ Hash
Gets a single param using a key.
-
#param_for(key) ⇒ Hash
Gets a single param using a key.
-
#params? ⇒ Boolean
Returns true / false if the service has any params.
-
#ran? ⇒ Boolean
(also: #run?)
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.
-
#service_class ⇒ Class
Gets the current service class.
-
#service_class=(new_service_class) ⇒ Object
Sets the service_class on the instance.
-
#set_params(new_params) ⇒ Object
Sets the params variable (@params) on the service.
-
#set_service_class(new_service_class) ⇒ Object
Sets the service_class on the instance.
-
#succeeded? ⇒ Boolean
Returns true/false on if the service did succeed.
-
#success {|_self| ... } ⇒ Object
Calls the given block if the service was successful.
-
#successful? ⇒ Boolean
Returns true/false on if the service was successful.
Constructor Details
#initialize(params = {}) ⇒ Service
Initializes the service with a hash of params
62 63 64 65 66 67 68 69 70 |
# File 'lib/bbservices/service.rb', line 62 def initialize(params = {}) @object = nil @successful = false @ran = false @error = nil @service_class = nil @params = params end |
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
16 17 18 |
# File 'lib/bbservices/service.rb', line 16 def error @error end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
16 17 18 |
# File 'lib/bbservices/service.rb', line 16 def object @object end |
#params ⇒ Object
Returns the value of attribute params.
16 17 18 |
# File 'lib/bbservices/service.rb', line 16 def params @params end |
Class Method Details
.call ⇒ BBServices.Service
Creates the service instances and calls run upon said instance An alias to BBServices::Service‘s run method
41 42 43 44 45 |
# File 'lib/bbservices/service.rb', line 41 def run(params = {}, &block) new(params).tap do |service| service.run(&block) end end |
.call! ⇒ BBServices.Service
Creates the service instances and calls run! upon said instance An alias to BBServices::Service‘s run! method
44 45 46 47 48 |
# File 'lib/bbservices/service.rb', line 44 def run!(params = {}, &block) new(params).tap do |service| service.run!(&block) end end |
.internal_service_class ⇒ Class
Gets the current service class
55 56 57 |
# File 'lib/bbservices/service.rb', line 55 def internal_service_class @service_class end |
.run(params = {}, &block) ⇒ BBServices.Service
Creates the service instances and calls run upon said instance
24 25 26 27 28 |
# File 'lib/bbservices/service.rb', line 24 def run(params = {}, &block) new(params).tap do |service| service.run(&block) end end |
.run!(params = {}, &block) ⇒ BBServices.Service
Creates the service instances and calls run! upon said instance
34 35 36 37 38 |
# File 'lib/bbservices/service.rb', line 34 def run!(params = {}, &block) new(params).tap do |service| service.run!(&block) end end |
.service_class(klass) ⇒ BBServices.Service
Sets the service class on the Class. Please note this is an internal method.
49 50 51 |
# File 'lib/bbservices/service.rb', line 49 def service_class(klass) @service_class = klass end |
Instance Method Details
#error? ⇒ Boolean
Returns true / false if the service threw an error
222 223 224 |
# File 'lib/bbservices/service.rb', line 222 def error? !!@error end |
#failed? ⇒ Boolean
Returns true/false on if the service was unsuccessful. This will always be the inverse of successful?
194 195 196 |
# File 'lib/bbservices/service.rb', line 194 def failed? !succeeded? end |
#failure {|_self| ... } ⇒ Object
Calls the given block if the service failed
204 205 206 |
# File 'lib/bbservices/service.rb', line 204 def failure yield(self) if failed? end |
#number_of_params ⇒ Number
Gets the number of params
161 162 163 |
# File 'lib/bbservices/service.rb', line 161 def number_of_params @params ? @params.length : 0 end |
#on(success: proc {}, failure: proc {}) ⇒ Boolean
Calls success on success?, failure on !success?
212 213 214 215 216 217 218 |
# File 'lib/bbservices/service.rb', line 212 def on(success: proc {}, failure: proc {}) if successful? success.call else failure.call end end |
#param(key) ⇒ Hash
Gets a single param using a key
155 156 157 |
# File 'lib/bbservices/service.rb', line 155 def param(key) @params[key] if @params end |
#param_for(key) ⇒ Hash
Gets a single param using a key
148 149 150 |
# File 'lib/bbservices/service.rb', line 148 def param_for(key) param(key) end |
#params? ⇒ Boolean
Returns true / false if the service has any params
173 174 175 |
# File 'lib/bbservices/service.rb', line 173 def params? !!(@params && @params.length) end |
#ran? ⇒ Boolean Also known as: run?
Returns true/false on if the service has been ran
167 168 169 |
# File 'lib/bbservices/service.rb', line 167 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, initialize_service and run_service will then be called.
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/bbservices/service.rb', line 76 def run(&block) set_ran begin initialize_service run_service rescue => e set_successful(false) set_error(e) ensure call_block(&block) end end |
#run!(&block) ⇒ BBServices.Service Also known as: call!
Runs the service using ‘unsafe’ execution. The @run variable will be set to true, initialize_service and run_service will then be called.
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/bbservices/service.rb', line 93 def run!(&block) set_ran begin initialize_service run_service! call_block(&block) rescue => e set_successful(false) set_error(e) raise e end end |
#service_class ⇒ Class
Gets the current service class. This will use @service_class if set, otherwise will fallback to self.class.internal_service_class.
127 128 129 |
# File 'lib/bbservices/service.rb', line 127 def service_class @service_class ||= self.class.internal_service_class end |
#service_class=(new_service_class) ⇒ Object
Sets the service_class on the instance. This will override the self.class.internal_service_class.
120 121 122 |
# File 'lib/bbservices/service.rb', line 120 def service_class=(new_service_class) set_service_class(new_service_class) end |
#set_params(new_params) ⇒ Object
Sets the params variable (@params) on the service.
133 134 135 136 137 |
# File 'lib/bbservices/service.rb', line 133 def set_params(new_params) raise BBServices::ServiceHashTypeError unless new_params.is_a?(Hash) @params = new_params end |
#set_service_class(new_service_class) ⇒ Object
Sets the service_class on the instance. This will override the self.class.internal_service_class.
114 115 116 |
# File 'lib/bbservices/service.rb', line 114 def set_service_class(new_service_class) @service_class = new_service_class end |
#succeeded? ⇒ Boolean
Returns true/false on if the service did succeed.
182 183 184 |
# File 'lib/bbservices/service.rb', line 182 def succeeded? successful? end |
#success {|_self| ... } ⇒ Object
Calls the given block if the service was successful
199 200 201 |
# File 'lib/bbservices/service.rb', line 199 def success yield(self) if succeeded? end |
#successful? ⇒ Boolean
Returns true/false on if the service was successful.
188 189 190 |
# File 'lib/bbservices/service.rb', line 188 def successful? @successful end |