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(&block) ⇒ Object
(also: #then)
Creates a new chain in the service with block, returns the chain instance for method chaining.
-
#error? ⇒ Boolean
Returns a true / false value if an error has been thrown, this will be passed to the last_service if one is avalible, otherwise false will be returned.
-
#errors ⇒ Object
Returns all of the errors from the last_service, if no last_service is avalible then an empty array will be returned.
-
#failed? ⇒ Boolean
Returns true/false on if the service was unsuccessful.
-
#failure {|_self| ... } ⇒ Object
Calls the given block if the chain failed.
-
#initialize(service) ⇒ ServiceChain
constructor
Initializes the ServiceChain.
-
#last_service ⇒ Object
Returns the last service which was ran.
-
#on(success: proc {}, failure: proc {}) ⇒ Boolean
Calls success on success?, failure on !success?.
-
#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 service was successful.
Constructor Details
#initialize(service) ⇒ ServiceChain
Initializes the ServiceChain
12 13 14 15 16 17 18 |
# File 'lib/bbservices/service_chain.rb', line 12 def initialize(service) raise NilServiceInChainError if service.nil? @successful = nil @services = [service] 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(&block) ⇒ Object Also known as: then
Creates a new chain in the service with block, returns the chain instance for method chaining. The block should be used to call the next service and recieves the following params:
-
BBServices::ServiceChain (self)
23 24 25 26 27 |
# File 'lib/bbservices/service_chain.rb', line 23 def chain(&block) self.tap do |c| c.send(:_chain, block) end end |
#error? ⇒ Boolean
Returns a true / false value if an error has been thrown, this will be passed to the last_service if one is avalible, otherwise false will be returned
81 82 83 |
# File 'lib/bbservices/service_chain.rb', line 81 def error? last_service ? last_service.error? : false end |
#errors ⇒ Object
Returns all of the errors from the last_service, if no last_service is avalible then an empty array will be returned
87 88 89 |
# File 'lib/bbservices/service_chain.rb', line 87 def errors last_service ? last_service.errors : [] end |
#failed? ⇒ Boolean
Returns true/false on if the service was unsuccessful. This will always be the inverse of successful?
51 52 53 |
# File 'lib/bbservices/service_chain.rb', line 51 def failed? (@successful == nil ? false : !@successful) end |
#failure {|_self| ... } ⇒ Object
Calls the given block if the chain failed
61 62 63 |
# File 'lib/bbservices/service_chain.rb', line 61 def failure yield(self) if failed? end |
#last_service ⇒ Object
Returns the last service which was ran. This will return the last service, if the previous chain returned a non-service instance
33 34 35 |
# File 'lib/bbservices/service_chain.rb', line 33 def last_service @services.last end |
#on(success: proc {}, failure: proc {}) ⇒ Boolean
Calls success on success?, failure on !success?
69 70 71 72 73 74 75 |
# File 'lib/bbservices/service_chain.rb', line 69 def on(success: proc {}, failure: proc {}) if successful? success.call else failure.call end end |
#succeeded? ⇒ Boolean
Returns true/false on if the chain did succeed.
39 40 41 |
# File 'lib/bbservices/service_chain.rb', line 39 def succeeded? successful? end |
#success {|_self| ... } ⇒ Object
Calls the given block if the chain was successful
56 57 58 |
# File 'lib/bbservices/service_chain.rb', line 56 def success yield(self) if succeeded? end |
#successful? ⇒ Boolean
Returns true/false on if the service was successful.
45 46 47 |
# File 'lib/bbservices/service_chain.rb', line 45 def successful? (@successful == nil ? false : @successful) end |