class Ballast::Service

A class which implements a common abstraction for services.

@attribute [r] owner

@return [Object|NilClass] The owner of this service.

Attributes

owner[R]

Public Class Methods

call(operation = :perform, owner: nil, raise_errors: false, params: {}, **kwargs, &block) click to toggle source

Invokes one of the operations exposed by the service.

@param operation [String] The operation to invoke. @param owner [Object|NilClass] The owner of the service. @param raise_errors [Boolean] Whether to raise errors instead of returning a failure. @param params [Hash] The parameters to pass to the service. @param kwargs [Hash] Other modifiers to pass to the service. @param block [Proc] A lambda to pass to the service. @return [Response] The response of the service.

# File lib/ballast/service.rb, line 91
def self.call(operation = :perform, owner: nil, raise_errors: false, params: {}, **kwargs, &block)
  fail!(status: 501, error: "Unsupported operation #{self}.#{operation}.") unless respond_to?(operation)
  Response.new(true, data: send(operation, owner: owner, params: params, **kwargs, &block))
rescue Errors::Failure => failure
  handle_failure(failure, raise_errors)
end
fail!(details) click to toggle source

Marks the failure of the operation.

@param details [Object] The error(s) occurred.

# File lib/ballast/service.rb, line 101
def self.fail!(details)
  raise(Errors::Failure, details)
end
fail_validation!(details) click to toggle source

Marks the failure of the validation of the operation.

@param details [Object] The error(s) occurred.

# File lib/ballast/service.rb, line 108
def self.fail_validation!(details)
  raise(Errors::ValidationFailure, details)
end
handle_failure(failure, raise_errors) click to toggle source

Handles a failure.

@param failure [Failure] The failure to handle. @param raise_errors [Boolean] If `true` it will simply raise the error, otherwise it will return a failure as as Service::Response. @return [Response] A failure response.

# File lib/ballast/service.rb, line 156
def self.handle_failure(failure, raise_errors)
  raise_errors ? raise(failure) : Response.new(false, error: failure.details)
end
new(owner = nil) click to toggle source

Creates a service object.

@param owner [Object|NilClass] The owner of the service.

# File lib/ballast/service.rb, line 115
def initialize(owner = nil)
  @owner = owner
end

Public Instance Methods

call(operation = :perform, owner: nil, raise_errors: false, params: {}, **kwargs, &block) click to toggle source

Invokes one of the operations exposed by the service.

@param operation [String] The operation to invoke. @param owner [Object|NilClass] The owner of the service. @param raise_errors [Boolean] Whether to raise errors instead of returning a failure. @param params [Hash] The parameters to pass to the service. @param kwargs [Hash] Other modifiers to pass to the service. @param block [Proc] A lambda to pass to the service. @return [Response] The response of the service.

# File lib/ballast/service.rb, line 128
def call(operation = :perform, owner: nil, raise_errors: false, params: {}, **kwargs, &block)
  # PI: Ignore Roodi on this method
  @owner = owner if owner
  fail!(status: 501, error: "Unsupported operation #{self.class}##{operation}.") unless respond_to?(operation)
  Response.new(true, data: send(operation, params: params, **kwargs, &block))
rescue Errors::Failure => failure
  self.class.send(:handle_failure, failure, raise_errors)
end
fail!(details) click to toggle source

Marks the failure of the operation.

@param details [Object] The error(s) occurred.

# File lib/ballast/service.rb, line 140
def fail!(details)
  raise(Errors::Failure, details)
end
fail_validation!(details) click to toggle source

Marks the failure of the validation of the operation.

@param details [Object] The error(s) occurred.

# File lib/ballast/service.rb, line 147
def fail_validation!(details)
  raise(Errors::ValidationFailure, details)
end