Class: SingleActionService::Base
- Inherits:
-
Object
- Object
- SingleActionService::Base
- Includes:
- ModuleHelper
- Defined in:
- lib/single_action_service/base.rb
Overview
Parent class for services. A service is an object that implements part of the business logic. Create an inheritor to use it and call ‘success’ or ‘error’ methods to return a result object.
Class Method Summary collapse
- .create_result_class ⇒ Object
- .define_methods_to_create_error_results ⇒ Object
-
.errors(errors_data = nil) ⇒ Object
Call this method to generate methods in the service to return specific errors.
- .parse_errors(errors_data) ⇒ Object
- .result_class ⇒ Object
Methods included from ModuleHelper
#constantize, #module_parent, #module_parent_name
Class Method Details
.create_result_class ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/single_action_service/base.rb', line 49 def create_result_class demodulized_name = name.split('::').last result_class_name = "#{demodulized_name}Result" return if module_parent.const_defined?(result_class_name) # Programmatically create the inheritor for the service result object # with autogenerated methods for checking for errors. errors = @errors @result_class = Class.new(SingleActionService::Result) do def self.define_error_checking_method(error) method_name = "#{error.name}_error?" define_method(method_name) do error_code == error.code end end errors.each do |error| define_error_checking_method(error) end end module_parent.const_set(result_class_name, @result_class) end |
.define_methods_to_create_error_results ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/single_action_service/base.rb', line 74 def define_methods_to_create_error_results @errors.each do |error_object| result_method_name = "#{error_object.name}_error" define_method(result_method_name) do |data = nil| error(code: error_object.code, data: data) end end end |
.errors(errors_data = nil) ⇒ Object
Call this method to generate methods in the service to return specific errors.
Each hash can contain keys: :name => A symbol representing a name of the error. :code => A symbol representing an error code of the error.
For each name, a method “#name_error” will be generated to return a result with the corresponding error code. The returned result will have “#name_error?” methods for checking for a specific error.
For example, if you pass an array: [{ name: :already_exists, code: :‘errors.already_exists’ }], the ‘already_exists_error’ method will be generated to return the result with a :‘errors.already_exists’ code. You can check for the error by calling ‘already_exists_error?’ method on the result object.
35 36 37 38 39 40 41 |
# File 'lib/single_action_service/base.rb', line 35 def errors(errors_data = nil) return @errors if errors_data.nil? parse_errors(errors_data) create_result_class define_methods_to_create_error_results end |
.parse_errors(errors_data) ⇒ Object
43 44 45 46 47 |
# File 'lib/single_action_service/base.rb', line 43 def parse_errors(errors_data) @errors = errors_data.map do |error_data| SingleActionService::ServiceError.new(**error_data) end end |
.result_class ⇒ Object
83 84 85 |
# File 'lib/single_action_service/base.rb', line 83 def result_class @result_class ||= SingleActionService::Result end |