module Confix
This module makes it easy for any configuration object to define itself declaratively.
Usage¶ ↑
Include this module in a new object, like this:
class MyConfiguration include Confix setting :database_url config :external_api do setting :client_id setting :client_secret end end
Now, one can access these properties like this:
cfg = MyConfiguration.new cfg.database_url = 'http://www.database.com' cfg.external_api.client_id = 'MyApp'
or
cfg.external_api.update :client_id => 'MyApp', :client_secret => '1234567890'
One can access the settings using indexers or method calls, just like in OpenStruct. Also, symbols or strings can be used by the indexers, mimicking HashWithIndifferentAccess. One can even get or set a property of a child configuration easily:
raise 'invalid secret' if cfg['external_api.client_secret'] != secret
Notes¶ ↑
Internally, all settings are stored in the 'root' configuration object. Imagine you have the following configuration setup:
class Configuration include Confix config :child1 do setting :one end config :child2 do setting :two config :child2a do setting :three end end end
Configurations child1
and child2
will not store their own configuration values. Instead, all configuration options are stored in the root object, using the following keys:
-
child1.one
-
child2.two
-
+child2.child2a.three
This allows for rapid lookup and exporting. Any operation on a child configuration will forward their call to the root object.
Convenience accessors are created to retrieve values. Using the example above, one can access the same setting in the following ways:
config = Configuration.new config.child1.one = 'One' config.child2[:two] = 'Two' config['child2.child2a.three'] = 'Three' config[:child1].one # => 'One' config.child2[:one] # raises {UndefinedSetting} config.child2.child2a.three # => 'Three'
If you use an intermediate key, you will get a Config
object.
config.child2 # #<Confix::Config @parent=#<Configuration>> config.child2 = 'something' # (raises {CannotModifyConfiguration})
Assigns¶ ↑
You may add assignment variables to the {#assigns} hash. This hash is used to interpolate string settings.
Example¶ ↑
config.assigns[:some_path] = '/path/to/something' config.path_setting = '%{some_path}/file.yml' config.path_setting # => '/path/to/something/file.yml'
Constants
- VERSION
Public Class Methods
# File lib/confix.rb, line 91 def self.included(base) base.send :include, InstanceMethods base.send :extend, ClassMethods base.send :include, RootInstanceMethods base.send :extend, RootClassMethods end
# File lib/confix.rb, line 408 def self.valid_key?(key) key.to_s !~ /[^_a-zA_Z0-9]/ end