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

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

#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/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.

Parameters:

  • warning_message (String)

    Descriptive sentence of the warning.

Returns:

  • (Array<String>)

    Array of all warnings.



56
57
58
59
# File 'lib/nueca_rails_interfaces/v2/service_interface.rb', line 56

def add_warning(warning_message)
  _warnings << warning_message
  warnings
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/v2/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.



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.

Returns:

  • (Boolean)

    True or False



63
64
65
# File 'lib/nueca_rails_interfaces/v2/service_interface.rb', line 63

def performed?
  performed
end

#warningsArray<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.

Returns:

  • (Array<String>)

    Array of all warnings.



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.

Returns:

  • (Boolean)

    True or False



69
70
71
# File 'lib/nueca_rails_interfaces/v2/service_interface.rb', line 69

def warnings?
  _warnings.any?
end