module Confix::ClassMethods
Attributes
key_from_root[R]
Public Instance Methods
config(key, template = nil, &block)
click to toggle source
Defines a child configuration for this configuration.
# File lib/confix.rb, line 295 def config(key, template = nil, &block) key = key.to_s raise ArgumentError, "invalid key: #{key}" unless Confix.valid_key?(key) config = Class.new(Config) config.instance_variable_set '@parent', self config.instance_variable_set '@key_from_root', expand_key(key) # If no template or block are specified, infer a default template from the name. if !template && !block template = key raise ArgumentError, "no template or block specified, and no template :#{key} found" unless config_root.templates[template] elsif template template = template.to_s raise ArgumentError, "template :#{key} not found" unless config_root.templates[template] end # If a template is specified, first apply its block. config.class_eval &config_root.templates[template] if template # Apply the block. config.class_eval &block if block # Wrap up. define_accessor_methods key configs[key] = config config end
config_root()
click to toggle source
# File lib/confix.rb, line 337 def config_root @parent ? @parent.config_root : self end
configs()
click to toggle source
# File lib/confix.rb, line 333 def configs @configs ||= {} end
defaults()
click to toggle source
# File lib/confix.rb, line 330 def defaults @defaults ||= {} end
expand_key(key)
click to toggle source
Support
# File lib/confix.rb, line 346 def expand_key(key) [ @key_from_root, key ].compact.join('.') end
key_defined?(key)
click to toggle source
# File lib/confix.rb, line 350 def key_defined?(key) if key.is_a?(Array) tail = key else tail = key.to_s.split('.') end head = tail.shift if configs.keys.include?(head) configs[head].key_defined?(tail) elsif settings.include?(head) && tail.empty? true else false end end
setting(key, default = nil)
click to toggle source
Defines a setting for this configuration. If this configuration was defined as a child of some parent configuration, this parent configuration will also create a definition for this setting, but no accessor methods.
@param [Object] default Specify a default value for the setting.
# File lib/confix.rb, line 285 def setting(key, default = nil) key = key.to_s raise ArgumentError, "invalid key: #{key}" unless Confix.valid_key?(key) settings << key defaults[key] = default if default define_accessor_methods key end
settings()
click to toggle source
Attributes
# File lib/confix.rb, line 327 def settings @settings ||= [] end
Private Instance Methods
define_accessor_methods(key)
click to toggle source
# File lib/confix.rb, line 369 def define_accessor_methods(key) class_eval <<-RUBY, __FILE__, __LINE__+1 def #{key}; get(:#{key}) end def #{key}=(value) set(:#{key}, value) end RUBY end