Class: Defi::Value
- Inherits:
-
Object
- Object
- Defi::Value
- Defined in:
- lib/defi/value.rb
Overview
Represents a result of an operation, encapsulating either the returned value or an exception raised during the execution.
This class is used to handle the outcome of a method invocation, allowing to distinguish between successful results and exceptions.
Constant Summary collapse
- RAISE =
"raise"
- RETURN =
"return"
Instance Attribute Summary collapse
-
#object ⇒ #object_id
readonly
The returned or the raised object.
Instance Method Summary collapse
-
#call ⇒ #object_id
Returns the object if no exception was raised, otherwise raises the exception.
-
#initialize ⇒ Value
constructor
Initializes a Value object with the result of the provided block.
-
#inspect ⇒ String
Returns a human-readable representation of the Value object.
-
#raised? ⇒ Boolean
Checks if an exception was raised during the execution.
-
#to_h ⇒ Hash
Returns a hash containing the properties of the Value object.
-
#to_s ⇒ String
Returns a string representation of the Value object.
Constructor Details
#initialize ⇒ Value
Initializes a Value object with the result of the provided block. Captures any exception raised during the block execution.
rubocop:disable Lint/RescueException
29 30 31 32 33 34 35 |
# File 'lib/defi/value.rb', line 29 def initialize @object = yield @raised = false rescue ::Exception => e @object = e @raised = true end |
Instance Attribute Details
#object ⇒ #object_id (readonly)
Returns The returned or the raised object.
14 15 16 |
# File 'lib/defi/value.rb', line 14 def object @object end |
Instance Method Details
#call ⇒ #object_id
Returns the object if no exception was raised, otherwise raises the exception.
45 46 47 48 49 |
# File 'lib/defi/value.rb', line 45 def call raise object if raised? object end |
#inspect ⇒ String
Returns a human-readable representation of the Value object.
82 83 84 |
# File 'lib/defi/value.rb', line 82 def inspect "Value(object: #{object}, raised: #{raised?})" end |
#raised? ⇒ Boolean
Checks if an exception was raised during the execution.
58 59 60 |
# File 'lib/defi/value.rb', line 58 def raised? @raised end |
#to_h ⇒ Hash
Returns a hash containing the properties of the Value object.
65 66 67 68 69 70 |
# File 'lib/defi/value.rb', line 65 def to_h { raised: raised?, object: } end |
#to_s ⇒ String
Returns a string representation of the Value object.
75 76 77 |
# File 'lib/defi/value.rb', line 75 def to_s "#{raise_or_return} #{object}" end |