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


26
27
28
# File 'lib/mixlib/config/configurable.rb', line 26

def initialize(symbol)
  @symbol = symbol
end

Instance Attribute Details

#default_blockObject (readonly)

Returns the value of attribute default_block


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

def default_block
  @default_block
end

#default_valueObject (readonly)

Returns the value of attribute default_value


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

def default_value
  @default_value
end

#symbolObject (readonly)

Returns the value of attribute symbol


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

def symbol
  @symbol
end

Instance Method Details

#defaultObject


69
70
71
72
73
74
75
# File 'lib/mixlib/config/configurable.rb', line 69

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

#default_block?Boolean


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

def default_block?
  instance_variable_defined?(:@default_block)
end

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


44
45
46
47
48
# File 'lib/mixlib/config/configurable.rb', line 44

def defaults_to(default_value = nil, &block)
  @default_value = default_value
  @default_block = block if block_given?
  self
end

#get(config) ⇒ Object


55
56
57
58
59
60
61
62
63
# File 'lib/mixlib/config/configurable.rb', line 55

def get(config)
  if config.key?(symbol)
    config[symbol]
  elsif default_block?
    default_block.call
  else
    config[symbol] = safe_dup(default_value)
  end
end

#has_default?Boolean Also known as: has_default


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

def has_default?
  instance_variable_defined?(:@default_value)
end

#is_default?(config) ⇒ Boolean


77
78
79
# File 'lib/mixlib/config/configurable.rb', line 77

def is_default?(config)
  !config.key?(symbol) || config[symbol] == default_value
end

#set(config, value) ⇒ Object


65
66
67
# File 'lib/mixlib/config/configurable.rb', line 65

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

#writes_value(&block) ⇒ Object


50
51
52
53
# File 'lib/mixlib/config/configurable.rb', line 50

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

#writes_value?Boolean


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

def writes_value?
  instance_variable_defined?(:@writes_value)
end