Class: CommandTower::Services::ServiceResult
- Inherits:
-
Object
- Object
- CommandTower::Services::ServiceResult
- Defined in:
- app/services/command_tower/services/service_result.rb
Constant Summary collapse
- INTERACTOR_INTERNAL_KEYS =
%i[error rollback].freeze
- FAILURE_ONLY_KEYS =
%i[ msg status valid_arguments invalid_arguments invalid_argument_hash invalid_argument_keys application_error service_metadata ].freeze
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
Class Method Summary collapse
- .failure(errors:, data: {}, metadata: {}) ⇒ Object
- .from_interactor_context(context) ⇒ Object
- .success(data: {}, metadata: {}) ⇒ Object
Instance Method Summary collapse
- #failure? ⇒ Boolean
-
#initialize(success:, data:, errors:, metadata:) ⇒ ServiceResult
constructor
A new instance of ServiceResult.
- #success? ⇒ Boolean
Constructor Details
#initialize(success:, data:, errors:, metadata:) ⇒ ServiceResult
Returns a new instance of ServiceResult.
20 21 22 23 24 25 |
# File 'app/services/command_tower/services/service_result.rb', line 20 def initialize(success:, data:, errors:, metadata:) @success = success @data = data @errors = errors @metadata = end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
18 19 20 |
# File 'app/services/command_tower/services/service_result.rb', line 18 def data @data end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
18 19 20 |
# File 'app/services/command_tower/services/service_result.rb', line 18 def errors @errors end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
18 19 20 |
# File 'app/services/command_tower/services/service_result.rb', line 18 def @metadata end |
Class Method Details
.failure(errors:, data: {}, metadata: {}) ⇒ Object
39 40 41 |
# File 'app/services/command_tower/services/service_result.rb', line 39 def self.failure(errors:, data: {}, metadata: {}) new(success: false, data:, errors:, metadata:) end |
.from_interactor_context(context) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/services/command_tower/services/service_result.rb', line 43 def self.from_interactor_context(context) = (context) if context.success? success(data: extract_success_data(context), metadata: ) elsif context.application_error failure(errors: [context.application_error], metadata: ) elsif context.invalid_arguments failure( errors: [ CommandTower::Errors::ValidationError.new(details: context.invalid_argument_hash) ], metadata: ) else failure(errors: [CommandTower::Errors::InternalError.new], metadata: ) end end |
.success(data: {}, metadata: {}) ⇒ Object
35 36 37 |
# File 'app/services/command_tower/services/service_result.rb', line 35 def self.success(data: {}, metadata: {}) new(success: true, data:, errors: [], metadata:) end |
Instance Method Details
#failure? ⇒ Boolean
31 32 33 |
# File 'app/services/command_tower/services/service_result.rb', line 31 def failure? !success? end |
#success? ⇒ Boolean
27 28 29 |
# File 'app/services/command_tower/services/service_result.rb', line 27 def success? @success end |