Class: D13n::Configuration::Manager
- Inherits:
-
Object
- Object
- D13n::Configuration::Manager
- Defined in:
- lib/d13n/configuration/manager.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #alias_for(key) ⇒ Object
- #alias_key_for(value) ⇒ Object
- #app_name ⇒ Object
- #apply_mask(hash) ⇒ Object
- #apply_transformations(key, value) ⇒ Object
- #evaluate_procs(value) ⇒ Object
- #fetch(key) ⇒ Object
- #flattened ⇒ Object
- #has_key?(key) ⇒ Boolean
-
#initialize ⇒ Manager
constructor
A new instance of Manager.
- #invoke_callbacks(direction, source) ⇒ Object
- #key(value) ⇒ Object
- #keys ⇒ Object
- #log_config(direction, source) ⇒ Object
- #register_callback(key, &proc) ⇒ Object
- #remove_config(source) ⇒ Object
- #remove_config_type(sym) ⇒ Object
- #replace_or_add_config(source) ⇒ Object
- #reset_alias ⇒ Object
- #reset_cache ⇒ Object
-
#reset_to_defaults ⇒ Object
Generally only useful during initial construction and tests.
- #source(key) ⇒ Object
- #to_collector_hash ⇒ Object
- #to_hash ⇒ Object
- #transform_from_default(key) ⇒ Object
Constructor Details
#initialize ⇒ Manager
Returns a new instance of Manager.
28 29 30 31 |
# File 'lib/d13n/configuration/manager.rb', line 28 def initialize reset_to_defaults @callbacks = Hash.new { |hash, key| hash[key] = [] } end |
Instance Method Details
#[](key) ⇒ Object
4 5 6 |
# File 'lib/d13n/configuration/manager.rb', line 4 def [](key) @cache[key] end |
#alias_for(key) ⇒ Object
24 25 26 |
# File 'lib/d13n/configuration/manager.rb', line 24 def alias_for(key) @alias_cache[key] end |
#alias_key_for(value) ⇒ Object
20 21 22 |
# File 'lib/d13n/configuration/manager.rb', line 20 def alias_key_for(value) alias_for(key(value)) end |
#app_name ⇒ Object
213 214 215 |
# File 'lib/d13n/configuration/manager.rb', line 213 def app_name D13n.config[:app_name] end |
#apply_mask(hash) ⇒ Object
191 192 193 194 195 196 |
# File 'lib/d13n/configuration/manager.rb', line 191 def apply_mask(hash) MASK_DEFAULTS. \ select {|_, proc| proc.call}. \ each {|key, _| hash.delete(key) } hash end |
#apply_transformations(key, value) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/d13n/configuration/manager.rb', line 104 def apply_transformations(key, value) if transform = transform_from_default(key) begin transform.call(value) rescue => e D13n.logger.error("Error applying transformation for #{key}, pre-transform value was: #{value}.", e) raise e end else value end end |
#evaluate_procs(value) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/d13n/configuration/manager.rb', line 121 def evaluate_procs(value) if value.respond_to?(:call) instance_eval(&value) else value end end |
#fetch(key) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/d13n/configuration/manager.rb', line 87 def fetch(key) config_stack.each do |config| next unless config accessor = key.to_sym if config.has_key?(accessor) evaluated = evaluate_procs(config[accessor]) begin return apply_transformations(accessor, evaluated) rescue next end end end nil end |
#flattened ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/d13n/configuration/manager.rb', line 175 def flattened config_stack.reverse.inject({}) do |flat,layer| thawed_layer = layer.to_hash.dup thawed_layer.each do |k,v| begin thawed_layer[k] = instance_eval(&v) if v.respond_to?(:call) rescue => e D13n.logger.debug("#{e.class.name} : #{e.message} - when accessing config key #{k}") thawed_layer[k] = nil end thawed_layer.delete(:config) end flat.merge(thawed_layer.to_hash) end end |
#has_key?(key) ⇒ Boolean
8 9 10 |
# File 'lib/d13n/configuration/manager.rb', line 8 def has_key?(key) @cache.has_key?(key) end |
#invoke_callbacks(direction, source) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/d13n/configuration/manager.rb', line 159 def invoke_callbacks(direction, source) return unless source source.keys.each do |key| if @cache[key] != source[key] @callbacks[key].each do |proc| if direction == :add proc.call(source[key]) else proc.call(@cache[key]) end end end end end |
#key(value) ⇒ Object
16 17 18 |
# File 'lib/d13n/configuration/manager.rb', line 16 def key(value) @cache.key(value) end |
#keys ⇒ Object
12 13 14 |
# File 'lib/d13n/configuration/manager.rb', line 12 def keys @cache.keys end |
#log_config(direction, source) ⇒ Object
148 149 150 151 152 |
# File 'lib/d13n/configuration/manager.rb', line 148 def log_config(direction, source) D13n.logger.debug do "Updating config (#{direction}) from #{source.class}. Results: #{flattened.inspect}" end end |
#register_callback(key, &proc) ⇒ Object
154 155 156 157 |
# File 'lib/d13n/configuration/manager.rb', line 154 def register_callback(key, &proc) @callbacks[key] << proc proc.call(@cache[key]) end |
#remove_config(source) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/d13n/configuration/manager.rb', line 45 def remove_config(source) case source when EnvironmentSource then @environment_source = nil when ServerSource then @server_source = nil when ManualSource then @manual_source = nil when YamlSource then @yaml_source = nil when DefaultSource then @default_source = nil end reset_cache reset_alias invoke_callbacks(:remove, source) log_config(:remove, source) end |
#remove_config_type(sym) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/d13n/configuration/manager.rb', line 33 def remove_config_type(sym) source = case sym when :environment then @environment_source when :server then @server_source when :manual then @manual_source when :yaml then @yaml_source when :default then @default_source end remove_config(source) end |
#replace_or_add_config(source) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/d13n/configuration/manager.rb', line 60 def replace_or_add_config(source) source.freeze invoke_callbacks(:add, source) case source when EnvironmentSource then @environment_source = source when ServerSource then @server_source = source when ManualSource then @manual_source = source when YamlSource then @yaml_source = source when DefaultSource then @default_source = source else D13n.logger.warn("Invalid config format; config will be ignored: #{source}") end reset_cache reset_alias log_config(:add, source) end |
#reset_alias ⇒ Object
144 145 146 |
# File 'lib/d13n/configuration/manager.rb', line 144 def reset_alias @alias_cache = @default_source.default_alias end |
#reset_cache ⇒ Object
140 141 142 |
# File 'lib/d13n/configuration/manager.rb', line 140 def reset_cache @cache = Hash.new {|hash,key| hash[key] = self.fetch(key) } end |
#reset_to_defaults ⇒ Object
Generally only useful during initial construction and tests
130 131 132 133 134 135 136 137 138 |
# File 'lib/d13n/configuration/manager.rb', line 130 def reset_to_defaults @environment_source = EnvironmentSource.new @server_source = nil @manual_source = nil @yaml_source = nil @default_source = DefaultSource.new reset_cache reset_alias end |
#source(key) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/d13n/configuration/manager.rb', line 79 def source(key) config_stack.each do |config| if config.respond_to?(key.to_sym) || config.has_key?(key.to_sym) return config end end end |
#to_collector_hash ⇒ Object
202 203 204 205 206 207 208 209 210 211 |
# File 'lib/d13n/configuration/manager.rb', line 202 def to_collector_hash DottedHash.new(apply_mask(flattened)).to_hash.delete_if do |k, v| default = DEFAULTS[k] if default default[:exclude_from_reported_settings] else false end end end |
#to_hash ⇒ Object
198 199 200 |
# File 'lib/d13n/configuration/manager.rb', line 198 def to_hash DottedHash.new(apply_mask(flattened)).to_hash end |
#transform_from_default(key) ⇒ Object
117 118 119 |
# File 'lib/d13n/configuration/manager.rb', line 117 def transform_from_default(key) D13n::Configuration::DefaultSource.transform_for(key) end |