Class: Halcyon::Config
- Inherits:
-
Object
- Object
- Halcyon::Config
- Defined in:
- lib/halcyon/config.rb,
lib/halcyon/config/file.rb,
lib/halcyon/config/paths.rb,
lib/halcyon/config/helpers.rb
Overview
Application configuration map.
Defined Under Namespace
Modules: Helpers Classes: File, Paths
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
Class Method Summary collapse
-
.defaults(env = nil) ⇒ Object
Default configuration values.
-
.load_from(path) ⇒ Object
Loads the contents of a configuration file found at
path
.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Alias for the
get
method. -
#[]=(key, value) ⇒ Object
Alias for the
put
method. -
#configure(config = {}) ⇒ Object
Sets the configuration up with the values given.
-
#defaults(env = nil) ⇒ Object
Shortcut for Halcyon::Config.defaults.
-
#delete(key) ⇒ Object
Removes the configuration value from the hash.
-
#get(key) ⇒ Object
Get the configuration value associated with the key.
-
#initialize(config = {}, &block) ⇒ Config
constructor
Creates an empty configuration hash (Mash) and sets up the configuration to whatever the settings are provided, merging over the defaults.
- #inspect ⇒ Object
-
#load_from(path) ⇒ Object
Loads the contents of a configuration file found at
path
and merges it with the current configuration. -
#method_missing(method, *args) ⇒ Object
Allows retrieval of single key config values and setting single config values.
-
#put(key_or_config_hash, value = nil) ⇒ Object
Put the configuration value associated with the key or setup with a hash.
-
#setup(config = {}) ⇒ Object
Sets up the configuration by storing the settings provided (via param or via block).
-
#to_hash ⇒ Object
Returns the configuration as a hash.
-
#to_yaml ⇒ Object
Returns the configuration rendered as YAML.
-
#use ⇒ Object
Yields and returns the configuration.
Constructor Details
#initialize(config = {}, &block) ⇒ Config
Creates an empty configuration hash (Mash) and sets up the configuration to whatever the settings are provided, merging over the defaults.
Examples:
Halcyon::Config.new(:environment => :development)
OR
Halcyon::Config.new(:allow_from => :all)
OR
Halcyon::Config.new
OR
Halcyon::Config.new do |c|
c[:foo] = true
end
34 35 36 37 38 39 |
# File 'lib/halcyon/config.rb', line 34 def initialize(config={}, &block) env = config.delete(:environment) self.config = Mash.new self.setup(self.defaults(env).merge(config)) self.use(&block) if block_given? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/halcyon/config.rb', line 93 def method_missing(method, *args) if method.to_s[-1,1] == '=' self.put(method.to_s.tr('=',''), *args) else self.get(method) end end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
7 8 9 |
# File 'lib/halcyon/config.rb', line 7 def config @config end |
Class Method Details
.defaults(env = nil) ⇒ Object
Default configuration values.
Defaults to the configuration for :development
.
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/halcyon/config.rb', line 198 def defaults(env = nil) base = { :app => nil, :root => Dir.pwd, :environment => :development, :allow_from => 'all', :logging => { :type => 'Logger', :level => 'debug' }, :paths => Paths.new, :hooks => {:startup => [], :shutdown => []} } case (env || :development) when :development base.merge({ :environment => :development }) when :test base.merge({ :app => 'Specs', :environment => :test, :logging => { :type => 'Logger', :level => 'warn', :file => 'log/test.log' } }) when :console base.merge({ :environment => :console }) when :production base.merge({ :environment => :production, :logging => { :type => 'Logger', :level => 'warn', :file => 'log/production.log' } }) end end |
Instance Method Details
#[](key) ⇒ Object
145 146 147 |
# File 'lib/halcyon/config.rb', line 145 def [](key) self.get(key) end |
#[]=(key, value) ⇒ Object
155 156 157 |
# File 'lib/halcyon/config.rb', line 155 def []=(key, value) self.put(key, value) end |
#configure(config = {}) ⇒ Object
Sets the configuration up with the values given.
43 44 45 46 47 |
# File 'lib/halcyon/config.rb', line 43 def configure(config={}) config.each do |(key, val)| self.config[key] = val end end |
#defaults(env = nil) ⇒ Object
Shortcut for Halcyon::Config.defaults.
174 175 176 |
# File 'lib/halcyon/config.rb', line 174 def defaults(env = nil) Halcyon::Config.defaults(env) end |
#delete(key) ⇒ Object
135 136 137 |
# File 'lib/halcyon/config.rb', line 135 def delete(key) self.config.delete(key) end |
#get(key) ⇒ Object
107 108 109 |
# File 'lib/halcyon/config.rb', line 107 def get(key) self.config[key] end |
#inspect ⇒ Object
186 187 188 189 190 |
# File 'lib/halcyon/config.rb', line 186 def inspect attrs = "" self.config.keys.each {|key| attrs << " #{key}=#{self.config[key].inspect}"} "#<Halcyon::Config#{attrs}>" end |
#load_from(path) ⇒ Object
Loads the contents of a configuration file found at path
and merges it with the current configuration.
181 182 183 184 |
# File 'lib/halcyon/config.rb', line 181 def load_from(path) self.configure(Halcyon::Config::File.load(path)) self end |
#put(key_or_config_hash, value = nil) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/halcyon/config.rb', line 121 def put(key_or_config_hash, value = nil) if value.nil? and key_or_config_hash.is_a?(Hash) self.configure(key_or_config_hash) else self.config[key_or_config_hash] = value end end |
#setup(config = {}) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/halcyon/config.rb', line 62 def setup(config={}) if block_given? yield(self.config.dup) end # merge new settings self.configure(config) end |
#to_hash ⇒ Object
Returns the configuration as a hash.
168 169 170 |
# File 'lib/halcyon/config.rb', line 168 def to_hash self.config.to_hash end |
#to_yaml ⇒ Object
Returns the configuration rendered as YAML.
161 162 163 164 |
# File 'lib/halcyon/config.rb', line 161 def to_yaml require 'yaml' self.config.to_hash.to_yaml end |