Class: SeaFood::Service::ServiceResult

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(success: true, data: nil, errors: nil) ⇒ ServiceResult

Initializes the ServiceResult.

Parameters:

  • success (Boolean) (defaults to: true)

    Indicates if the service call was successful.

  • data (Any) (defaults to: nil)

    The data returned by the service.



90
91
92
93
94
# File 'lib/sea_food/service.rb', line 90

def initialize(success: true, data: nil, errors: nil)
  @success = success
  @data = (data || {}).with_indifferent_access
  @errors = (errors.to_h || {}).with_indifferent_access
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key) ⇒ Object

:rubocop:disable Style/MissingRespondToMissing



113
114
115
116
117
118
119
# File 'lib/sea_food/service.rb', line 113

def method_missing(key)
  if succeeded?
    @data[key]
  else
    @errors[key]
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



85
86
87
# File 'lib/sea_food/service.rb', line 85

def data
  @data
end

#errorsObject (readonly)

Returns the value of attribute errors.



85
86
87
# File 'lib/sea_food/service.rb', line 85

def errors
  @errors
end

#successObject (readonly)

Returns the value of attribute success.



85
86
87
# File 'lib/sea_food/service.rb', line 85

def success
  @success
end

Instance Method Details

#failed?Boolean Also known as: fail?

Checks if the service call failed.

Returns:

  • (Boolean)

    True if failed, false otherwise.



106
107
108
# File 'lib/sea_food/service.rb', line 106

def failed?
  !success
end

#succeeded?Boolean Also known as: success?

Checks if the service call was successful.

Returns:

  • (Boolean)

    True if successful, false otherwise.



98
99
100
# File 'lib/sea_food/service.rb', line 98

def succeeded?
  success
end