Permalink
Browse files

Allow loading config from CONFIG_JSON env var

  • Loading branch information...
1 parent fe21134 commit 2135f144b5c5f637a74400df6a2e8227b4587b3e @mloughran mloughran committed Jan 3, 2014
Showing with 18 additions and 4 deletions.
  1. +18 −4 app.rb
View
22 app.rb
@@ -4,11 +4,25 @@
require 'yaml'
require 'json'
+def symbolize(obj)
+ case obj
+ when Hash
+ obj.inject({}) { |mem, (k,v)| mem[k.to_sym] = symbolize(v); mem }
+ else
+ obj
+ end
+end
+
class Environment
CONFIG = begin
- YAML.load_file(File.expand_path('../config.yml', __FILE__))
- rescue Errno::ENOENT
- raise "Please make sure that a ./config.yml file exists (see .example)"
+ if ENV["CONFIG_JSON"]
+ symbolize JSON.parse(ENV["CONFIG_JSON"])
+ else
+ YAML.load_file(File.expand_path('../config.yml', __FILE__))
+ end
+ rescue => e
+ puts "Config required in a CONFIG_JSON env variable or ./config.yml (see .example)"
+ raise e
end
attr_reader :name
@@ -19,7 +33,7 @@ def self.list
def initialize(name)
@name = name
- env_config = CONFIG[name] || raise("Unknown config")
+ env_config = CONFIG[name.to_sym] || raise("Unknown config")
@nof

nof Jan 23, 2014

hi.
by this change, does not load config.yml.example

@mloughran

mloughran Jan 23, 2014

Contributor

Good point, fixed in 067ec7a

# Default config
@config = {

0 comments on commit 2135f14

Please sign in to comment.