Class: ConfigNewton::Configuration
- Inherits:
-
Object
- Object
- ConfigNewton::Configuration
- Defined in:
- lib/config_newton.rb
Instance Attribute Summary collapse
-
#properties ⇒ Object
readonly
Returns the value of attribute properties.
Instance Method Summary collapse
-
#[](property) ⇒ Object
Read an individual property on the configuration.
-
#[]=(property, value) ⇒ Object
Set an individual property on the configuration.
-
#add(name, options = {}) ⇒ Object
(also: #property)
Add a new property that can be set and read on this configuration.
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
-
#load(yaml_string, root_node = nil) ⇒ Object
Load configuration from a YAML string.
-
#load_from(file_or_path, root_node = 'configuration') ⇒ Object
Load configuration from a YAML file specified by the path given (or a file pointer).
-
#set(properties = {}, &block) ⇒ Object
Set the properties of the configuration either by providing a hash or by providing a block.
-
#to_hash ⇒ Object
Converts the configuration into a symbol-keyed hash.
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
54 55 56 57 58 |
# File 'lib/config_newton.rb', line 54 def initialize @hash = {} @properties = [] @defaults = {} end |
Instance Attribute Details
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
60 61 62 |
# File 'lib/config_newton.rb', line 60 def properties @properties end |
Instance Method Details
#[](property) ⇒ Object
Read an individual property on the configuration.
85 86 87 |
# File 'lib/config_newton.rb', line 85 def [](property) @hash[property.to_sym] || @defaults[property.to_sym] end |
#[]=(property, value) ⇒ Object
Set an individual property on the configuration.
90 91 92 |
# File 'lib/config_newton.rb', line 90 def []=(property, value) @hash[property.to_sym] = value end |
#add(name, options = {}) ⇒ Object Also known as: property
Add a new property that can be set and read on this configuration.
Options:
:default
-
Specify a default value.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/config_newton.rb', line 68 def add(name, = {}) raise ArgumentError, "Property name cannot be blank." unless name && name != "" @properties << name.to_sym @defaults[name.to_sym] = [:default] if [:default] self.class.class_eval <<-RUBY def #{name} self[:#{name}] end def #{name}=(value) self[:#{name}] = value end RUBY end |
#load(yaml_string, root_node = nil) ⇒ Object
Load configuration from a YAML string. Provide a root node if you want something other than the entire yaml document to be utilized. For example, in Rails, you might provide the Rails environment as a root node.
124 125 126 127 |
# File 'lib/config_newton.rb', line 124 def load(yaml_string, root_node = nil) hash = YAML::load(yaml_string) set(root_node ? hash[root_node] : hash) end |
#load_from(file_or_path, root_node = 'configuration') ⇒ Object
Load configuration from a YAML file specified by the path given (or a file pointer). Provide a root node if you want something other than the entire yaml document to be utilized. For example, in Rails, you might provide the Rails environment as a root node.
135 136 137 138 |
# File 'lib/config_newton.rb', line 135 def load_from(file_or_path, root_node = 'configuration') hash = YAML::load_file(file_or_path) set(root_node ? hash[root_node] : hash) end |
#set(properties = {}, &block) ⇒ Object
Set the properties of the configuration either by providing a hash or by providing a block. The block will yield a configuration object that has method setters and getters.
Example:
MyClass.config.set do |config|
config.property = 123
end
110 111 112 113 114 115 116 117 |
# File 'lib/config_newton.rb', line 110 def set(properties = {}, &block) if block_given? yield self else @hash.merge!(properties.inject({}){|h,(k,v)| h[k.to_sym] = v; h}) end self end |
#to_hash ⇒ Object
Converts the configuration into a symbol-keyed hash.
96 97 98 |
# File 'lib/config_newton.rb', line 96 def to_hash @defaults.merge(@hash) end |