Module: NuecaRailsInterfaces::V3::ServiceInterface
- Defined in:
- lib/nueca_rails_interfaces/v3/service_interface.rb
Overview
V3 Service Interface adds a way for developers to determine if it was ran successfully or not. When performing the service, it will immediately return the data. It will also assign the success? attribute of the class. The action method should return a boolean value. Warnings feature is now also removed.
Class Method Summary collapse
Instance Method Summary collapse
-
#action ⇒ void
Override this method and put the main logic of the service here.
-
#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.
-
#success? ⇒ Boolean?
Returns the service’s status if it performed successfully or not.
Class Method Details
.included(base) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/nueca_rails_interfaces/v3/service_interface.rb', line 12 def included(base) # 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| 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/v3/service_interface.rb', line 40 def action raise NotImplementedError, 'Requires implementation of action.' 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/v3/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.
28 29 30 31 32 33 34 35 |
# File 'lib/nueca_rails_interfaces/v3/service_interface.rb', line 28 def perform unless performed? @success = action.present? @performed = true end data end |
#performed? ⇒ Boolean
Checks if the service has been performed. Do not override.
55 56 57 |
# File 'lib/nueca_rails_interfaces/v3/service_interface.rb', line 55 def performed? performed end |
#success? ⇒ Boolean?
Returns the service’s status if it performed successfully or not. Nil return means that the service is still not performed.
62 63 64 |
# File 'lib/nueca_rails_interfaces/v3/service_interface.rb', line 62 def success? @success end |