Module: NuecaRailsInterfaces::V1::ServiceInterface
- Defined in:
- lib/nueca_rails_interfaces/v1/service_interface.rb
Overview
Service Mixin Interface. Include from this module when creating a new service. In this version, a service is defined as an absolute reusable piece of code. They should not be related to views and presentation of data; they are instead processors. Because of this, errors encountered should be raised as exceptions naturally. Errors here are treated as bad data. Services are not responsible for handling bad data. Services assume all data passed is correct and valid. Services should no longer validate these data. Services can still store warnings; these warnings are sent to Sentry, although not yet implemented. All services will have a ‘perform` method that will call the `action` method. This is the invoker of the service. All services will have an `action` method that will contain the main logic of the service. All services will have a `data` method that will contain the resulting data that the service produced. Developers will mainly override `action` method and `data method`.
Class Method Summary collapse
Instance Method Summary collapse
-
#action ⇒ Object
Override this method and put the main logic of the service here.
-
#add_warning(warning_message) ⇒ Array<String>
Method used to add a warning.
-
#data ⇒ Object
Override this method and put the resulting data of the service here.
-
#perform ⇒ self
This is the main method of the service.
-
#performed? ⇒ Boolean
Checks if the service has been performed.
-
#warnings ⇒ Array<String>
This should contain all the warnings that the service has encountered.
-
#warnings? ⇒ Boolean
Used to check if the service has encountered any warnings.
Class Method Details
.included(_) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/nueca_rails_interfaces/v1/service_interface.rb', line 17 def self.included(_) raise NuecaRailsInterfaces::DeprecatedError # Rails.logger&.warn( # <<~MSG # ################################################# # # DEPRECATION WARNING # # # V1::ServiceInterface will be deprecated soon. # # # Please use V2::ServiceInterface instead. # # ################################################# # MSG # ) end |
Instance Method Details
#action ⇒ Object
Override this method and put the main logic of the service here.
46 47 48 |
# File 'lib/nueca_rails_interfaces/v1/service_interface.rb', line 46 def action raise NotImplementedError, 'Requires implementation of action.' end |
#add_warning(warning_message) ⇒ Array<String>
Method used to add a warning. Do not override.
61 62 63 64 |
# File 'lib/nueca_rails_interfaces/v1/service_interface.rb', line 61 def add_warning() _warnings << warnings end |
#data ⇒ Object
Override this method and put the resulting data of the service here. If blank, then return an empty hash manually. Reason being is for readability’s sake in the services.
54 55 56 |
# File 'lib/nueca_rails_interfaces/v1/service_interface.rb', line 54 def data raise NotImplementedError, 'Requires implementation of data.' end |
#perform ⇒ self
This is the main method of the service. This is the method that should be called to perform the service. Do not override this method. Instead, override the ‘action` method.
34 35 36 37 38 39 40 41 42 |
# File 'lib/nueca_rails_interfaces/v1/service_interface.rb', line 34 def perform unless performed? action @performed = true process_warnings end self end |
#performed? ⇒ Boolean
Checks if the service has been performed. Do not override.
68 69 70 |
# File 'lib/nueca_rails_interfaces/v1/service_interface.rb', line 68 def performed? performed end |
#warnings ⇒ Array<String>
This should contain all the warnings that the service has encountered. Intentionally made to be frozen to avoid free modification. Do not override this method.
82 83 84 |
# File 'lib/nueca_rails_interfaces/v1/service_interface.rb', line 82 def warnings _warnings.dup.freeze end |
#warnings? ⇒ Boolean
Used to check if the service has encountered any warnings. Do not override.
74 75 76 |
# File 'lib/nueca_rails_interfaces/v1/service_interface.rb', line 74 def warnings? _warnings.any? end |