Class: Mixlib::Config::Configurable
- Inherits:
-
Object
- Object
- Mixlib::Config::Configurable
- Defined in:
- lib/mixlib/config/configurable.rb
Instance Attribute Summary collapse
-
#has_default ⇒ Object
readonly
Returns the value of attribute has_default.
Instance Method Summary collapse
- #default ⇒ Object
- #defaults_to(default_value = nil, &block) ⇒ Object
- #get(config) ⇒ Object
-
#initialize(symbol) ⇒ Configurable
constructor
A new instance of Configurable.
- #set(config, value) ⇒ Object
- #writes_value(&block) ⇒ Object
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_default ⇒ Object (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
#default ⇒ Object
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 |