class SeaFood::Service
def call(params = {})
-
(ServiceResult)
- The result of the service call.
Parameters:
-
args
(Hash
) -- Arguments to pass to the service.
def call(params = {}) service = new(**params) service.call service.result || ServiceResult.new rescue ServiceError => e service.result || e.try(:result) end
def call
def call raise NotImplementedError, 'Subclasses must implement the call method' end
def call!(params = {})
def call!(params = {}) result = call(params) return result || ServiceResult.new unless result.fail? raise ServiceError, result end
def fail(errors = nil)
-
data
(Any
) -- Optional data to be returned in the result.
def fail(errors = nil) @result = ServiceResult.new(success: false, errors: errors) end
def fail!(errors = nil)
-
errors
(Any
) -- Errors to be returned in the result.
def fail!(errors = nil) @result = ServiceResult.new(success: false, errors: errors) raise ServiceError, @result end
def fail_and_merge(errors)
def fail_and_merge(errors) @result = if @result.blank? ServiceResult.new(success: false, errors: errors) else ServiceResult.new(success: false, errors: errors.merge!(@result.errors)) end end
def initialize(params = {})
-
params
(Hash
) -- The parameters to be passed to the service.
def initialize(params = {}) if SeaFood.configuration.enforce_interface raise NotImplementedError, 'Subclasses must implement the initialize method ' \ 'because `enforce_interface` is set to true' end @params = params @result = ServiceResult.new end
def success(data = nil)
-
data
(Any
) -- Optional data to be returned in the result.
def success(data = nil) @result = ServiceResult.new(success: true, data: data) end
def validate_pipeline(pipeline)
def validate_pipeline(pipeline) pipeline.each do |key, form| fail_and_merge({ key => form.errors.messages }) unless form.valid? end fail!(@result.errors) if @result.failed? end
def validate_with(key, form)
-
The
({ key: form [ActiveModel::Model] }
) -- form objects to validate.
def validate_with(key, form) fail({ key => form.errors.messages }) unless form.valid? end
def validate_with!(key, form)
-
The
({ key: form [ActiveModel::Model] }
) -- form objects to validate.
def validate_with!(key, form) fail!({ key => form.errors.messages }) unless form.valid? end