class Figgy::Configuration

Attributes

always_reload[RW]

Whether to reload a configuration file each time it is accessed

freeze[RW]

Whether to freeze all loaded objects. Useful in production environments.

overlays[R]

The list of defined overlays

preload[RW]

Whether to load all configuration files upon creation @note This does not prevent :always_reload from working.

roots[R]

The directories in which to search for configuration files

Public Class Methods

new() click to toggle source

Constructs a new {Figgy::Configuration Figgy::Configuration} instance.

By default, uses a root of the current directory, and defines handlers for .yml, .yaml, .yml.erb, .yaml.erb, and .json.

# File lib/figgy/configuration.rb, line 23
def initialize
  @roots    = [Dir.pwd]
  @handlers = []
  @overlays = []
  @always_reload = false
  @preload = false
  @freeze = false

  define_handler 'yml', 'yaml' do |contents|
    YAML.load(contents)
  end

  define_handler 'yml.erb', 'yaml.erb' do |contents|
    erb = ERB.new(contents).result
    YAML.load(erb)
  end

  define_handler 'json' do |contents|
    JSON.parse(contents)
  end
end

Public Instance Methods

add_root(path) click to toggle source
# File lib/figgy/configuration.rb, line 49
def add_root(path)
  @roots.unshift File.expand_path(path)
end
always_reload?() click to toggle source

@see always_reload=

# File lib/figgy/configuration.rb, line 54
def always_reload?
  !!@always_reload
end
define_combined_overlay(*names) click to toggle source

Adds an overlay using the combined values of other overlays.

@example Searches for files in 'production_US'

config.define_overlay :environment, 'production'
config.define_overlay :country, 'US'
config.define_combined_overlay :environment, :country
# File lib/figgy/configuration.rb, line 87
def define_combined_overlay(*names)
  combined_name = names.join("_").to_sym
  value = names.map { |name| overlay_value(name) }.join("_")
  @overlays << [combined_name, value]
end
define_handler(*extensions, &block) click to toggle source

Adds a new handler for files with any extension in extensions.

@example Adding an XML handler

config.define_handler 'xml' do |body|
  Hash.from_xml(body)
end
# File lib/figgy/configuration.rb, line 107
def define_handler(*extensions, &block)
  @handlers += extensions.map { |ext| [ext, block] }
end
define_overlay(name, value = nil) { || ... } click to toggle source

Adds an overlay named name, found at value.

If a block is given, yields to the block to determine value.

@param name an internal name for the overlay @param value the value of the overlay @example An environment overlay

config.define_overlay(:environment) { Rails.env }
# File lib/figgy/configuration.rb, line 76
def define_overlay(name, value = nil)
  value = yield if block_given?
  @overlays << [name, value]
end
extensions() click to toggle source

@return [Array<String>] the list of recognized extensions

# File lib/figgy/configuration.rb, line 112
def extensions
  @handlers.map { |ext, handler| ext }
end
freeze?() click to toggle source

@see freeze=

# File lib/figgy/configuration.rb, line 64
def freeze?
  !!@freeze
end
handler_for(filename) click to toggle source

@return [Proc] the handler for a given filename

# File lib/figgy/configuration.rb, line 117
def handler_for(filename)
  match = @handlers.find { |ext, handler| filename =~ /\.#{ext}$/ }
  match && match.last
end
overlay_dirs() click to toggle source

@return [Array<String>] the list of directories to search for config files

# File lib/figgy/configuration.rb, line 94
def overlay_dirs
  return @roots if @overlays.empty?
  overlay_values.map { |overlay|
    @roots.map { |root| overlay ? File.join(root, overlay) : root }
  }.flatten.uniq
end
preload?() click to toggle source

@see preload=

# File lib/figgy/configuration.rb, line 59
def preload?
  !!@preload
end
root=(path) click to toggle source
# File lib/figgy/configuration.rb, line 45
def root=(path)
  @roots = [File.expand_path(path)]
end

Private Instance Methods

overlay_value(name) click to toggle source
# File lib/figgy/configuration.rb, line 124
def overlay_value(name)
  overlay = @overlays.find { |n, v| name == n }
  raise "No such overlay: #{name.inspect}" unless overlay
  overlay.last
end
overlay_values() click to toggle source
# File lib/figgy/configuration.rb, line 130
def overlay_values
  @overlays.map(&:last)
end