Class: ConfigReader
- Inherits:
-
Object
- Object
- ConfigReader
- Defined in:
- lib/config_reader.rb,
lib/config_reader/version.rb,
lib/config_reader/magic_hash.rb
Defined Under Namespace
Classes: MagicHash
Constant Summary collapse
- VERSION =
"1.0.1"
Class Attribute Summary collapse
-
.config ⇒ Object
Returns the value of attribute config.
-
.config_file ⇒ Object
Returns the value of attribute config_file.
-
.sekrets_file ⇒ Object
Returns the value of attribute sekrets_file.
Class Method Summary collapse
- .[](key) ⇒ Object
- .find_config ⇒ Object
- .find_env ⇒ Object
- .inspect ⇒ Object
- .load_config ⇒ Object
- .load_sekrets ⇒ Object
- .merge_configs(env, conf, sekrets) ⇒ Object
- .method_missing(key, *args, &block) ⇒ Object
- .reload ⇒ Object
Class Attribute Details
.config ⇒ Object
Returns the value of attribute config.
13 14 15 |
# File 'lib/config_reader.rb', line 13 def config @config end |
.config_file ⇒ Object
Returns the value of attribute config_file.
13 14 15 |
# File 'lib/config_reader.rb', line 13 def config_file @config_file end |
.sekrets_file ⇒ Object
Returns the value of attribute sekrets_file.
13 14 15 |
# File 'lib/config_reader.rb', line 13 def sekrets_file @sekrets_file end |
Class Method Details
.[](key) ⇒ Object
28 29 30 |
# File 'lib/config_reader.rb', line 28 def [](key) config[key.to_sym] end |
.find_config ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/config_reader.rb', line 32 def find_config return @config_file if File.exist?(@config_file) %w( . config ).each do |dir| config_file = File.join(dir, @config_file) return config_file if File.exist?(config_file) end '' end |
.find_env ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/config_reader.rb', line 55 def find_env if defined?(Rails) && Rails.stage Rails.stage elsif defined?(Rails) && Rails.env Rails.env elsif defined?(RAILS_ENV) RAILS_ENV elsif defined?(Padrino) && Padrino.env Padrino.env.to_s elsif defined?(PADRINO_ENV) PADRINO_ENV elsif ENV['RACK_ENV'] ENV['RACK_ENV'] elsif defined?(APP_ENV) APP_ENV end end |
.inspect ⇒ Object
51 52 53 |
# File 'lib/config_reader.rb', line 51 def inspect puts config.inspect end |
.load_config ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/config_reader.rb', line 73 def load_config raise 'No config file set' unless @config_file if defined?(ERB) conf = YAML.load(ERB.new(File.open(find_config).read).result) else conf = YAML.load(File.open(find_config).read) end raise 'No config found' unless conf conf end |
.load_sekrets ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/config_reader.rb', line 87 def load_sekrets sekrets = {} if @sekrets_file begin require 'sekrets' sekrets = ::Sekrets.settings_for(@sekrets_file) raise 'No sekrets found' unless sekrets rescue LoadError $stderr.puts "You specified a sekrets_file, but the sekrets gem isn't available." end end sekrets end |
.merge_configs(env, conf, sekrets) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/config_reader.rb', line 103 def merge_configs(env, conf, sekrets) _conf = conf['defaults'] _conf.merge!(sekrets['defaults']) if sekrets && sekrets['defaults'] _conf.merge!(conf[env]) if conf[env] _conf.merge!(sekrets[env]) if sekrets && sekrets[env] ConfigReader::MagicHash.convert_hash(_conf) end |
.method_missing(key, *args, &block) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/config_reader.rb', line 43 def method_missing(key, *args, &block) if key.to_s.end_with?('=') raise ArgumentError.new('ConfigReader is immutable') end config[key] || nil end |
.reload ⇒ Object
24 25 26 |
# File 'lib/config_reader.rb', line 24 def reload merge_configs(find_env, load_config, load_sekrets) end |