Module: ConfigPlus::Base
- Included in:
- ConfigPlus
- Defined in:
- lib/config_plus/base.rb
Instance Attribute Summary collapse
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
-
#configure {|config| ... } ⇒ Object
Sets up configuration of
ConfigPlusand loads data. -
#generate(options = {}) ⇒ Object
Sets up configuration of
ConfigPlusand loads data. - #single_generate(source, options = {}) ⇒ Object
Instance Attribute Details
#root ⇒ Object (readonly)
Returns the value of attribute root.
3 4 5 |
# File 'lib/config_plus/base.rb', line 3 def root @root end |
Instance Method Details
#configure {|config| ... } ⇒ Object
Sets up configuration of ConfigPlus and loads data
Set a YAML file path to source property and you can access its data with ConfigPlus.root.
ConfigPlus.configure do |conf|
conf.source = '/path/to/yaml/file.yml'
end
When you set a directory path to source, you get configuration data that is merged every contents of YAML files under the directory you specify.
18 19 20 21 |
# File 'lib/config_plus/base.rb', line 18 def configure yield config if block_given? load end |
#generate(options = {}) ⇒ Object
Sets up configuration of ConfigPlus and loads data
You can describe the following code, when it needs only a single file for a resource of ConfigPlus.
ConfigPlus.generate(from: '/path/to/yaml/file.yml')
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/config_plus/base.rb', line 30 def generate(={}) config.source = .delete(:from) || .delete('from') .each do |k, v| if config.has_property?(k) config.property_set(k, v) else raise "Unknown configuration property `#{k}'" end end load end |
#single_generate(source, options = {}) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/config_plus/base.rb', line 42 def single_generate(source, ={}) meth = [:as, :config_method].lazy.map {|nm| .delete(nm) || .delete(nm.to_s) }.find {|v| v } || :config klass = .delete(:to) || .delete('to') raise unless klass conf = self::Config.new conf.source = source .each do |k, v| conf.has_property?(k) and conf.property_set(k, v) or raise "Unknown configuration property `#{k}'" end hsh = conf.loader.load conf.node_model.new(hsh).tap do |tree| [klass.singleton_class, klass].each do |obj| obj.instance_eval { define_method meth, lambda { tree } } end end end |