Class: Roast::ConfigManager
- Inherits:
-
Object
- Object
- Roast::ConfigManager
- Defined in:
- lib/roast/config_manager.rb
Defined Under Namespace
Classes: ConfigManagerAlreadyPreparedError, ConfigManagerError, ConfigManagerNotPreparedError, IllegalCogNameError
Instance Method Summary collapse
-
#config_for(cog_class, name = nil) ⇒ Object
: (singleton(Cog), ?Symbol?) -> Cog::Config.
-
#initialize(cog_registry, config_procs) ⇒ ConfigManager
constructor
: (Cog::Registry, Array[^() -> void]) -> void.
-
#prepare! ⇒ Object
: () -> void.
-
#prepared? ⇒ Boolean
: () -> bool.
-
#preparing? ⇒ Boolean
: () -> bool.
Constructor Details
#initialize(cog_registry, config_procs) ⇒ ConfigManager
: (Cog::Registry, Array[^() -> void]) -> void
12 13 14 15 16 17 18 19 20 |
# File 'lib/roast/config_manager.rb', line 12 def initialize(cog_registry, config_procs) @cog_registry = cog_registry @config_procs = config_procs @config_context = ConfigContext.new #: ConfigContext @global_config = Cog::Config.new #: Cog::Config @general_configs = {} #: Hash[singleton(Cog), Cog::Config] @regexp_scoped_configs = {} #: Hash[singleton(Cog), Hash[Regexp, Cog::Config]] @name_scoped_configs = {} #: Hash[singleton(Cog), Hash[Symbol, Cog::Config]] end |
Instance Method Details
#config_for(cog_class, name = nil) ⇒ Object
: (singleton(Cog), ?Symbol?) -> Cog::Config
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/roast/config_manager.rb', line 44 def config_for(cog_class, name = nil) raise ConfigManagerNotPreparedError unless prepared? # All cogs will always have a config; empty by default if the cog was never explicitly configured config = cog_class.config_class.new(@global_config.instance_variable_get(:@values).deep_dup) config = config.merge(fetch_general_config(cog_class)) @regexp_scoped_configs.fetch(cog_class, {}).select do |pattern, _| pattern.match?(name.to_s) unless name.nil? end.values.each { |cfg| config = config.merge(cfg) } unless name.nil? name_scoped_config = fetch_name_scoped_config(cog_class, name) config = config.merge(name_scoped_config) end config.validate! config end |
#prepare! ⇒ Object
: () -> void
23 24 25 26 27 28 29 30 31 |
# File 'lib/roast/config_manager.rb', line 23 def prepare! raise ConfigManagerAlreadyPreparedError if preparing? || prepared? @preparing = true bind_global bind_registered_cogs @config_procs.each { |cp| @config_context.instance_eval(&cp) } @prepared = true end |
#prepared? ⇒ Boolean
: () -> bool
39 40 41 |
# File 'lib/roast/config_manager.rb', line 39 def prepared? @prepared ||= false end |
#preparing? ⇒ Boolean
: () -> bool
34 35 36 |
# File 'lib/roast/config_manager.rb', line 34 def preparing? @preparing ||= false end |