Module: Configurable::ClassMethods
- Defined in:
- lib/configurable/class_methods.rb,
lib/configurable/ordered_hash_patch.rb
Overview
ClassMethods extends classes that include Configurable and provides methods for declaring configurations.
Constant Summary collapse
- CONFIGURATIONS_CLASS =
OrderedHashPatch
Instance Attribute Summary collapse
-
#config_registry ⇒ Object
readonly
A hash of (key, Config) pairs tracking configs defined on self.
Class Method Summary collapse
-
.initialize(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#cache_configurations(on = true) ⇒ Object
Caches the configurations hash so as to improve peformance.
-
#configurations ⇒ Object
A hash of (key, Config) pairs representing all configurations defined on this class or inherited from ancestors.
-
#parse(argv = ARGV, options = {}) ⇒ Object
Parses configurations from argv in a non-destructive manner by generating a ConfigParser using the configurations for self.
-
#parse!(argv = ARGV, options = {}) ⇒ Object
Same as parse, but removes parsed args from argv.
Instance Attribute Details
#config_registry ⇒ Object (readonly)
A hash of (key, Config) pairs tracking configs defined on self. See configurations for all configs declared across all ancestors.
16 17 18 |
# File 'lib/configurable/class_methods.rb', line 16 def config_registry @config_registry end |
Class Method Details
.initialize(base) ⇒ Object
:nodoc:
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/configurable/class_methods.rb', line 18 def self.initialize(base) # :nodoc: unless base.instance_variable_defined?(:@config_registry) base.instance_variable_set(:@config_registry, CONFIGURATIONS_CLASS.new) end unless base.instance_variable_defined?(:@use_indifferent_access) base.instance_variable_set(:@use_indifferent_access, true) end unless base.instance_variable_defined?(:@configurations) base.instance_variable_set(:@configurations, nil) end end |
Instance Method Details
#cache_configurations(on = true) ⇒ Object
Caches the configurations hash so as to improve peformance. Call with on set to false to turn off caching.
83 84 85 86 |
# File 'lib/configurable/class_methods.rb', line 83 def cache_configurations(on=true) @configurations = nil @configurations = self.configurations if on end |
#configurations ⇒ Object
A hash of (key, Config) pairs representing all configurations defined on this class or inherited from ancestors. The configurations hash is generated on each call to ensure it accurately reflects any configs added on ancestors. This slows down initialization and config access through instance.config.
Call cache_configurations after all configs have been declared in order to prevent regeneration of configurations and to significantly improve performance.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/configurable/class_methods.rb', line 61 def configurations return @configurations if @configurations configurations = CONFIGURATIONS_CLASS.new configurations.extend(IndifferentAccess) if @use_indifferent_access ancestors.reverse.each do |ancestor| next unless ancestor.kind_of?(ClassMethods) ancestor.config_registry.each_pair do |key, value| if value.nil? configurations.delete(key) else configurations[key] = value end end end configurations end |
#parse(argv = ARGV, options = {}) ⇒ Object
Parses configurations from argv in a non-destructive manner by generating a ConfigParser using the configurations for self. Returns an array like
- args, config
-
where the args are the arguments that remain after parsing,
and config is a hash of the parsed configs. The parser is yielded to the block, if given, to register additonal options.
See ConfigParser#parse for more information.
39 40 41 |
# File 'lib/configurable/class_methods.rb', line 39 def parse(argv=ARGV, ={}) # :yields: parser parse!(argv.dup, ) end |
#parse!(argv = ARGV, options = {}) ⇒ Object
Same as parse, but removes parsed args from argv.
44 45 46 47 48 49 50 |
# File 'lib/configurable/class_methods.rb', line 44 def parse!(argv=ARGV, ={}) parser = ConfigParser.new parser.add(configurations) args = parser.parse!(argv, ) [args, parser.config] end |