Class: Defi::Value Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Value

Initialize the value class.

rubocop:disable Lint/RescueException

Parameters:

  • block (Proc)

    The block of code to execute.



18
19
20
21
22
23
24
# File 'lib/defi/value.rb', line 18

def initialize(&block)
  @object = block.call
  @raised = false
rescue ::Exception => e
  @object = e
  @raised = true
end

Instance Attribute Details

#object#object_id (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The returned or the raised object.

Returns:

  • (#object_id)

    The returned or the raised object.



10
11
12
# File 'lib/defi/value.rb', line 10

def object
  @object
end

Instance Method Details

#call#object_id

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raise or return the value.

Returns:

  • (#object_id)

    Raised exception or returned object.



30
31
32
33
34
# File 'lib/defi/value.rb', line 30

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.



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

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)?



39
40
41
# File 'lib/defi/value.rb', line 39

def raised?
  @raised
end

#to_hHash

Properties of the value.

Returns:

  • (Hash)

    The properties of the value.



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

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

#to_sString

String of the value.

Returns:

  • (String)

    The string representation of the value.



60
61
62
63
64
65
66
67
68
# File 'lib/defi/value.rb', line 60

def to_s
  string = if raised?
             'raise'
           else
             'return'
           end

  "#{string} #{object}"
end