Module: Choices

Extended by:
Choices
Included in:
Choices
Defined in:
lib/choices.rb

Defined Under Namespace

Modules: Rails

Instance Method Summary collapse

Instance Method Details

#load_settings(filename, env) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/choices.rb', line 7

def load_settings(filename, env)
  mash = Hashie::Mash.new(load_settings_hash(filename, env))
  
  with_local_settings(filename, env, '.local') do |local|
    mash.update local
  end
  
  return mash
end

#load_settings_hash(filename, env) ⇒ Object



17
18
19
20
# File 'lib/choices.rb', line 17

def load_settings_hash(filename, env)
  yaml_content = ERB.new(IO.read(filename)).result
  yaml_load(yaml_content)[env]
end

#with_local_settings(filename, env, suffix) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/choices.rb', line 22

def with_local_settings(filename, env, suffix)
  local_filename = filename.sub(/(\.\w+)?$/, "#{suffix}\\1")
  if File.exists? local_filename
    hash = load_settings_hash(local_filename, env)
    yield hash if hash
  end
end

#yaml_load(content) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/choices.rb', line 30

def yaml_load(content)
  if defined?(YAML::ENGINE) && defined?(Syck)
    # avoid using broken Psych in 1.9.2
    old_yamler = YAML::ENGINE.yamler
    YAML::ENGINE.yamler = 'syck'
  end
  begin
    YAML::load(content)
  ensure
    YAML::ENGINE.yamler = old_yamler if defined?(YAML::ENGINE) && defined?(Syck)
  end
end