Class: Ballast::Service
- Inherits:
-
Object
- Object
- Ballast::Service
- Defined in:
- lib/ballast/service.rb
Overview
A class which implements a common abstraction for services.
Defined Under Namespace
Classes: Response
Instance Attribute Summary (collapse)
-
- (Object|NilClass) owner
readonly
The owner of this service.
Class Method Summary (collapse)
-
+ (Response) call(operation = :perform, owner: nil, raise_errors: false, params: {}, **kwargs, &block)
Invokes one of the operations exposed by the service.
-
+ (Object) fail!(details, on_validation: false)
Marks the failure of the operation.
-
+ (Response) handle_failure(failure, raise_errors)
Handles a failure.
Instance Method Summary (collapse)
-
- (Response) call(operation = :perform, owner: nil, raise_errors: false, params: {}, **kwargs, &block)
Invokes one of the operations exposed by the service.
-
- (Object) fail!(details, on_validation: false)
Marks the failure of the operation.
-
- (Service) initialize(owner = nil)
constructor
Creates a service object.
Constructor Details
- (Service) initialize(owner = nil)
Creates a service object.
109 110 111 |
# File 'lib/ballast/service.rb', line 109 def initialize(owner = nil) @owner = owner end |
Instance Attribute Details
- (Object|NilClass) owner (readonly)
Returns The owner of this service.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/ballast/service.rb', line 11 class Service # A response to a service invocation. # # @attribute [r] success # @return [Boolean] Whether the invocation was successful or not. # @attribute [r] data # @return [Object] The data returned by the operation. # @attribute [r] errors # @return [Array] The errors returned by the operation. class Response attr_reader :success, :data, :errors # Creates a new service response. # # @param success [Boolean] Whether the invocation was successful or not. # @param data [Object|NilClass] The data returned by the operation. # @param errors [Array|NilClass] The errors returned by the operation. # @param error [Object|NilClass] Alias for errors. *Ignored if `errors` is present.* def initialize(success = true, data: nil, errors: nil, error: nil) errors ||= error.ensure_array @success = success.to_boolean @data = data @errors = errors.ensure_array(no_duplicates: true, compact: true) end # Returns whether the invocation was successful or not. # # @return [Boolean] `true` if the service invocation was successful, `false` otherwise. def success? # TODO@PI: Ignore rubocop on this @success end alias_method :successful?, :success? alias_method :succeeded?, :success? # Returns whether the invocation failed or not. # # @return [Boolean] `true` if the service invocation failed, `false` otherwise. def fail? !@success end alias_method :failed?, :fail? # Returns the first error returned by the operation. # # @return [Object] The first error returned by the service. def error @errors.first end # Converts this response to a AJAX response. # # @param transport [Object|NilClass] The transport to use for sending. Must respond to `render`, `params`, `request.format` and `performed?`. # @return [AjaxResponse] The AJAX response, which will include only the first error. def as_ajax_response(transport = nil) status, = if successful? [:ok, nil] elsif error.is_a?(Hash) [error[:status], error[:error]] else [:unknown, error] end AjaxResponse.new(status: status, data: data, error: , transport: transport) end end attr_reader :owner # 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. 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 # Marks the failure of the operation. # # @param details [Object] The error(s) occurred. # @param on_validation [Boolean] Whether the error(s) was/were validation error(s). def self.fail!(details, on_validation: false) raise(on_validation ? Errors::ValidationFailure : Errors::Failure, details) end # Creates a service object. # # @param owner [Object|NilClass] The owner of the service. def initialize(owner = nil) @owner = owner end # 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. 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 # Marks the failure of the operation. # # @param details [Object] The error(s) occurred. # @param on_validation [Boolean] Whether the error(s) was/were validation error(s). def fail!(details, on_validation: false) self.class.fail!(details, on_validation: on_validation) end # 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. def self.handle_failure(failure, raise_errors) raise_errors ? raise(failure) : Response.new(false, error: failure.details) end end |
Class Method Details
+ (Response) call(operation = :perform, owner: nil, raise_errors: false, params: {}, **kwargs, &block)
Invokes one of the operations exposed by the service.
91 92 93 94 95 96 |
# 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 |
+ (Object) fail!(details, on_validation: false)
Marks the failure of the operation.
102 103 104 |
# File 'lib/ballast/service.rb', line 102 def self.fail!(details, on_validation: false) raise(on_validation ? Errors::ValidationFailure : Errors::Failure, details) end |
+ (Response) handle_failure(failure, raise_errors)
Handles a failure.
144 145 146 |
# File 'lib/ballast/service.rb', line 144 def self.handle_failure(failure, raise_errors) raise_errors ? raise(failure) : Response.new(false, error: failure.details) end |
Instance Method Details
- (Response) call(operation = :perform, owner: nil, raise_errors: false, params: {}, **kwargs, &block)
Invokes one of the operations exposed by the service.
122 123 124 125 126 127 128 129 |
# File 'lib/ballast/service.rb', line 122 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 |
- (Object) fail!(details, on_validation: false)
Marks the failure of the operation.
135 136 137 |
# File 'lib/ballast/service.rb', line 135 def fail!(details, on_validation: false) self.class.fail!(details, on_validation: on_validation) end |