Module: Choices
Defined Under Namespace
Modules: Rails
Instance Method Summary collapse
- #load_settings(filename, env) ⇒ Object
- #load_settings_hash(filename) ⇒ Object
- #with_local_settings(filename, suffix) ⇒ Object
- #yaml_load(content) ⇒ Object
Instance Method Details
#load_settings(filename, env) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/choices.rb', line 8 def load_settings(filename, env) mash = Hashie::Mash.new(load_settings_hash(filename)) with_local_settings(filename, '.local') do |local| mash.update local end mash.fetch(env) do raise IndexError, %{Missing key for "#{env}" in `#{filename}'} end end |
#load_settings_hash(filename) ⇒ Object
20 21 22 23 |
# File 'lib/choices.rb', line 20 def load_settings_hash(filename) yaml_content = ERB.new(IO.read(filename)).result yaml_load(yaml_content) end |
#with_local_settings(filename, suffix) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/choices.rb', line 25 def with_local_settings(filename, suffix) local_filename = filename.sub(/(\.\w+)?$/, "#{suffix}\\1") if File.exists? local_filename hash = load_settings_hash(local_filename) yield hash if hash end end |
#yaml_load(content) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/choices.rb', line 33 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 |