Class: SeaFood::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/sea_food/service.rb

Defined Under Namespace

Classes: ServiceError, ServiceResult

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Service

Initializes the service.

Parameters:

  • params (Hash) (defaults to: {})

    The parameters to be passed to the service.



9
10
11
12
13
14
15
16
17
# File 'lib/sea_food/service.rb', line 9

def initialize(params = {})
  if SeaFood.configuration.enforce_interface
    raise NotImplementedError,
          'Subclasses must implement the initialize method ' \
          'because `enforce_interface` is set to true'
  end
  @params = params
  @result = ServiceResult.new
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'lib/sea_food/service.rb', line 5

def params
  @params
end

#resultObject (readonly)

Returns the value of attribute result.



5
6
7
# File 'lib/sea_food/service.rb', line 5

def result
  @result
end

Class Method Details

.call(params = {}) ⇒ ServiceResult

Calls the service and handles any exceptions.

Parameters:

  • args (Hash)

    Arguments to pass to the service.

Returns:



23
24
25
26
27
28
29
# File 'lib/sea_food/service.rb', line 23

def call(params = {})
  service = new(**params)
  service.call
  service.result || ServiceResult.new
rescue ServiceError => e
  service.result || e.try(:result)
end

.call!(params = {}) ⇒ Object

Raises:



31
32
33
34
35
36
# File 'lib/sea_food/service.rb', line 31

def call!(params = {})
  result = call(params)
  return result || ServiceResult.new unless result.fail?

  raise ServiceError, result
end

Instance Method Details

#callObject

The main method to be implemented by subclasses.

Raises:

  • (NotImplementedError)


40
41
42
# File 'lib/sea_food/service.rb', line 40

def call
  raise NotImplementedError, 'Subclasses must implement the call method'
end