Class: Defi::Value

Inherits:
Object
  • Object
show all
Defined in:
lib/defi/value.rb

Overview

Represents a result of an operation, encapsulating both successful returns and exceptions. This class intentionally catches all exceptions (Exception) instead of StandardError to maintain complete control over method execution outcomes, particularly in testing contexts. This ensures that system-level exceptions like SystemExit cannot prematurely terminate test execution with potentially false positive results.

Constant Summary collapse

RAISE =
"raise"
RETURN =
"return"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeValue

Note:

This implementation catches Exception instead of StandardError to provide complete control over method execution outcomes, including system-level exceptions when needed.

Initializes a Value object by executing the provided block.

Examples:

Successful execution

value = Defi::Value.new { 1 + 1 }
value.call # => 2
value.raised? # => false

Exception handling

value = Defi::Value.new { raise TypeError, "Invalid type" }
value.raised? # => true
value.object # => #<TypeError: Invalid type>

Yield Returns:

  • (#object_id)

    Result or exception from block execution



34
35
36
37
38
39
40
# File 'lib/defi/value.rb', line 34

def initialize
  @object = yield
  @raised = false
rescue ::Exception => e # rubocop:disable Lint/RescueException
  @object = e
  @raised = true
end

Instance Attribute Details

#object#object_id (readonly)

Returns The returned value or caught exception.

Returns:

  • (#object_id)

    The returned value or caught exception



15
16
17
# File 'lib/defi/value.rb', line 15

def object
  @object
end

Instance Method Details

#call#object_id

Returns the result or raises the captured exception.

Examples:

With successful result

Value.new { "success" }.call # => "success"

With exception

Value.new { raise "error" }.call # raises RuntimeError: error

Returns:

  • (#object_id)

    The operation result

Raises:

  • (Exception)

    The captured exception if raised? is true



52
53
54
55
56
# File 'lib/defi/value.rb', line 52

def call
  raise object if raised?

  object
end

#inspectString

Returns Human-readable representation.

Returns:

  • (String)

    Human-readable representation



64
65
66
# File 'lib/defi/value.rb', line 64

def inspect
  "Value(object: #{object}, raised: #{raised?})"
end

#raised?Boolean

Returns true if execution raised an exception.

Returns:

  • (Boolean)

    true if execution raised an exception



59
60
61
# File 'lib/defi/value.rb', line 59

def raised?
  @raised
end

#to_hHash

Returns Value properties.

Returns:

  • (Hash)

    Value properties



69
70
71
# File 'lib/defi/value.rb', line 69

def to_h
  { raised: raised?, object: }
end

#to_sString

Returns String representation showing outcome type and value.

Returns:

  • (String)

    String representation showing outcome type and value



74
75
76
# File 'lib/defi/value.rb', line 74

def to_s
  "#{raise_or_return} #{object}"
end