module ConfigPlus::Base
Attributes
root[R]
Public Instance Methods
configure() { |config| ... }
click to toggle source
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.
# File lib/config_plus/base.rb, line 18 def configure yield config if block_given? load end
generate(options={})
click to toggle source
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')
# File lib/config_plus/base.rb, line 30 def generate(options={}) config.source = options.delete(:from) || options.delete('from') options.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={})
click to toggle source
# File lib/config_plus/base.rb, line 42 def single_generate(source, options={}) meth = [:as, :config_method].lazy.map {|nm| options.delete(nm) || options.delete(nm.to_s) }.find {|v| v } || :config klass = options.delete(:to) || options.delete('to') raise unless klass conf = self::Config.new conf.source = source options.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
Protected Instance Methods
config()
click to toggle source
# File lib/config_plus/base.rb, line 66 def config @config ||= ::ConfigPlus::Config.new end
Private Instance Methods
included(base)
click to toggle source
# File lib/config_plus/base.rb, line 94 def included(base) method_name = self.config.config_method return unless method_name own = ::ConfigPlus::Helper.config_for(base, self.root) inheritance = inherited_config_of(base) node_model = config.node_model [base, base.singleton_class].each do |obj| obj.instance_eval do config = inheritance.empty? ? own : ::ConfigPlus::Merger.merge(inheritance, own || {}) config = node_model.new(config) define_method method_name, lambda { config } end end end
inherited_config_of(klass)
click to toggle source
# File lib/config_plus/base.rb, line 81 def inherited_config_of(klass) klass.ancestors.select {|clazz| clazz != klass and clazz != self and clazz.ancestors.include?(self) }.reverse.each.inject({}) {|hsh, clazz| h = clazz.public_send(self.config.config_method) h = ::ConfigPlus::Helper.config_for(clazz, self.root) unless h or h.is_a?(Hash) ::ConfigPlus::Merger.merge(hsh, h) } end
load()
click to toggle source
Loads a configuration data as a hash object from files specified with source
or root_dir
settings.
# File lib/config_plus/base.rb, line 76 def load hash = config.loader.load @root = config.node_model.new(hash) end