Class: Plumbum::OneProvider

Inherits:
Object
  • Object
show all
Includes:
Providers::Singular
Defined in:
lib/plumbum/one_provider.rb

Overview

Provider that provides a single value for a specified key.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Provider

#get, #has?, #options, #read_only?, #set, #write_once?

Constructor Details

#initialize(key, value: Plumbum::UNDEFINED, **options) ⇒ OneProvider

Returns a new instance of OneProvider.

Parameters:

  • key (String, Symbol)

    the key used to identify the provided value.

  • value (Object) (defaults to: Plumbum::UNDEFINED)

    the provided value, if any.

  • options (Hash)

    additional options for the provider.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/plumbum/one_provider.rb', line 14

def initialize(key, value: Plumbum::UNDEFINED, **options)
  super()

  tools
    .assertions
    .validate_name(key, as: :key)

  @key     = key.to_s
  @value   = value
  @options = validate_options(options)
end

Instance Attribute Details

#keyString, Symbol (readonly)

Returns the key used to identify the provided value.

Returns:

  • (String, Symbol)

    the key used to identify the provided value.



27
28
29
# File 'lib/plumbum/one_provider.rb', line 27

def key
  @key
end

Instance Method Details

#valueObject?

Returns the provided value, or nil if the value is not defined.

Returns:

  • (Object, nil)

    the provided value, or nil if the value is not defined.



31
32
33
# File 'lib/plumbum/one_provider.rb', line 31

def value
  @value == Plumbum::UNDEFINED ? nil : @value
end

#value=(value) ⇒ Object

Parameters:

  • value (Object)

    the changed value.



36
37
38
39
40
# File 'lib/plumbum/one_provider.rb', line 36

def value=(value)
  require_mutable(key)

  set_value(key, value)
end