module Configliere::ConfigFile

ConfigFile – load configuration from a simple YAML file

Public Instance Methods

default_conf_dir() click to toggle source
# File lib/configliere/config_file.rb, line 89
def default_conf_dir
  lookup_conf_dir(:user, 'configliere')
end
determine_conf_location(level, scope) click to toggle source
# 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') click to toggle source
# 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) click to toggle source
# 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={}) click to toggle source

Load params from a YAML file, as a hash of handle => param_hash pairs

@param filename [String] the file to read. If it does not contain a ‘/’,

the filename is expanded relative to Configliere::DEFAULT_CONFIG_DIR

@param options [Hash] @option options :env [String]

If an :env option is given, only the indicated subhash is merged. This
lets you for example specify production / environment / test settings

@return [Configliere::Params] the Settings object

@example

# Read from ~/.configliere/foo.yaml
Settings.read(foo.yaml)

@example

# Read from config/foo.yaml and use settings appropriate for development/staging/production
Settings.read(App.root.join('config', 'environment.yaml'), :env => ENV['RACK_ENV'])

The env option is not coerced to_sym, so make sure your key type matches the file’s

# File lib/configliere/config_file.rb, line 35
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
    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
    warn "Loading empty configliere settings file #{filename}"
  end
  self
end
read_json(json_str, options={}) click to toggle source

we depend on you to require some sort of JSON

# File lib/configliere/config_file.rb, line 64
def read_json json_str, options={}
  require 'multi_json'
  new_data = MultiJson.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
read_yaml(yaml_str, options={}) click to toggle source
# File lib/configliere/config_file.rb, line 50
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
save!(filename) click to toggle source

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)

# File lib/configliere/config_file.rb, line 78
def save! filename
  filename = expand_filename(filename)
  hsh = self.export.to_hash
  FileUtils.mkdir_p(File.dirname(filename))
  File.open(filename, 'w'){|file| file << YAML.dump(hsh) }
end

Protected Instance Methods

expand_filename(filename) click to toggle source
# File lib/configliere/config_file.rb, line 112
def expand_filename filename
  if filename.to_s.include?('/')
    File.expand_path(filename)
  else
    default_conf_dir.join(filename).to_s
  end
end
filetype(filename) click to toggle source
# File lib/configliere/config_file.rb, line 107
def filetype filename
  filename =~ /\.([^\.]+)$/ ;
  $1
end