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

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

#actionvoid

This method returns an undefined value.

Override this method and put the main logic of the service here.

Raises:

  • (NotImplementedError)

    If the method is not overridden.



40
41
42
# File 'lib/nueca_rails_interfaces/v3/service_interface.rb', line 40

def action
  raise NotImplementedError, 'Requires implementation of action.'
end

#dataObject

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.

Returns:

  • (Object)

    Data of the service.

Raises:

  • (NotImplementedError)

    If the method is not overridden.



49
50
51
# File 'lib/nueca_rails_interfaces/v3/service_interface.rb', line 49

def data
  raise NotImplementedError, 'Requires implementation of data.'
end

#performObject

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.

Returns:

  • (Object)

    Data of the service.



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.

Returns:

  • (Boolean)

    True or False



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.

Returns:

  • (Boolean, nil)

    True or False or nil



62
63
64
# File 'lib/nueca_rails_interfaces/v3/service_interface.rb', line 62

def success?
  @success
end