Class: EzConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/ez_config.rb

Defined Under Namespace

Classes: NoConfigForEnv

Constant Summary collapse

PRODUCTION_REGEX =
/^production/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opt = {}) ⇒ EzConfig

Returns a new instance of EzConfig.



24
25
26
27
28
# File 'lib/ez_config.rb', line 24

def initialize(opt={})
  @env              = opt[:env].to_s || ENV['RACK_ENV'] || ENV['RAILS_ENV']
  @path             = opt[:path] || "#{Dir.pwd}/config/app_config"
  @production_regex = opt[:production_regex] || PRODUCTION_REGEX
end

Class Method Details

.[](k) ⇒ Object



15
16
17
# File 'lib/ez_config.rb', line 15

def [](k)
  instance[k]
end

.configure(opt) ⇒ Object



7
8
9
# File 'lib/ez_config.rb', line 7

def configure(opt)
  @opt = opt
end

.instanceObject



11
12
13
# File 'lib/ez_config.rb', line 11

def instance
  @instance ||= new (@opt || {})
end

.to_hashObject



19
20
21
# File 'lib/ez_config.rb', line 19

def to_hash
  instance.to_hash
end

Instance Method Details

#[](k) ⇒ Object



30
31
32
# File 'lib/ez_config.rb', line 30

def [](k)
  to_hash[k]
end

#configObject Also known as: to_hash



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ez_config.rb', line 42

def config
  @config ||= files.inject({}) do |config, file|
    key   = File.basename file, '.yml'
    yaml  = YAML.load_file file
    val   = yaml[@env] || yaml[default_env]

    raise NoConfigForEnv, "Environment #{@env} not found in #{file}" unless val

    config[key] = val
    config
  end
end

#default_envObject



38
39
40
# File 'lib/ez_config.rb', line 38

def default_env
  @env =~ @production_regex ? 'production' : 'non_production'
end

#filesObject



34
35
36
# File 'lib/ez_config.rb', line 34

def files
  Dir.glob File.join(@path, '*.yml')
end