Class: Sidewalk::Config
- Inherits:
-
Object
- Object
- Sidewalk::Config
- Defined in:
- lib/sidewalk/config.rb
Overview
Class for storing application configuration.
This will:
-
load
config/environment.rb
(arbitrary Ruby) -
read
config/environment.yaml
into aHash
-
the same for config/#{ENV}.rb and
.yaml
Class Attribute Summary collapse
-
.path ⇒ Object
Where to look for configuration files.
Class Method Summary collapse
- .development? ⇒ Boolean
-
.environment ⇒ Object
What the current Rack environment is.
-
.environment=(foo) ⇒ Object
Override the auto-detected environment.
-
.instance ⇒ Config
(also: init!)
The main instance of Config.
- .production? ⇒ Boolean
- .testing? ⇒ Boolean
Instance Method Summary collapse
-
#[](key) ⇒ Object
Return a configuration value from YAML.
-
#[]=(key, value) ⇒ Object
Store a configuration value, as if it were in the YAML.
-
#initialize(path) ⇒ Config
constructor
Initialize a Config object at the given path.
-
#load_ruby!(file, options = {}) ⇒ Object
Execute a Ruby file.
-
#load_yaml!(file, options = {}) ⇒ Object
Read a YAML file, and merge with the current config.
Constructor Details
#initialize(path) ⇒ Config
Initialize a Config object at the given path.
You probably want to use the class methods instead.
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/sidewalk/config.rb', line 26 def initialize path @config = Hash.new @root = path [ 'environment.rb', "#{Config.environment}.rb", ].each{|x| load_ruby!(x, :silent => true)} yaml_configs = [ 'environment.yaml', "#{Config.environment}.yaml", ].each{|x| load_yaml!(x, :silent => true)} end |
Class Attribute Details
.path ⇒ Object
Where to look for configuration files.
This defaults to config/
underneath the application root.
117 118 119 |
# File 'lib/sidewalk/config.rb', line 117 def path @path ||= Application.local_root + '/config' end |
Class Method Details
.development? ⇒ Boolean
147 148 149 |
# File 'lib/sidewalk/config.rb', line 147 def development? self.environment == 'development' end |
.environment ⇒ Object
What the current Rack environment is.
This is an arbitrary string, but will usually be:
- production
-
this is live, probably via Passenger
- development
-
running on a developer’s local machine, probably via
+rackup+ or +shotgun+
- testing
-
automated tests are running.
This is copied from ENV, but can be overridden (see #environment=).
132 133 134 |
# File 'lib/sidewalk/config.rb', line 132 def environment @environment ||= ENV['RACK_ENV'] || raise("Unable to determine environment. Set ENV['RACK_ENV'].") end |
.environment=(foo) ⇒ Object
Override the auto-detected environment.
Handy for testing - especially as RACK_ENV might not even be set.
139 140 141 |
# File 'lib/sidewalk/config.rb', line 139 def environment= foo @environment = foo end |
.instance ⇒ Config Also known as: init!
The main instance of Sidewalk::Config.
You probably don’t want to use this - all of the methods defined on instances are usable as class methods instead, that map onto the instance.
109 110 111 |
# File 'lib/sidewalk/config.rb', line 109 def instance @instance ||= Config.new(self.path) end |
.production? ⇒ Boolean
143 144 145 |
# File 'lib/sidewalk/config.rb', line 143 def production? self.environment == 'production' end |
.testing? ⇒ Boolean
151 152 153 |
# File 'lib/sidewalk/config.rb', line 151 def testing? self.environment == 'testing' end |
Instance Method Details
#[](key) ⇒ Object
Return a configuration value from YAML.
Acts like a Hash
. Also available as a class method.
45 46 47 |
# File 'lib/sidewalk/config.rb', line 45 def [] key @config[key] end |
#[]=(key, value) ⇒ Object
Store a configuration value, as if it were in the YAML.
Also available as a class method.
55 56 57 |
# File 'lib/sidewalk/config.rb', line 55 def []= key, value @config[key] = value end |
#load_ruby!(file, options = {}) ⇒ Object
Execute a Ruby file.
Nothing special is done, it’s just evaluated.
Also available as a class method.
70 71 72 73 74 75 76 77 |
# File 'lib/sidewalk/config.rb', line 70 def load_ruby! file, = {} path = File.join(@root, file) begin load path rescue LoadError raise unless [:silent] end end |
#load_yaml!(file, options = {}) ⇒ Object
Read a YAML file, and merge with the current config.
This is available via #[]
Also available as a class method.
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/sidewalk/config.rb', line 90 def load_yaml! file, = {} path = File.join(@root, file) if File.exists? path @config.merge! YAML.load(File.read(path)) else unless [:silent] raise LoadError.new("unable to find #{file}") end end end |