Class: Mixlib::Config::Configurable

Inherits:
Object
  • Object
show all
Defined in:
lib/mixlib/config/configurable.rb

Instance Method Summary collapse

Constructor Details

#initialize(symbol) ⇒ Configurable

Returns a new instance of Configurable.



22
23
24
25
26
27
# File 'lib/mixlib/config/configurable.rb', line 22

def initialize(symbol)
  @symbol = symbol
  @default = nil
  @default_value = nil
  @writes_value = nil
end

Instance Method Details

#defaults_to(default_value = nil, &block) ⇒ Object



29
30
31
32
33
# File 'lib/mixlib/config/configurable.rb', line 29

def defaults_to(default_value = nil, &block)
  @default = block
  @default_value = default_value
  self
end

#get(config) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mixlib/config/configurable.rb', line 40

def get(config)
  if config.has_key?(@symbol)
    config[@symbol]
  elsif @default
    @default.call
  else
    begin
      # Some things cannot be dup'd, and you won't know this till after the fact
      config[@symbol] = @default_value.dup
    rescue TypeError
      @default_value
    end
  end
end

#set(config, value) ⇒ Object



55
56
57
# File 'lib/mixlib/config/configurable.rb', line 55

def set(config, value)
  config[@symbol] = @writes_value ? @writes_value.call(value) : value
end

#writes_value(&block) ⇒ Object



35
36
37
38
# File 'lib/mixlib/config/configurable.rb', line 35

def writes_value(&block)
  @writes_value = block
  self
end