Module: NuecaRailsInterfaces::V2::ServiceInterface
- Defined in:
- lib/nueca_rails_interfaces/v2/service_interface.rb
Overview
V2 Service Interface is the same as V1 Service Interface, except it is more straightforward. When performing the service, it will immediately return the data
Class Method Summary collapse
Instance Method Summary collapse
-
#action ⇒ void
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 ⇒ Object
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(base) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/nueca_rails_interfaces/v2/service_interface.rb', line 9 def included(base) raise NuecaRailsInterfaces::DeprecatedError # This is the main method of the service in a class context. # This is the method that should be called to perform the service statically. # Use this instead if the service instance is not needed. # Do not override this method. Instead, override the `action` method. Returns the data immediately. # @return [Object] Data of the service. base.define_singleton_method(:perform) do |*arguments| # rubocop:disable Lint/UnreachableCode instance = NuecaRailsInterfaces::Util.process_class_arguments(self, *arguments) instance.perform end end |
Instance Method Details
#action ⇒ void
This method returns an undefined value.
Override this method and put the main logic of the service here.
40 41 42 |
# File 'lib/nueca_rails_interfaces/v2/service_interface.rb', line 40 def action raise NotImplementedError, 'Requires implementation of action.' end |
#add_warning(warning_message) ⇒ Array<String>
Method used to add a warning. Do not override.
56 57 58 59 |
# File 'lib/nueca_rails_interfaces/v2/service_interface.rb', line 56 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.
49 50 51 |
# File 'lib/nueca_rails_interfaces/v2/service_interface.rb', line 49 def data raise NotImplementedError, 'Requires implementation of data.' end |
#perform ⇒ Object
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. Returns the data immediately.
27 28 29 30 31 32 33 34 35 |
# File 'lib/nueca_rails_interfaces/v2/service_interface.rb', line 27 def perform unless performed? action @performed = true process_warnings end data end |
#performed? ⇒ Boolean
Checks if the service has been performed. Do not override.
63 64 65 |
# File 'lib/nueca_rails_interfaces/v2/service_interface.rb', line 63 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.
77 78 79 |
# File 'lib/nueca_rails_interfaces/v2/service_interface.rb', line 77 def warnings _warnings.dup.freeze end |
#warnings? ⇒ Boolean
Used to check if the service has encountered any warnings. Do not override.
69 70 71 |
# File 'lib/nueca_rails_interfaces/v2/service_interface.rb', line 69 def warnings? _warnings.any? end |