Class: RailsAiBridge::Service::Result

Inherits:
Object
  • Object
show all
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.

Examples:

Success Result

result = Service::Result.new(true, data: {users: ["alice", "bob"]})
result.success? # => true
result.data      # => {users: ["alice", "bob"]}

Failure Result

result = Service::Result.new(false, errors: ["Invalid input"])
result.failure? # => true
result.errors   # => ["Invalid input"]

Chaining

result.on_success { |r| puts "Success: #{r.data}" }
       .on_failure { |r| puts "Error: #{r.errors.first}" }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(success, data: nil, errors: nil, metadata: nil) ⇒ Result

Initialize a new result object.

Parameters:

  • success (Boolean)

    whether the operation succeeded

  • data (Object) (defaults to: nil)

    result data (nil for failures)

  • errors (Array<String>, String, nil) (defaults to: nil)

    error messages; nil means no errors (same as empty). Stored as a duplicated, frozen Array so defaults are not shared across instances.

  • metadata (Hash, nil) (defaults to: nil)

    additional metadata; nil is treated like {}. A duplicate is frozen so the caller's Hash is never frozen in place.



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

#dataObject (readonly)

Returns the value of attribute data.



26
27
28
# File 'lib/rails_ai_bridge/service/result.rb', line 26

def data
  @data
end

#errorsObject (readonly)

Returns the value of attribute errors.



26
27
28
# File 'lib/rails_ai_bridge/service/result.rb', line 26

def errors
  @errors
end

#metadataObject (readonly)

Returns the value of attribute metadata.



26
27
28
# File 'lib/rails_ai_bridge/service/result.rb', line 26

def 
  @metadata
end

#successObject (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.

Returns:

  • (Boolean)

    true if 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.

Yields:

  • (Result)

    the result object

Yield Parameters:

  • result (Result)

    the result object

Returns:

  • (Result)

    self for 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.

Yields:

  • (Result)

    the result object

Yield Parameters:

  • result (Result)

    the result object

Returns:

  • (Result)

    self for 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.

Returns:

  • (Boolean)

    true if successful



46
47
48
# File 'lib/rails_ai_bridge/service/result.rb', line 46

def success?
  success
end

#to_hHash

Convert result to hash representation.

Returns:

  • (Hash)

    hash with success, data, errors, and metadata



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