Class: Shamu::Services::Result
- Inherits:
-
Object
- Object
- Shamu::Services::Result
- Extended by:
- ActiveModel::Translation
- Defined in:
- lib/shamu/services/result.rb
Overview
Attributes collapse
-
#entity ⇒ Entities::Entity
readonly
The entity created or changed by the request.
-
#request ⇒ Request
readonly
The request submitted to the Service.
-
#value ⇒ Object
readonly
The primary return value of the service call.
-
#values ⇒ Array<Object>
readonly
The values returned by the service call.
Attributes collapse
-
#entity! ⇒ Entities::Entity
The entity created or changed by the request.
-
#nested_results ⇒ Array<Result>
may have caused the request to fail.
-
#value! ⇒ Object
The primary return value of the service call.
Class Method Summary collapse
-
.coerce(value, **args) ⇒ Result
The value coerced to a Result.
Instance Method Summary collapse
-
#errors ⇒ ActiveModel::Errors
Errors gathered from all the validation sources.
-
#initialize(*values, request: :not_set, entity: :not_set) ⇒ Result
constructor
A new instance of Result.
-
#inspect ⇒ String
Debug friendly string.
-
#join(result) ⇒ Object
Joins a dependency's result to the result of the request.
-
#model_name ⇒ Object
Delegate model_name to request/entity.
-
#pretty_print(pp) ⇒ String
Even friendlier debug string.
- #valid! ⇒ self
-
#valid? ⇒ Boolean
True if there were not recorded errors.
Constructor Details
#initialize(*values, request: :not_set, entity: :not_set) ⇒ Result
Returns a new instance of Result.
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
#entity ⇒ Entities::Entity (readonly)
Returns the entity created or changed by the request.
19 20 21 |
# File 'lib/shamu/services/result.rb', line 19 def entity @entity end |
#request ⇒ Request (readonly)
Returns the request submitted to the Service.
16 17 18 |
# File 'lib/shamu/services/result.rb', line 16 def request @request end |
#value ⇒ Object (readonly)
Returns the primary return value of the service call.
32 33 34 |
# File 'lib/shamu/services/result.rb', line 32 def value @value end |
#values ⇒ Array<Object> (readonly)
Returns 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.
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.
23 24 25 26 |
# File 'lib/shamu/services/result.rb', line 23 def entity! valid! entity end |
#errors ⇒ ActiveModel::Errors
90 91 92 |
# File 'lib/shamu/services/result.rb', line 90 def errors @errors ||= ActiveModel::Errors.new( self ) end |
#inspect ⇒ String
Returns 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_name ⇒ Object
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_results ⇒ Array<Result>
may have caused the request to fail.
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.
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
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.
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.
36 37 38 39 |
# File 'lib/shamu/services/result.rb', line 36 def value! valid! value end |