Module: Configliere::ConfigFile
- Defined in:
- lib/configliere/config_file.rb
Overview
ConfigFile -- load configuration from a simple YAML file
Instance Method Summary collapse
- #default_conf_dir ⇒ Object
- #determine_conf_location(level, scope) ⇒ Object
- #load_configuration_in_order!(scope = 'configliere') ⇒ Object
- #lookup_conf_dir(level, scope) ⇒ Object
-
#read(filename, options = {}) ⇒ Configliere::Params
Load params from a YAML file, as a hash of handle => param_hash pairs.
-
#read_json(json_str, options = {}) ⇒ Object
we depend on you to require some sort of JSON.
- #read_yaml(yaml_str, options = {}) ⇒ Object
-
#save!(filename) ⇒ Object
save to disk.
Instance Method Details
#default_conf_dir ⇒ Object
89 90 91 |
# File 'lib/configliere/config_file.rb', line 89 def default_conf_dir lookup_conf_dir(:user, 'configliere') end |
#determine_conf_location(level, scope) ⇒ Object
85 86 87 |
# File 'lib/configliere/config_file.rb', line 85 def determine_conf_location(level, scope) lookup_conf_dir(level, scope).join("#{scope}.yaml").to_s end |
#load_configuration_in_order!(scope = 'configliere') ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/configliere/config_file.rb', line 97 def load_configuration_in_order!(scope = 'configliere') [ :machine, :user, :app ].each do |level| conf = determine_conf_location(level, scope) read(conf) if Pathname(conf).exist? end resolve! end |
#lookup_conf_dir(level, scope) ⇒ Object
93 94 95 |
# File 'lib/configliere/config_file.rb', line 93 def lookup_conf_dir(level, scope) Configliere::DEFAULT_CONFIG_LOCATION[level].call(scope) end |
#read(filename, options = {}) ⇒ Configliere::Params
Load params from a YAML file, as a hash of handle => param_hash pairs
The env option is not coerced to_sym, so make sure your key type matches the file's
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/configliere/config_file.rb', line 35 def read filename, ={} if filename.is_a?(Symbol) then raise Configliere::DeprecatedError, "Loading from a default config file is no longer provided" ; end filename = (filename) begin case filetype(filename) when 'json' then read_json(File.open(filename), ) when 'yaml' then read_yaml(File.open(filename), ) else read_yaml(File.open(filename), ) end rescue Errno::ENOENT warn "Loading empty configliere settings file #{filename}" end self end |
#read_json(json_str, options = {}) ⇒ Object
we depend on you to require some sort of JSON
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/configliere/config_file.rb', line 64 def read_json json_str, ={} require 'multi_json' new_data = MultiJson.load(json_str) || {} # Extract the :env (production/development/etc) if [:env] new_data = new_data[[:env]] || {} end deep_merge! new_data self end |
#read_yaml(yaml_str, options = {}) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/configliere/config_file.rb', line 50 def read_yaml yaml_str, ={} require 'yaml' new_data = YAML.load(yaml_str) || {} # Extract the :env (production/development/etc) if [:env] new_data = new_data[[:env]] || {} end deep_merge! new_data self end |
#save!(filename) ⇒ Object
save to disk.
- file is in YAML format, as a hash of handle => param_hash pairs
- filename defaults to Configliere::DEFAULT_CONFIG_FILE (~/.configliere, probably)
78 79 80 81 82 83 |
# File 'lib/configliere/config_file.rb', line 78 def save! filename filename = (filename) hsh = self.export.to_hash FileUtils.mkdir_p(File.dirname(filename)) File.open(filename, 'w'){|file| file << YAML.dump(hsh) } end |