Class: BBServices::ServiceChain

Inherits:
Object
  • Object
show all
Defined in:
lib/bbservices/service_chain.rb

Overview

Container for chained services.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#servicesObject (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

Returns:

  • (Boolean)

    true/false on if an error has occurred



81
82
83
# File 'lib/bbservices/service_chain.rb', line 81

def error?
  last_service ? last_service.error? : false
end

#errorsObject

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?

Returns:

  • (Boolean)

    true/false on if the service failed.



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

Yields:

  • (_self)

Yield Parameters:



61
62
63
# File 'lib/bbservices/service_chain.rb', line 61

def failure
  yield(self) if failed?
end

#last_serviceObject

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?

Parameters:

  • success (Proc) (defaults to: proc {})

    The proc to be called upon a successful chain

  • failure (Proc) (defaults to: proc {})

Returns:

  • (Boolean)

    true/false if the chain has any params



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.

Returns:

  • (Boolean)

    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

Yields:

  • (_self)

Yield Parameters:



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.

Returns:

  • (Boolean)

    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