Class: EnvConfig

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment) ⇒ EnvConfig

Returns a new instance of EnvConfig.

Raises:



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

#environmentObject (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

Yields:



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

Raises:



72
73
74
# File 'lib/env_config.rb', line 72

def []=(key, value)
  raise ImmutableError.new("config is read-only")
end