Class: RailsAiBridge::Service::Result
- Inherits:
-
Object
- Object
- RailsAiBridge::Service::Result
- Defined in:
- lib/rails_ai_bridge/service/result.rb
Overview
Result object returned by all service calls.
Provides a standardized way to return success/failure states with associated data and error messages. Supports method chaining via on_success/on_failure. #errors and #metadata are defensive copies, frozen, so callers do not share mutable default state and cannot mutate the internal arrays/hashes in place.
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.
-
#success ⇒ Object
readonly
Returns the value of attribute success.
Instance Method Summary collapse
-
#failure? ⇒ Boolean
Check if the operation failed.
-
#initialize(success, data: nil, errors: nil, metadata: nil) ⇒ Result
constructor
Initialize a new result object.
-
#on_failure {|Result| ... } ⇒ Result
Execute block if failed, enabling method chaining.
-
#on_success {|Result| ... } ⇒ Result
Execute block if successful, enabling method chaining.
-
#success? ⇒ Boolean
Check if the operation was successful.
-
#to_h ⇒ Hash
Convert result to hash representation.
Constructor Details
#initialize(success, data: nil, errors: nil, metadata: nil) ⇒ Result
Initialize a new result object.
36 37 38 39 40 41 |
# File 'lib/rails_ai_bridge/service/result.rb', line 36 def initialize(success, data: nil, errors: nil, metadata: nil) @success = success @data = data @errors = Array(errors).dup.freeze @metadata = ( || {}).dup.freeze end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
26 27 28 |
# File 'lib/rails_ai_bridge/service/result.rb', line 26 def data @data end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
26 27 28 |
# File 'lib/rails_ai_bridge/service/result.rb', line 26 def errors @errors end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
26 27 28 |
# File 'lib/rails_ai_bridge/service/result.rb', line 26 def @metadata end |
#success ⇒ Object (readonly)
Returns the value of attribute success.
26 27 28 |
# File 'lib/rails_ai_bridge/service/result.rb', line 26 def success @success end |
Instance Method Details
#failure? ⇒ Boolean
Check if the operation failed.
53 54 55 |
# File 'lib/rails_ai_bridge/service/result.rb', line 53 def failure? !success end |
#on_failure {|Result| ... } ⇒ Result
Execute block if failed, enabling method chaining.
73 74 75 76 77 |
# File 'lib/rails_ai_bridge/service/result.rb', line 73 def on_failure(&block) return self unless failure? && block tap(&block) end |
#on_success {|Result| ... } ⇒ Result
Execute block if successful, enabling method chaining.
62 63 64 65 66 |
# File 'lib/rails_ai_bridge/service/result.rb', line 62 def on_success(&block) return self unless success && block tap(&block) end |
#success? ⇒ Boolean
Check if the operation was successful.
46 47 48 |
# File 'lib/rails_ai_bridge/service/result.rb', line 46 def success? success end |
#to_h ⇒ Hash
Convert result to hash representation.
82 83 84 85 86 87 88 89 |
# File 'lib/rails_ai_bridge/service/result.rb', line 82 def to_h { success: success, data: data, errors: errors, metadata: } end |