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

Class Method Summary 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.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/shamu/services/result.rb', line 57

def initialize( *values, request: :not_set, entity: :not_set ) # rubocop:disable Metrics/LineLength, Metrics/PerceivedComplexity
  @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
    # Make sure the request captures errors even if the block doesn't
    # check
    request && request.valid?

    @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

Class Method Details

.coerce(value, **args) ⇒ Result

Returns the value coerced to a Shamu::Services::Result.

Returns:



113
114
115
116
117
118
119
# File 'lib/shamu/services/result.rb', line 113

def self.coerce( value, **args )
  if value.is_a?( Result )
    value
  else
    Result.new( *Array.wrap( value ), **args )
  end
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.



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

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

#inspectString

Returns debug friendly string.

Returns:

  • (String)

    debug friendly string



122
123
124
125
126
127
128
129
130
# File 'lib/shamu/services/result.rb', line 122

def inspect # rubocop:disable Metrics/AbcSize
  result = "#<#{ self.class } valid: #{ valid? }"
  result << ", errors: #{ errors.inspect }" if errors.any?
  result << ", entity: #{ entity.inspect }" if entity
  result << ", value: #{ value.inspect }"   if value && value != entity
  result << ", values: #{ values.inspect }" if values.length > 1
  result << ">"
  result
end

#join(result) ⇒ Object

Joins a dependency's result to the result of the request.



107
108
109
110
# File 'lib/shamu/services/result.rb', line 107

def join( result )
  nested_results << result
  append_error_source result
end

#model_nameObject

Delegate model_name to request/entity



95
96
97
# File 'lib/shamu/services/result.rb', line 95

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

#nested_resultsArray<Result>

may have caused the request to fail.

Returns:

  • (Array<Result>)

    results from calling dependent assemblies that



43
44
45
# File 'lib/shamu/services/result.rb', line 43

def nested_results
  @nested_results ||= []
end

#pretty_print(pp) ⇒ String

Returns even friendlier debug string.

Returns:

  • (String)

    even friendlier debug string.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/shamu/services/result.rb', line 133

def pretty_print( pp ) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  pp.object_address_group( self ) do
    pp.breakable " "
    pp.text "valid: "
    pp.pp valid?

    if errors.any?
      pp.comma_breakable
      pp.text "errors:"
      pp.breakable " "
      pp.pp errors
    end

    if entity
      pp.comma_breakable
      pp.text "entity:"
      pp.breakable " "
      pp.pp entity
    end

    if !value.nil? && value != entity
      pp.comma_breakable
      pp.text "value:"
      pp.breakable " "
      pp.pp value
    end

    if values.length > 1
      pp.comma_breakable
      pp.text "values:"
      pp.breakable " "
      pp.pp values - [ value ]
    end
  end
end

#valid!self

Returns:

  • (self)

Raises:



101
102
103
104
# File 'lib/shamu/services/result.rb', line 101

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.



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

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