Class: Dacs::AppConfig
- Inherits:
-
Hash
- Object
- Hash
- Dacs::AppConfig
- Extended by:
- Forwardable
- Defined in:
- lib/dacs/app_config.rb
Overview
This configuration system is for deployment-specific values, such as AWS keys and API URLs.
Configuration values can be accessed anywhere in the app using the AppConfig class, which behaves like a Hash.
Note that all keys are strings - not symbols.
Configuration keys can be set at three levels: hardcoded defaults, in config/<APPNAME>.yml, or in the environment.
-
Defaults are set below, in the initializer.
-
Defaults will be overridden by config/<APPNAME>.yml. This file is broken into per-environment sections just like database.yml, so to configure for the development environment, you’d use something like the following in config/<APPNAME>.yml:
development:
api_base_uri: http://localhost:4567 authenticate: false aws_access_key: 'XXX...' aws_secret_key: 'XXX...' simpledb_domain: 'my_sandbox_domain'
-
Values in <APPNAME>.yml will be overidden by <APPNAME>_* environment variables. For instance, to set key ‘foo’ = ‘bar’, set APPNAME_FOO=‘bar’ in the process environment.
Instance Attribute Summary collapse
-
#app_name ⇒ Object
readonly
Returns the value of attribute app_name.
-
#config_path ⇒ Object
readonly
Returns the value of attribute config_path.
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Class Method Summary collapse
- .environment ⇒ Object
-
.init!(app_name, options = {}) ⇒ Object
Usage:.
- .instance ⇒ Object
- .schema ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #dump ⇒ Object
- #fetch(key, &block) ⇒ Object
-
#initialize ⇒ AppConfig
constructor
A new instance of AppConfig.
- #source(key) ⇒ Object
Constructor Details
#initialize ⇒ AppConfig
Returns a new instance of AppConfig.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/dacs/app_config.rb', line 94 def initialize raise "You must initialize with Dacs::AppConfig.init!()" unless @@options @app_name = @@options[:app_name] @config_path = @@options[:config_path] @logger = @@options[:logger] @environment = @@options[:environment] @defaults = self.class.schema.defaults find_or_create_config_file! defaults_source = DefaultSource.new(@defaults) file_source = FileSource.new(config_path, @environment) env_source = EnvironmentSource.new(@app_name) load_values!(self.class.schema, env_source, file_source, defaults_source) end |
Instance Attribute Details
#app_name ⇒ Object (readonly)
Returns the value of attribute app_name.
89 90 91 |
# File 'lib/dacs/app_config.rb', line 89 def app_name @app_name end |
#config_path ⇒ Object (readonly)
Returns the value of attribute config_path.
91 92 93 |
# File 'lib/dacs/app_config.rb', line 91 def config_path @config_path end |
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
92 93 94 |
# File 'lib/dacs/app_config.rb', line 92 def environment @environment end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
90 91 92 |
# File 'lib/dacs/app_config.rb', line 90 def logger @logger end |
Class Method Details
.environment ⇒ Object
85 86 87 |
# File 'lib/dacs/app_config.rb', line 85 def self.environment @@options[:environment] end |
.init!(app_name, options = {}) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/dacs/app_config.rb', line 59 def self.init!(app_name, ={}) @instance = nil @@options = .merge(:app_name => app_name) @@options[:app_root] ||= Pathname(Dir.pwd) @@options[:config_path] ||= @@options[:app_root] + 'config' + "#{app_name}.yml" @@options[:logger] ||= ::Logger.new($stderr) @@options[:environment] ||= :development @@options[:defaults] ||= {} if block_given? schema = Schema.new yield(schema) @@schema = schema else @@schema = PermissiveSchema.new(@@options[:defaults]) end self.instance end |
.instance ⇒ Object
77 78 79 |
# File 'lib/dacs/app_config.rb', line 77 def self.instance @instance ||= new end |
.schema ⇒ Object
81 82 83 |
# File 'lib/dacs/app_config.rb', line 81 def self.schema @@schema end |
Instance Method Details
#[](key) ⇒ Object
117 118 119 |
# File 'lib/dacs/app_config.rb', line 117 def [](key) super(key).value end |
#dump ⇒ Object
128 129 130 131 132 133 134 |
# File 'lib/dacs/app_config.rb', line 128 def dump table = Table(%w[Key Value Source]) each_pair do |key, configured_value| table << [key, configured_value.value, configured_value.source.to_s] end table.as(:text) end |
#fetch(key, &block) ⇒ Object
121 122 123 124 125 126 |
# File 'lib/dacs/app_config.rb', line 121 def fetch(key, &block) case result = super(key, &block) when ConfiguredValue then result.value else result end end |
#source(key) ⇒ Object
110 111 112 113 114 115 |
# File 'lib/dacs/app_config.rb', line 110 def source(key) configured_value = Hash.instance_method(:fetch).bind(self).call(key.to_s) do raise ConfigurationError, "No such key '#{key}'" end configured_value.source.to_s end |