Class: ConfigManager::Configuration
- Inherits:
-
Object
- Object
- ConfigManager::Configuration
- Defined in:
- lib/configmanager/configuration.rb
Overview
Represents a collection of properties stored as a hash.
Direct Known Subclasses
Instance Method Summary collapse
-
#add_properties(values, at = "", options = {}) ⇒ NilClass
Adds a hash of properties to this configuration.
-
#add_property(key, value, options = {}) ⇒ NilClass
(also: #[]=, #set_property)
Adds a property to this configuration.
-
#get_property(key, options = {}) ⇒ Object
(also: #[])
Get the value of the specified property.
-
#has_property?(key) ⇒ TrueClass, FalseClass
Check to see if the specified property is defined for this configuration.
-
#initialize(options = {}) ⇒ Configuration
constructor
Creates a new empty configuration.
Constructor Details
#initialize(options = {}) ⇒ Configuration
Creates a new empty configuration.
19 20 21 22 23 24 |
# File 'lib/configmanager/configuration.rb', line 19 def initialize( = {}) @root = {} @use_interpolator = get_option(, :use_interpolator, false, true) @interpolator = get_option(, :interpolator, false, ConfigManager::DefaultInterpolator) @tolerate_missing_references = get_option(, :tolerate_missing_references, false, false) end |
Instance Method Details
#add_properties(values, at = "", options = {}) ⇒ NilClass
Adds a hash of properties to this configuration.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/configmanager/configuration.rb', line 34 def add_properties(values, at = "", = {}) # Add each item in the hash - Nested hashes call this method recursively. at_array = at.split(".") values.each_pair do |key, value| full_key = (at_array + [key]).join(".") value.kind_of?(Hash) ? add_properties(value, full_key, ) : add_property(full_key, value, ) end nil end |
#add_property(key, value, options = {}) ⇒ NilClass Also known as: []=, set_property
Adds a property to this configuration. The key is a ‘.’ separated string that fully qualifies this property. So the property a.b identifies the property ‘b’ inside the group ‘a’. Similarly a.b.c identifies the property ‘c’ inside the group ‘b’ which is inside the group ‘a’.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/configmanager/configuration.rb', line 56 def add_property(key, value, = {}) # Default options. at = get_option(, :at, false, "") overwrite = get_option(, :overwrite, false, false) # Create and add the property. key_array = key.split(".") at_array = at.split(".") leaf = key_array[-1] at_array = at_array + key_array[0..-2] at_node = get_node(at_array, create = true) # Got the node, now add the property, unless it already exists. raise ConfigManager::KeyError.new("Cannot add - '#{at_array.join(".")}.#{leaf}' already exists!") if at_node.has_key?(leaf) and not overwrite at_node[leaf] = value nil end |
#get_property(key, options = {}) ⇒ Object Also known as: []
Get the value of the specified property. The key is a ‘.’ separated string that fully qualifies this property. So the property a.b identifies the property ‘b’ inside the group ‘a’. Similarly a.b.c identifies the property ‘c’ inside the group ‘b’ which is inside the group ‘a’.
If enabled, the property’s value is interpolated. Finally the list of post-processors are invoked and the value is returned.
Note : Interpolator implementations may support additional options too.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/configmanager/configuration.rb', line 91 def get_property(key, = {}) use_interpolator = get_option(, :use_interpolator, false, @use_interpolator) interpolator = get_option(, :interpolator, false, @interpolator) tolerate_missing_references = set_option(, :tolerate_missing_references, @tolerate_missing_references, false) post_processors = pop_option(, :post_processors, false, []) # Get the required property's raw value. key_array = key.split(".") create = false raw_value = get_node(key_array, create) # Interpolate. raw_value = interpolator.interpolate(key, raw_value, self, ) if use_interpolator # Post-process. final_value = raw_value post_processors.each { |processor| final_value = processor.call(final_value) } # All done. final_value end |
#has_property?(key) ⇒ TrueClass, FalseClass
Check to see if the specified property is defined for this configuration. This method will throw an exception if the key is invalid.
115 116 117 118 119 120 121 122 |
# File 'lib/configmanager/configuration.rb', line 115 def has_property?(key) key_array = key.split(".") create = false get_node(key_array, create) return true rescue ConfigManager::PropertyNotFoundError return false end |