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

#initializeServiceChain

Initializes the ServiceChain



11
12
13
14
# File 'lib/bbservices/service_chain.rb', line 11

def initialize
  @services = []
  @successful = true
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(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

#errorObject



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

Returns:

  • (Boolean)

    true/false on if an error has occurred



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?

Returns:

  • (Boolean)

    true/false on if the chain failed.



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

Yields:

  • (_self)

Yield Parameters:



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?

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



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_serviceObject



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.

Returns:

  • (Boolean)

    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

Yields:

  • (_self)

Yield Parameters:



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.

Returns:

  • (Boolean)

    true/false on if the chain was successful.



39
40
41
# File 'lib/bbservices/service_chain.rb', line 39

def successful?
  @successful
end