Class: Figgy::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/figgy/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Constructs a new Figgy::Configuration instance.

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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

Instance Attribute Details

#always_reloadObject

Whether to reload a configuration file each time it is accessed



10
11
12
# File 'lib/figgy/configuration.rb', line 10

def always_reload
  @always_reload
end

#freezeObject

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



17
18
19
# File 'lib/figgy/configuration.rb', line 17

def freeze
  @freeze
end

#overlaysObject (readonly)

The list of defined overlays



7
8
9
# File 'lib/figgy/configuration.rb', line 7

def overlays
  @overlays
end

#preloadObject

Note:

This does not prevent :always_reload from working.

Whether to load all configuration files upon creation



14
15
16
# File 'lib/figgy/configuration.rb', line 14

def preload
  @preload
end

#rootsObject (readonly)

The directories in which to search for configuration files



4
5
6
# File 'lib/figgy/configuration.rb', line 4

def roots
  @roots
end

Instance Method Details

#add_root(path) ⇒ Object



49
50
51
# File 'lib/figgy/configuration.rb', line 49

def add_root(path)
  @roots.unshift File.expand_path(path)
end

#always_reload?Boolean

Returns:

  • (Boolean)

See Also:



54
55
56
# File 'lib/figgy/configuration.rb', line 54

def always_reload?
  !!@always_reload
end

#define_combined_overlay(*names) ⇒ Object

Adds an overlay using the combined values of other overlays.

Examples:

Searches for files in ‘production_US’

config.define_overlay :environment, 'production'
config.define_overlay :country, 'US'
config.define_combined_overlay :environment, :country


87
88
89
90
91
# 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) ⇒ Object

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

Examples:

Adding an XML handler

config.define_handler 'xml' do |body|
  Hash.from_xml(body)
end


107
108
109
# File 'lib/figgy/configuration.rb', line 107

def define_handler(*extensions, &block)
  @handlers += extensions.map { |ext| [ext, block] }
end

#define_overlay(name, value = nil) ⇒ Object

Adds an overlay named name, found at value.

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

Examples:

An environment overlay

config.define_overlay(:environment) { Rails.env }

Parameters:

  • name

    an internal name for the overlay

  • value (defaults to: nil)

    the value of the overlay



76
77
78
79
# File 'lib/figgy/configuration.rb', line 76

def define_overlay(name, value = nil)
  value = yield if block_given?
  @overlays << [name, value]
end

#extensionsArray<String>

Returns the list of recognized extensions.

Returns:

  • (Array<String>)

    the list of recognized extensions



112
113
114
# File 'lib/figgy/configuration.rb', line 112

def extensions
  @handlers.map { |ext, handler| ext }
end

#freeze?Boolean

Returns:

  • (Boolean)

See Also:



64
65
66
# File 'lib/figgy/configuration.rb', line 64

def freeze?
  !!@freeze
end

#handler_for(filename) ⇒ Proc

Returns the handler for a given filename.

Returns:

  • (Proc)

    the handler for a given filename



117
118
119
120
# File 'lib/figgy/configuration.rb', line 117

def handler_for(filename)
  match = @handlers.find { |ext, handler| filename =~ /\.#{ext}$/ }
  match && match.last
end

#overlay_dirsArray<String>

Returns the list of directories to search for config files.

Returns:

  • (Array<String>)

    the list of directories to search for config files



94
95
96
97
98
99
# 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?Boolean

Returns:

  • (Boolean)

See Also:



59
60
61
# File 'lib/figgy/configuration.rb', line 59

def preload?
  !!@preload
end

#root=(path) ⇒ Object



45
46
47
# File 'lib/figgy/configuration.rb', line 45

def root=(path)
  @roots = [File.expand_path(path)]
end