module Shamu::Services::ObservableSupport

Adds the ability for other services to observe requests on the service.

In contrast to {Events} that are async and independent, observers are called immediately when a request is performed and may influence the behavior of the request.

See {#ObserverSupport} for details on observing other services.

Public Instance Methods

observe( &block ) click to toggle source

Ask to be notified of important actions as they are executed on the service.

@yield (observed_request) @yieldparam [ObservedRequest] action

# File lib/shamu/services/observable_support.rb, line 18
def observe( &block )
  @observers ||= []
  @observers << block
end

Private Instance Methods

notify_observers( observed_action ) click to toggle source

@!visibility public

Notify all registered observers about the pending request. @param [ObservedAction] observed_action

# File lib/shamu/services/observable_support.rb, line 53
def notify_observers( observed_action )
  return unless defined? @observers

  @observers.each do |observer|
    observer.call( observed_action )
  end
end
with_observers( request ) { |request| ... } click to toggle source

@!visibility public

Invoke a block notifying observers before the action is performed allowing them to modify inputs or request the action be canceled.

@param [Request] request the service request @return [Result] @yield [Request]

# File lib/shamu/services/observable_support.rb, line 33
def with_observers( request, &block )
  observed = ObservedRequest.new( request: request )
  notify_observers( observed )

  returned =
    if observed.cancel_requested?
      request.error :base, :canceled
    else
      yield( request )
    end

  result = Result.coerce( returned, request: request )

  observed.complete( result, false )
end
with_partial_request( *args ) { |wrapped_request| ... } click to toggle source

Override {Shamu::Services::RequestSupport#with_partial_request} to make all requests observable. {#audit_request audit the request}.

Calls superclass method
# File lib/shamu/services/observable_support.rb, line 63
def with_partial_request( *args, &block )
  super( *args ) do |request|
    with_observers request do |wrapped_request|
      yield wrapped_request
    end
  end
end