Class: Defi::Value
- Inherits:
-
Object
- Object
- Defi::Value
- 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
-
#object ⇒ #object_id
readonly
The returned value or caught exception.
Instance Method Summary collapse
-
#call ⇒ #object_id
Returns the result or raises the captured exception.
-
#initialize ⇒ Value
constructor
Initializes a Value object by executing the provided block.
-
#inspect ⇒ String
Human-readable representation.
-
#raised? ⇒ Boolean
True if execution raised an exception.
-
#to_h ⇒ Hash
Value properties.
-
#to_s ⇒ String
String representation showing outcome type and value.
Constructor Details
#initialize ⇒ Value
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.
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.
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.
52 53 54 55 56 |
# File 'lib/defi/value.rb', line 52 def call raise object if raised? object end |
#inspect ⇒ String
Returns 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.
59 60 61 |
# File 'lib/defi/value.rb', line 59 def raised? @raised end |
#to_h ⇒ Hash
Returns Value properties.
69 70 71 |
# File 'lib/defi/value.rb', line 69 def to_h { raised: raised?, object: } end |
#to_s ⇒ String
Returns 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 |