Class: Mixlib::Config::Configurable

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol) ⇒ Configurable

Returns a new instance of Configurable.



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

def initialize(symbol)
  @symbol = symbol
  @default_block = nil
  @has_default = false
  @default_value = nil
  @writes_value = nil
end

Instance Attribute Details

#has_defaultObject (readonly)

Returns the value of attribute has_default.



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

def has_default
  @has_default
end

Instance Method Details

#defaultObject



64
65
66
67
68
69
70
# File 'lib/mixlib/config/configurable.rb', line 64

def default
  if @default_block
    @default_block.call
  else
    @default_value
  end
end

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



32
33
34
35
36
37
# File 'lib/mixlib/config/configurable.rb', line 32

def defaults_to(default_value = nil, &block)
  @has_default = true
  @default_block = block
  @default_value = default_value
  self
end

#get(config) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mixlib/config/configurable.rb', line 44

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

#set(config, value) ⇒ Object



60
61
62
# File 'lib/mixlib/config/configurable.rb', line 60

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

#writes_value(&block) ⇒ Object



39
40
41
42
# File 'lib/mixlib/config/configurable.rb', line 39

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