class BBServices::ServiceChain
Container for chained services.
Attributes
Public Class Methods
Initializes the ServiceChain
# File lib/bbservices/service_chain.rb, line 11 def initialize @services = [] @successful = true end
Public Instance Methods
# 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
# File lib/bbservices/service_chain.rb, line 77 def error previous_service ? previous_service.error : nil end
Returns true / false if the chain threw an error @return [Boolean] true/false on if an error has occurred
# File lib/bbservices/service_chain.rb, line 73 def error? previous_service ? previous_service.error? : false end
Returns true/false on if the chain was unsuccessful. This will always be the inverse of successful? @return [Boolean] true/false on if the chain failed.
# File lib/bbservices/service_chain.rb, line 45 def failed? !succeeded? end
Calls the given block if the chain failed
# File lib/bbservices/service_chain.rb, line 55 def failure yield(self) if failed? end
Calls success on success?, failure on !success? @param [Proc] success The proc to be called upon a successful chain @param [Proc] failure @return [Boolean] true/false if the chain has any params
# File lib/bbservices/service_chain.rb, line 63 def on(success: proc {}, failure: proc {}) if successful? success.call else failure.call end end
# File lib/bbservices/service_chain.rb, line 25 def previous_service return nil unless @services.length @services.last end
Returns true/false on if the chain did succeed. @return [Boolean] true/false on if the chain did succeed.
# File lib/bbservices/service_chain.rb, line 33 def succeeded? successful? end
Calls the given block if the chain was successful
# File lib/bbservices/service_chain.rb, line 50 def success yield(self) if succeeded? end
Returns true/false on if the chain was successful. @return [Boolean] true/false on if the chain was successful.
# File lib/bbservices/service_chain.rb, line 39 def successful? @successful end
Private Instance Methods
# File lib/bbservices/service_chain.rb, line 83 def process_service(service) if service.is_a?(BBServices::Service) @services << service @successful = service.successful? else @successful = !!service end end