class Shamu::Services::ObservedRequest

Describes request that will be/has been performed by a service and the associated data properties.

Attributes

cancel_reason[R]

@return [Result] the result of a dependency that asked for the request to be canceled.

Public Instance Methods

cancel_requested?() click to toggle source

@return [Boolean] true if an observer has asked the request to be canceled.

# File lib/shamu/services/observed_request.rb, line 35
def cancel_requested?
  !!cancel_reason
end
complete( result, canceled ) click to toggle source

Mark the action as complete and run any {#on_success} or #{on_fail} callbacks.

@param [Result] result the result of the action. If valid success callbacks are invoked. @param [Boolean] canceled true if the action was canceled and not processed.

@return [Result] the result of all the observed callbacks.

# File lib/shamu/services/observed_request.rb, line 55
def complete( result, canceled )
  invoke_callbacks( result, @on_cancel_blocks ) if canceled

  result
end
on_canceled( &block ) click to toggle source

Execute block if the action was canceled by another observer. @yield(result) @yieldresult [Result]

# File lib/shamu/services/observed_request.rb, line 42
def on_canceled( &block )
  @on_cancel_blocks ||= []
  @on_cancel_blocks << block
end
request_cancel( result = Result.new ) click to toggle source

Ask that the service cancel the request.

@return [Result] a nested result that should be reported for why the request was canceled.

# File lib/shamu/services/observed_request.rb, line 29
def request_cancel( result = Result.new )
  @cancel_reason = result
end

Private Instance Methods

invoke_callbacks( result, callbacks ) click to toggle source
# File lib/shamu/services/observed_request.rb, line 63
def invoke_callbacks( result, callbacks )
  return unless callbacks.present?

  callbacks.each do |callback|
    nested = callback.call( result )
    result.join( nested ) if nested
  end

  result
end