Class: Mixlib::Config::Configurable
- Inherits:
-
Object
- Object
- Mixlib::Config::Configurable
- Defined in:
- lib/mixlib/config/configurable.rb
Instance Attribute Summary collapse
-
#default_block ⇒ Object
readonly
Returns the value of attribute default_block.
-
#default_value ⇒ Object
readonly
Returns the value of attribute default_value.
-
#symbol ⇒ Object
readonly
Returns the value of attribute symbol.
Instance Method Summary collapse
- #default ⇒ Object
- #default_block? ⇒ Boolean
- #defaults_to(default_value = nil, &block) ⇒ Object
- #get(config) ⇒ Object
- #has_default? ⇒ Boolean (also: #has_default)
-
#initialize(symbol) ⇒ Configurable
constructor
A new instance of Configurable.
- #is_default?(config) ⇒ Boolean
- #set(config, value) ⇒ Object
- #writes_value(&block) ⇒ Object
- #writes_value? ⇒ Boolean
Constructor Details
#initialize(symbol) ⇒ Configurable
Returns a new instance of Configurable.
26 27 28 |
# File 'lib/mixlib/config/configurable.rb', line 26 def initialize(symbol) @symbol = symbol end |
Instance Attribute Details
#default_block ⇒ Object (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_value ⇒ Object (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 |
#symbol ⇒ Object (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
#default ⇒ Object
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 |