Class: EnvConfig
- Inherits:
-
Object
- Object
- EnvConfig
- Defined in:
- lib/env_config.rb,
lib/version.rb
Overview
usage example:
# environment.rb EnvConfig.configure(:default) do |config|
config[:verbose] = true
end
# test.rb EnvConfig.configure(:test) do |config|
config[:verbose] = false
end
# development.rb EnvConfig.configure(:development) do |config| end
config = EnvConfig.new(:test) config # => false
config = EnvConfig.new(:development) config # => true
Exceptions:
config # => EnvConfig::ArgumentError
config = EnvConfig.new(:unknown_env_123) # => EnvConfig::UnknownEnvironmentError
config = EnvConfig.new(:default) # => EnvConfig::AbstractError
EnvConfig.configure(:test) do … EnvConfig.configure(:default) do … # => EnvConfig::LoadOrderError
config = false # => EnvConfig::ImmutableError
Defined Under Namespace
Classes: AbstractError, ArgumentError, ImmutableError, LoadOrderError, UnknownEnvironmentError
Constant Summary collapse
- VERSION =
"0.0.2"
- @@configurations =
{}
Instance Attribute Summary collapse
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#[]=(key, value) ⇒ Object
NOTE this ensures readonly only for toplevel key=>value, not the value objects themselves.
-
#initialize(environment) ⇒ EnvConfig
constructor
A new instance of EnvConfig.
Constructor Details
#initialize(environment) ⇒ EnvConfig
Returns a new instance of EnvConfig.
56 57 58 59 60 |
# File 'lib/env_config.rb', line 56 def initialize(environment) @environment = environment.to_sym raise AbstractError if @environment == :default raise UnknownEnvironmentError if !@@configurations.has_key?(@environment) end |
Instance Attribute Details
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
48 49 50 |
# File 'lib/env_config.rb', line 48 def environment @environment end |
Class Method Details
.configure(environment) {|@@configurations[environment]| ... } ⇒ Object
50 51 52 53 54 |
# File 'lib/env_config.rb', line 50 def self.configure(environment) environment = environment.to_sym set_default_values(environment) yield @@configurations[environment] end |
Instance Method Details
#[](key) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/env_config.rb', line 62 def [](key) # be strict if !get_config.has_key?(key) raise ArgumentError.new("key '#{key}' not defined in configuration or default") end get_config[key] end |
#[]=(key, value) ⇒ Object
NOTE this ensures readonly only for toplevel key=>value, not the value objects themselves
72 73 74 |
# File 'lib/env_config.rb', line 72 def []=(key, value) raise ImmutableError.new("config is read-only") end |