Class: Gless::EnvConfig
- Inherits:
-
Object
- Object
- Gless::EnvConfig
- Defined in:
- lib/gless/config.rb
Overview
Provides a bit of a wraper around yaml config files; nothing terribly complicated. Can merge multiple configs together. Expects all configs to be hashes.
Class Method Summary collapse
-
.env_dir=(dir) ⇒ Object
Bootstrapping method used to inform Gless as to where config files can be found.
Instance Method Summary collapse
-
#add_file(file) ⇒ Object
Add a file to those in use for configuration data.
- #deep_merge(b) ⇒ Object
-
#get(*args) ⇒ Object
Get an element from the configuration.
-
#get_default(default, *args) ⇒ Object
Optionally get an element from the configuration, otherwise returning the default value.
-
#initialize(hash = nil) ⇒ Gless::EnvConfig
constructor
Sets up the initial configuration environment.
- #merge(hash) ⇒ Object
-
#set(*indices, value) ⇒ Object
Set an element in the configuration to the given value, passed after all of the indices.
Constructor Details
#initialize(hash = nil) ⇒ Gless::EnvConfig
Sets up the initial configuration environment. @@env_dir must be set before this, or things will go poorly. The file it wants to load is, loosely, @@env_dir/lib/config/ENVIRONMENT.yml, where ENVIRONMENT is the environment variable of that name.
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/gless/config.rb', line 29 def initialize( hash = nil ) if hash @config = hash else @env = (ENV['ENVIRONMENT'] || 'development').to_sym env_file = "#{@@env_dir}/config/#{@env}.yml" raise "You need to create a configuration file named '#{@env}.yml' (generated from the ENVIRONMENT environment variable) under #{@@env_dir}/lib/config" unless File.exists? env_file @config = YAML::load_file env_file end end |
Class Method Details
.env_dir=(dir) ⇒ Object
Bootstrapping method used to inform Gless as to where config files can be found.
18 19 20 |
# File 'lib/gless/config.rb', line 18 def self.env_dir=(dir) @@env_dir=dir end |
Instance Method Details
#add_file(file) ⇒ Object
Add a file to those in use for configuration data. Simply merges in the new data, so each file should probably have its own top level singleton hash.
46 47 48 |
# File 'lib/gless/config.rb', line 46 def add_file file @config.merge!(YAML::load_file "#{@@env_dir}/#{file}") end |
#deep_merge(b) ⇒ Object
88 89 90 91 |
# File 'lib/gless/config.rb', line 88 def deep_merge(b) iter = -> a, step {a.merge(step) {|key, oldval, newval| [oldval, newval].all? {|v| v.kind_of? Hash} ? iter.(oldval, newval) : newval}}; @config = iter.(@config, b) end |
#get(*args) ⇒ Object
Get an element from the configuration. Takes an arbitrary number of arguments; each is taken to be a hash key. With no arguments, returns the whole configuration.
60 61 62 63 64 |
# File 'lib/gless/config.rb', line 60 def get( *args ) r = get_default nil, *args raise "Could not locate '#{args.join '.'}' in YAML config; please ensure that '#{@env}.yml' is up to date." if r.nil? r end |
#get_default(default, *args) ⇒ Object
Optionally get an element from the configuration, otherwise returning the default value.
75 76 77 78 79 80 81 82 |
# File 'lib/gless/config.rb', line 75 def get_default( default, *args ) if args.empty? return @config end r = get_sub_tree( @config, *args ) r.nil? ? default : r end |
#merge(hash) ⇒ Object
84 85 86 |
# File 'lib/gless/config.rb', line 84 def merge(hash) @config.merge!(hash) end |
#set(*indices, value) ⇒ Object
Set an element in the configuration to the given value, passed after all of the indices.
99 100 101 |
# File 'lib/gless/config.rb', line 99 def set(*indices, value) set_root @config, value, *indices end |