lib/configliere/config_file.rb in configliere-0.4.6 vs lib/configliere/config_file.rb in configliere-0.4.7

- old
+ new

@@ -28,27 +28,45 @@ # The env option is *not* coerced to_sym, so make sure your key type matches the file's def read filename, options={} if filename.is_a?(Symbol) then raise Configliere::DeprecatedError, "Loading from a default config file is no longer provided" ; end filename = expand_filename(filename) begin - read_yaml(File.open(filename), options) + case filetype(filename) + when 'json' then read_json(File.open(filename), options) + when 'yaml' then read_yaml(File.open(filename), options) + else read_yaml(File.open(filename), options) + end rescue Errno::ENOENT => e warn "Loading empty configliere settings file #{filename}" end self end def read_yaml yaml_str, options={} + require 'yaml' new_data = YAML.load(yaml_str) || {} # Extract the :env (production/development/etc) if options[:env] new_data = new_data[options[:env]] || {} end deep_merge! new_data self end + # + # we depend on you to require some sort of JSON + # + def read_json json_str, options={} + new_data = JSON.load(json_str) || {} + # Extract the :env (production/development/etc) + if options[:env] + new_data = new_data[options[:env]] || {} + end + deep_merge! new_data + self + end + # 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) def save! filename filename = expand_filename(filename) @@ -56,9 +74,14 @@ FileUtils.mkdir_p(File.dirname(filename)) File.open(filename, 'w'){|f| f << YAML.dump(hsh) } end protected + + def filetype filename + filename =~ /\.([^\.]+)$/ ; + $1 + end def expand_filename filename if filename.to_s.include?('/') File.expand_path(filename) else