Class: Shamu::Services::Result

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Translation
Defined in:
lib/shamu/services/result.rb

Overview

The result of a Service Request capturing the validation errors recorded while processing the request and the resulting Services::Entities::Entity and Request used.

Attributes collapse

Attributes collapse

Instance Method Summary collapse

Constructor Details

#initialize(*values, request: :not_set, entity: :not_set) ⇒ Result

Returns a new instance of Result.

Parameters:

  • values (Array<Object,#errors>)

    an array of objects that represent the result of the service call. If they respond to #errors those errors will be included in #errors on the result object itself.

  • request (Request) (defaults to: :not_set)

    submitted to the service. If :not_set, uses the first Shamu::Services::Request object found in the values.

  • entity (Entities::Entity) (defaults to: :not_set)

    submitted to the service. If :not_set, uses the first Entity object found in the values.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/shamu/services/result.rb', line 51

def initialize( *values, request: :not_set, entity: :not_set )
  @values = values
  @value  = values.first

  values.each do |source|
    request = source if request == :not_set && source.is_a?( Services::Request )
    entity  = source if entity == :not_set && source.is_a?( Entities::Entity )

    append_error_source source
  end

  unless request == :not_set
    @request = request
    append_error_source request
  end

  unless entity == :not_set
    @entity = entity
    append_error_source entity
  end
end

Instance Attribute Details

#entityEntities::Entity (readonly)

Returns the entity created or changed by the request.

Returns:



19
20
21
# File 'lib/shamu/services/result.rb', line 19

def entity
  @entity
end

#requestRequest (readonly)

Returns the request submitted to the Service.

Returns:



16
17
18
# File 'lib/shamu/services/result.rb', line 16

def request
  @request
end

#valueObject (readonly)

Returns the primary return value of the service call.

Returns:

  • (Object)

    the primary return value of the service call.



32
33
34
# File 'lib/shamu/services/result.rb', line 32

def value
  @value
end

#valuesArray<Object> (readonly)

Returns the values returned by the service call.

Returns:

  • (Array<Object>)

    the values returned by the service call.



29
30
31
# File 'lib/shamu/services/result.rb', line 29

def values
  @values
end

Instance Method Details

#entity!Entities::Entity

Returns the entity created or changed by the request.

Returns:

Raises:



23
24
25
26
# File 'lib/shamu/services/result.rb', line 23

def entity!
  valid!
  entity
end

#errorsActiveModel::Errors

Returns errors gathered from all the validation sources. Typically the #request and #entity.

Returns:

  • (ActiveModel::Errors)

    errors gathered from all the validation sources. Typically the #request and #entity.



80
81
82
# File 'lib/shamu/services/result.rb', line 80

def errors
  @errors ||= ActiveModel::Errors.new( self )
end

#model_nameObject

Delegate model_name to request/entity



85
86
87
# File 'lib/shamu/services/result.rb', line 85

def model_name
  ( request && request.model_name ) || ( entity && entity.model_name ) || ActiveModel::Name.new( self, nil, "Request" ) # rubocop:disable Metrics/LineLength
end

#valid!self

Returns:

  • (self)

Raises:



91
92
93
94
# File 'lib/shamu/services/result.rb', line 91

def valid!
  raise ServiceRequestFailedError, self unless valid?
  self
end

#valid?Boolean

Returns true if there were not recorded errors.

Returns:

  • (Boolean)

    true if there were not recorded errors.



74
75
76
# File 'lib/shamu/services/result.rb', line 74

def valid?
  errors.empty?
end

#value!Object

Returns the primary return value of the service call.

Returns:

  • (Object)

    the primary return value of the service call.

Raises:



36
37
38
39
# File 'lib/shamu/services/result.rb', line 36

def value!
  valid!
  value
end