Class: R10K::Settings::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/r10k/settings/container.rb

Overview

Defines a collection for application settings

This implements a hierarchical interface to application settings. Containers can define an optional parent container that will be used for default options if those options aren’t set on the given container.

Defined Under Namespace

Classes: InvalidKey

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ Container

Returns a new instance of Container.

Parameters:



13
14
15
16
17
18
# File 'lib/r10k/settings/container.rb', line 13

def initialize(parent = nil)
  @parent = parent

  @valid_keys = Set.new
  @settings = {}
end

Instance Attribute Details

#valid_keysSet<Symbol>

Returns All valid keys defined on the container or parent container.

Returns:

  • (Set<Symbol>)

    All valid keys defined on the container or parent container.



10
11
12
# File 'lib/r10k/settings/container.rb', line 10

def valid_keys
  @valid_keys
end

Instance Method Details

#[](key) ⇒ Object?

Look up a value in the container. The lookup checks the current container, and then falls back to the parent container if it’s given.

Parameters:

  • key (Symbol)

    The lookup key

Returns:

  • (Object, nil)

    The retrieved value if present.

Raises:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/r10k/settings/container.rb', line 29

def [](key)
  validate_key! key

  if @settings[key]
    @settings[key]
  elsif @parent && (pkey = @parent[key])
    begin
      @settings[key] = pkey.dup
    rescue TypeError
      @settings[key] = pkey
    end
    @settings[key]
  end
end

#[]=(key, value) ⇒ Object

Set a value on the container

Parameters:

  • key (Symbol)

    The lookup key

  • value (Object)

    The value to store in the container

Raises:



51
52
53
54
55
# File 'lib/r10k/settings/container.rb', line 51

def []=(key, value)
  validate_key! key

  @settings[key] = value
end

#add_valid_key(key) ⇒ void

Note:

This should only be used by R10K::Settings::Container#R10K#R10K::Settings#R10K::Settings::ClassSettings

This method returns an undefined value.

Define a valid container key

Parameters:

  • key (Symbol)


63
64
65
# File 'lib/r10k/settings/container.rb', line 63

def add_valid_key(key)
  @valid_keys.add(key)
end

#reset!void

This method returns an undefined value.

Clear all existing settings in this container. Valid settings are left alone.



83
84
85
# File 'lib/r10k/settings/container.rb', line 83

def reset!
  @settings = {}
end

#valid_key?(key) ⇒ true, false

Determine if a key is a valid setting.

Parameters:

  • key (Symbol)

Returns:

  • (true, false)


72
73
74
75
76
77
78
79
# File 'lib/r10k/settings/container.rb', line 72

def valid_key?(key)
  if @valid_keys.include?(key)
    true
  elsif @parent and @parent.valid_key?(key)
    @valid_keys.add(key)
    true
  end
end