class ConfigVar::Context

Public Class Methods

new() click to toggle source
# File lib/configvar/context.rb, line 3
def initialize
  @definitions = {}
  @values = {}
end

Public Instance Methods

method_missing(name, *args) click to toggle source

Fetch a configuration value. The name must be a lowercase version of an uppercase name defined in the environment. A NoMethodError is raised if no value matching the specified name is available.

# File lib/configvar/context.rb, line 19
def method_missing(name, *args)
  value = @values[name]
  if value.nil? && !@values.has_key?(name)
    address = "<#{self.class.name}:0x00#{(self.object_id << 1).to_s(16)}>"
    raise NoMethodError.new("undefined method `#{name}' for ##{address}")
  end
  value
end
optional_bool(name, default) click to toggle source

Define a required boolean config var with a default value.

# File lib/configvar/context.rb, line 94
def optional_bool(name, default)
  optional_custom(name) do |env|
    if value = env[name.to_s.upcase]
      {name => parse_bool(name, value)}
    else
      {name => default}
    end
  end
end
optional_custom(name, &blk) click to toggle source

Define an optional custom config var. The block must take the environment as a parameter, load and process values from the it, and return either a single value to bind to the name of the configuration variable or a `Hash` that will be merged into the collection of all config vars.

# File lib/configvar/context.rb, line 109
def optional_custom(name, &blk)
  define_config(name, &blk)
end
optional_int(name, default) click to toggle source

Define a required integer config var with a default value.

# File lib/configvar/context.rb, line 83
def optional_int(name, default)
  optional_custom(name) do |env|
    if value = env[name.to_s.upcase]
      {name => parse_int(name, value)}
    else
      {name => default}
    end
  end
end
optional_string(name, default) click to toggle source

Define a required string config var with a default value.

# File lib/configvar/context.rb, line 72
def optional_string(name, default)
  optional_custom(name) do |env|
    if value = env[name.to_s.upcase]
      {name => value}
    else
      {name => default}
    end
  end
end
reload(env) click to toggle source

Reload the environment from a Hash of available environment values.

# File lib/configvar/context.rb, line 9
def reload(env)
  @values = {}
  @definitions.each_value do |function|
    @values.merge!(function.call(env))
  end
end
required_bool(name) click to toggle source

Define a required boolean config var.

# File lib/configvar/context.rb, line 51
def required_bool(name)
  required_custom(name) do |env|
    if value = env[name.to_s.upcase]
      {name => parse_bool(name, value)}
    else
      raise ConfigError.new("A value must be provided for #{name.to_s.upcase}")
    end
  end
end
required_custom(name, &blk) click to toggle source

Define a required custom config var. The block must take the environment as a parameter, load and process values from the it, and return either a single value to bind to the name of the configuration variable or a `Hash` that will be merged into the collection of all config vars. If a required value is not found in the environment the block must raise a ConfigVar::ConfigError exception.

# File lib/configvar/context.rb, line 67
def required_custom(name, &blk)
  define_config(name, &blk)
end
required_int(name) click to toggle source

Define a required integer config var.

# File lib/configvar/context.rb, line 40
def required_int(name)
  required_custom(name) do |env|
    if value = env[name.to_s.upcase]
      {name => parse_int(name, value)}
    else
      raise ConfigError.new("A value must be provided for #{name.to_s.upcase}")
    end
  end
end
required_string(name) click to toggle source

Define a required string config var.

# File lib/configvar/context.rb, line 29
def required_string(name)
  required_custom(name) do |env|
    if value = env[name.to_s.upcase]
      {name => value}
    else
      raise ConfigError.new("A value must be provided for #{name.to_s.upcase}")
    end
  end
end

Private Instance Methods

define_config(name) { |env| ... } click to toggle source

Define a handler for a configuration value.

# File lib/configvar/context.rb, line 136
def define_config(name, &blk)
  if @definitions.has_key?(name)
    raise ConfigError.new("#{name.to_s.upcase} is already registered")
  end
  @definitions[name] = Proc.new do |env|
    value = yield env
    if value.kind_of?(Hash)
      value
    else
      {name => value}
    end
  end
end
parse_bool(name, value) click to toggle source

Convert a string to boolean. An ArgumentError is raised if the string is not a valid boolean.

# File lib/configvar/context.rb, line 125
def parse_bool(name, value)
  if ['1', 'true', 'enabled'].include?(value.downcase)
    true
  elsif ['0', 'false'].include?(value.downcase)
    false
  else
    raise ArgumentError.new("#{value} is not a valid boolean for #{name.to_s.upcase}")
  end
end
parse_int(name, value) click to toggle source

Convert a string to an integer. An ArgumentError is raised if the string is not a valid integer.

# File lib/configvar/context.rb, line 117
def parse_int(name, value)
  Integer(value)
rescue ArgumentError
  raise ArgumentError.new("#{value} is not a valid boolean for #{name.to_s.upcase}")
end