Class: Defi::Value

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

Overview

This class contains an object that returned or raised during the initialize.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeValue

Initialize the value class.

rubocop:disable Lint/RescueException

Yield Returns:

  • (#object_id)

    The challenged code.



14
15
16
17
18
19
20
# File 'lib/defi/value.rb', line 14

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.

Returns:

  • (#object_id)

    The returned or the raised object.



8
9
10
# File 'lib/defi/value.rb', line 8

def object
  @object
end

Instance Method Details

#call#object_id

Raise or return the value.

Returns:

  • (#object_id)

    Raised exception or returned object.



26
27
28
29
30
# File 'lib/defi/value.rb', line 26

def call
  raise object if raised?

  object
end

#inspectString

A string containing a human-readable representation of the value.

Returns:

  • (String)

    The human-readable representation of the value.



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

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

#raised?Boolean

Returns The value was raised (or returned)?.

Returns:

  • (Boolean)

    The value was raised (or returned)?



33
34
35
# File 'lib/defi/value.rb', line 33

def raised?
  @raised
end

#to_hHash

Properties of the value.

Returns:

  • (Hash)

    The properties of the value.



40
41
42
43
44
45
# File 'lib/defi/value.rb', line 40

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

#to_sString

String of the value.

Returns:

  • (String)

    The string representation of the value.



50
51
52
53
# File 'lib/defi/value.rb', line 50

def to_s
  string = raised? ? 'raise' : 'return'
  "#{string} #{object}"
end