Class: BBServices::ServiceChain
- Inherits:
-
Object
- Object
- BBServices::ServiceChain
- Defined in:
- lib/bbservices/service_chain.rb
Overview
Container for chained services.
Instance Attribute Summary collapse
-
#services ⇒ Object
readonly
Returns the value of attribute services.
Instance Method Summary collapse
- #chain(params = {}) ⇒ Object
- #error ⇒ Object
-
#error? ⇒ Boolean
Returns true / false if the chain threw an error.
-
#failed? ⇒ Boolean
Returns true/false on if the chain was unsuccessful.
-
#failure {|_self| ... } ⇒ Object
Calls the given block if the chain failed.
-
#initialize ⇒ ServiceChain
constructor
Initializes the ServiceChain.
-
#on(success: proc {}, failure: proc {}) ⇒ Boolean
Calls success on success?, failure on !success?.
- #previous_service ⇒ Object
-
#succeeded? ⇒ Boolean
Returns true/false on if the chain did succeed.
-
#success {|_self| ... } ⇒ Object
Calls the given block if the chain was successful.
-
#successful? ⇒ Boolean
Returns true/false on if the chain was successful.
Constructor Details
#initialize ⇒ ServiceChain
Initializes the ServiceChain
11 12 13 14 |
# File 'lib/bbservices/service_chain.rb', line 11 def initialize @services = [] @successful = true end |
Instance Attribute Details
#services ⇒ Object (readonly)
Returns the value of attribute services.
8 9 10 |
# File 'lib/bbservices/service_chain.rb', line 8 def services @services end |
Instance Method Details
#chain(params = {}) ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/bbservices/service_chain.rb', line 16 def chain(params = {}) tap do |_service_chain| if @successful service = yield(params, self, previous_service) process_service(service) end end end |
#error ⇒ Object
77 78 79 |
# File 'lib/bbservices/service_chain.rb', line 77 def error previous_service ? previous_service.error : nil end |
#error? ⇒ Boolean
Returns true / false if the chain threw an error
73 74 75 |
# File 'lib/bbservices/service_chain.rb', line 73 def error? previous_service ? previous_service.error? : false end |
#failed? ⇒ Boolean
Returns true/false on if the chain was unsuccessful. This will always be the inverse of successful?
45 46 47 |
# File 'lib/bbservices/service_chain.rb', line 45 def failed? !succeeded? end |
#failure {|_self| ... } ⇒ Object
Calls the given block if the chain failed
55 56 57 |
# File 'lib/bbservices/service_chain.rb', line 55 def failure yield(self) if failed? end |
#on(success: proc {}, failure: proc {}) ⇒ Boolean
Calls success on success?, failure on !success?
63 64 65 66 67 68 69 |
# File 'lib/bbservices/service_chain.rb', line 63 def on(success: proc {}, failure: proc {}) if successful? success.call else failure.call end end |
#previous_service ⇒ Object
25 26 27 28 29 |
# File 'lib/bbservices/service_chain.rb', line 25 def previous_service return nil unless @services.length @services.last end |
#succeeded? ⇒ Boolean
Returns true/false on if the chain did succeed.
33 34 35 |
# File 'lib/bbservices/service_chain.rb', line 33 def succeeded? successful? end |
#success {|_self| ... } ⇒ Object
Calls the given block if the chain was successful
50 51 52 |
# File 'lib/bbservices/service_chain.rb', line 50 def success yield(self) if succeeded? end |
#successful? ⇒ Boolean
Returns true/false on if the chain was successful.
39 40 41 |
# File 'lib/bbservices/service_chain.rb', line 39 def successful? @successful end |