Class: Figgy::Configuration
- Inherits:
-
Object
- Object
- Figgy::Configuration
- Defined in:
- lib/figgy/configuration.rb
Instance Attribute Summary collapse
-
#always_reload ⇒ Object
Whether to reload a configuration file each time it is accessed.
-
#freeze ⇒ Object
Whether to freeze all loaded objects.
-
#overlays ⇒ Object
readonly
The list of defined overlays.
-
#preload ⇒ Object
Whether to load all configuration files upon creation.
-
#roots ⇒ Object
readonly
The directories in which to search for configuration files.
Instance Method Summary collapse
- #add_root(path) ⇒ Object
- #always_reload? ⇒ Boolean
-
#define_combined_overlay(*names) ⇒ Object
Adds an overlay using the combined values of other overlays.
-
#define_handler(*extensions, &block) ⇒ Object
Adds a new handler for files with any extension in
extensions. -
#define_overlay(name, value = nil) ⇒ Object
Adds an overlay named
name, found atvalue. -
#extensions ⇒ Array<String>
The list of recognized extensions.
- #freeze? ⇒ Boolean
-
#handler_for(filename) ⇒ Proc
The handler for a given filename.
-
#initialize ⇒ Configuration
constructor
Constructs a new Figgy::Configuration instance.
-
#overlay_dirs ⇒ Array<String>
The list of directories to search for config files.
- #preload? ⇒ Boolean
- #root=(path) ⇒ Object
Constructor Details
#initialize ⇒ Configuration
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_reload ⇒ Object
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 |
#freeze ⇒ Object
Whether to freeze all loaded objects. Useful in production environments.
17 18 19 |
# File 'lib/figgy/configuration.rb', line 17 def freeze @freeze end |
#overlays ⇒ Object (readonly)
The list of defined overlays
7 8 9 |
# File 'lib/figgy/configuration.rb', line 7 def @overlays end |
#preload ⇒ Object
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 |
#roots ⇒ Object (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.(path) end |
#always_reload? ⇒ Boolean
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.
87 88 89 90 91 |
# File 'lib/figgy/configuration.rb', line 87 def (*names) combined_name = names.join("_").to_sym value = names.map { |name| (name) }.join("_") @overlays << [combined_name, value] end |
#define_handler(*extensions, &block) ⇒ Object
Adds a new handler for files with any extension in extensions.
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.
76 77 78 79 |
# File 'lib/figgy/configuration.rb', line 76 def (name, value = nil) value = yield if block_given? @overlays << [name, value] end |
#extensions ⇒ Array<String>
Returns 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
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.
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_dirs ⇒ Array<String>
Returns the list of directories to search for config files.
94 95 96 97 98 99 |
# File 'lib/figgy/configuration.rb', line 94 def return @roots if @overlays.empty? .map { || @roots.map { |root| ? File.join(root, ) : root } }.flatten.uniq end |
#preload? ⇒ Boolean
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.(path)] end |