class Figgy::Configuration
Attributes
Whether to reload a configuration file each time it is accessed
Whether to freeze all loaded objects. Useful in production environments.
The list of defined overlays
Whether to load all configuration files upon creation @note This does not prevent :always_reload
from working.
The directories in which to search for configuration files
Public Class Methods
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
# File lib/figgy/configuration.rb, line 49 def add_root(path) @roots.unshift File.expand_path(path) end
@see always_reload=
# File lib/figgy/configuration.rb, line 54 def always_reload? !!@always_reload end
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
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
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
@return [Array<String>] the list of recognized extensions
# File lib/figgy/configuration.rb, line 112 def extensions @handlers.map { |ext, handler| ext } end
@see freeze=
# File lib/figgy/configuration.rb, line 64 def freeze? !!@freeze end
@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
@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
@see preload=
# File lib/figgy/configuration.rb, line 59 def preload? !!@preload end
# File lib/figgy/configuration.rb, line 45 def root=(path) @roots = [File.expand_path(path)] end
Private Instance Methods
# 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
# File lib/figgy/configuration.rb, line 130 def overlay_values @overlays.map(&:last) end