Class: RubyPi::Tools::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_pi/tools/result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, success:, value: nil, error: nil, duration_ms: 0.0) ⇒ Result

Creates a new Result instance.

Parameters:

  • name (String, Symbol)

    The name of the tool that produced this result.

  • success (Boolean)

    Whether the tool executed successfully.

  • value (Object, nil) (defaults to: nil)

    The return value from the tool (on success).

  • error (String, nil) (defaults to: nil)

    The error message (on failure).

  • duration_ms (Float) (defaults to: 0.0)

    How long the tool took to execute, in milliseconds.



46
47
48
49
50
51
52
# File 'lib/ruby_pi/tools/result.rb', line 46

def initialize(name:, success:, value: nil, error: nil, duration_ms: 0.0)
  @name = name.to_s
  @success = success
  @value = value
  @error = error
  @duration_ms = duration_ms.to_f
end

Instance Attribute Details

#duration_msFloat (readonly)

Returns The execution time in milliseconds.

Returns:

  • (Float)

    The execution time in milliseconds.



37
38
39
# File 'lib/ruby_pi/tools/result.rb', line 37

def duration_ms
  @duration_ms
end

#errorString? (readonly)

Returns The error message if execution failed (nil if successful).

Returns:

  • (String, nil)

    The error message if execution failed (nil if successful).



34
35
36
# File 'lib/ruby_pi/tools/result.rb', line 34

def error
  @error
end

#nameString (readonly)

Returns The name of the tool that was executed.

Returns:

  • (String)

    The name of the tool that was executed.



28
29
30
# File 'lib/ruby_pi/tools/result.rb', line 28

def name
  @name
end

#valueObject? (readonly)

Returns The return value of the tool (nil if execution failed).

Returns:

  • (Object, nil)

    The return value of the tool (nil if execution failed).



31
32
33
# File 'lib/ruby_pi/tools/result.rb', line 31

def value
  @value
end

Instance Method Details

#inspectString

Provides a human-readable string representation of the result.

Returns:

  • (String)

    A summary string for debugging/logging.



77
78
79
80
# File 'lib/ruby_pi/tools/result.rb', line 77

def inspect
  status = @success ? "success" : "failure"
  "#<RubyPi::Tools::Result name=#{@name.inspect} status=#{status} duration_ms=#{@duration_ms}>"
end

#success?Boolean

Returns whether the tool execution was successful.

Returns:

  • (Boolean)

    true if the tool completed without error.



57
58
59
# File 'lib/ruby_pi/tools/result.rb', line 57

def success?
  @success
end

#to_hHash

Returns a hash representation of the result, useful for serialization.

Returns:

  • (Hash)

    A hash containing all result attributes.



64
65
66
67
68
69
70
71
72
# File 'lib/ruby_pi/tools/result.rb', line 64

def to_h
  {
    name: @name,
    success: @success,
    value: @value,
    error: @error,
    duration_ms: @duration_ms
  }
end