Class: FlexConf

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/flexconf.rb

Overview

A simple but flexible configuration class.

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ FlexConf #initialize(yaml_file, opts) ⇒ FlexConf #initializeFlexConf

Loads configuration from the supplied source(s).

Overloads:

  • #initialize(hash) ⇒ FlexConf

    Reads the supplied hash as the entire configuration. No options are handled; you’ll have to make sure the data you provide contains everything you intend it to.

    Parameters:

    • source (Hash)

      The configuration data you want to provide.

  • #initialize(yaml_file, opts) ⇒ FlexConf

    Loads the given YAML file and optional overrides.

    Parameters:

    • source (String, Hash)

      A YAML filename. The file must be in the current directory or a complete path must be given.

    • opts (optional, Hash)

      Specifies ways in which the supplied data can be overridden. Overrides are processed in the following order:

      1. `:scope` (limit the source configuration namespace)
      2. `:local` (secondary '*_local.yml' file)
      3. `:override` (hash supplied in code)
      4. `:environment` (environment variables)
      

    Options Hash (opts):

    • :override (Hash)

      A hash which will be merged over the source configuration.

    • :local (String, Boolean)

      A second YAML file containing local data (presumably outside source control). No error is raised if this file does not exist. A value of ‘true’ appends ‘_local` to the source file’s base name, e.g. ‘config_local.yml`.

    • :environment (Array, Boolean)

      If given an array of environment variable names, FlexConf will lowercase the names and then inject or override them into the configuration. Nested values can be marked by a double underscore (i.e. ‘FOO__BAR’ would set a value at [:foo]). A value of ‘true’ scans all environment variables, but will only override existing values (e.g. an existing config value at [:aws_access_key] could be overridden by the AWS_ACCESS_KEY environment variable.)

      (Use with caution! Casually setting ‘:environment => true’ if you have configuration variables such as [:home] or [:path] could have undesired results.)

    • :scope (String, Symbol)

      Limits the configuration to the values beneath the given top-level key in the YAML source file. Useful for Rails-style environment configurations (‘development’, ‘production’, etc.) Local, environment, and hash overrides are unaffected; they are assumed to be already within the intended scope.

  • #initializeFlexConf

    With no parameters, takes a common default case and acts as if you had run ‘FlexConf.new(’config.yml’, :local => ‘config_local.yml’, :environment => true)‘. Raises an exception if ’config.yml’ does not exist.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/flexconf.rb', line 50

def initialize(source=nil, options=nil)
  @data = {}
  case source
  when /.*\.yml/
    source_data = YAML.load_file(source)
    if options && options[:scope]
      flexify scoped(source_data, options[:scope])
    else
      flexify source_data
    end
    handle_overrides(source, options) if options
  when Hash
    flexify source
  when nil
    if File.exists?('config.yml')
      initialize('config.yml', :local => 'config_local.yml', :environment => true)
    else
      raise ArgumentError, "FlexConf can't load: no configuration was given and there is no config.yml file to default to."  
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



184
185
186
# File 'lib/flexconf.rb', line 184

def method_missing(name, *args, &block)
  self.has_key?(name) ? self[name] : super
end

Instance Method Details

#[](key) ⇒ Object

Returns the value for the given key, which can be a string or a symbol. Named keys can also be accessed via method calls (i.e. ‘config.foo`). Nested configurations are returned as FlexConf objects.



75
76
77
# File 'lib/flexconf.rb', line 75

def [](key)
  @data[normalize_key(key)]
end

#has_key?(key) ⇒ Boolean

Returns true if the given configuration value exists.

Returns:

  • (Boolean)


80
81
82
# File 'lib/flexconf.rb', line 80

def has_key?(key)
  @data.has_key? normalize_key(key)
end