Class: RFacter::Util::NonNullable Private

Inherits:
Concurrent::ThreadLocalVar
  • Object
show all
Defined in:
lib/rfacter/util/non_nullable.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.

Non-nullable thread local variable

A Subclass of Concurrent::ThreadLocalVar that raises a NameError if de-referenced to ‘nil`. This allows the creation of variables that must always be bound to a specific value before use.

Since:

  • 0.1.0

Instance Method Summary collapse

Constructor Details

#initialize(default = nil, err_message:, &default_block) ⇒ NonNullable

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 a new instance of NonNullable.

Parameters:

  • err_message (String)

    The error message to raise if the instance is de-referenced to a ‘nil` value.

Since:

  • 0.1.0



16
17
18
19
# File 'lib/rfacter/util/non_nullable.rb', line 16

def initialize(default = nil, err_message:, &default_block)
  @err_message = err_message
  super(default, &default_block)
end

Instance Method Details

#bind(value, &block) ⇒ Object

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.

Since:

  • 0.1.0



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rfacter/util/non_nullable.rb', line 27

def bind(value, &block)
  if block_given?
    old_value = nillable_value
    begin
      self.value = value
      yield
    ensure
      self.value = old_value
    end
  end
end

#valueObject

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.

Raises:

  • (NameError)

    when de-referenced to ‘nil`.

Since:

  • 0.1.0



40
41
42
43
44
45
46
# File 'lib/rfacter/util/non_nullable.rb', line 40

def value
  result = super

  raise(NameError, @err_message) if result.nil?

  result
end