Class: Lifer::Config
- Inherits:
-
Object
- Object
- Lifer::Config
- Defined in:
- lib/lifer/config.rb
Overview
This class is responsible for reading the Lifer configuration YAML file. This file should provided by the user, but the Lifer Ruby gem does provide a default file, as well.
Constant Summary collapse
- CONFIG_ENVIRONMENTS =
Some settings may take variants based on the current Lifer environment. The environments with variant configurations include “build” (for static builds, or: production mode) and “serve” (for development mode).
[:build, :serve]
- GLOBAL_SETTINGS =
The “global” section of the config file is the one explicitly special part of the config. It’s used to provide information Lifer needs to keep track of across the entire pre-build and build process.
[ {build: CONFIG_ENVIRONMENTS}, :host, :output_directory, {prebuild: CONFIG_ENVIRONMENTS} ]
- DEFAULT_CONFIG_FILE =
The Lifer Ruby gem provides a default configuration file as a template.
"%s/lib/lifer/templates/config.yaml" % Lifer.gem_root
- DEFAULT_LAYOUT_FILE =
The Lifer Ruby gem provides a default layout file (ERB) as a template.
"%s/lib/lifer/templates/layout.html.erb" % Lifer.gem_root
- DEFAULT_IMPLICIT_SETTINGS =
Provides “implicit settings” that may not be set anywhere but really do require a value.
{ layout_file: DEFAULT_LAYOUT_FILE }
- DEFAULT_REGISTERED_SETTINGS =
A setting must be registered before Lifer will read it and do anything with it. The following settings are registered by default.
(Note that when users add their own custom Ruby classes with custom settings, they must register those settings dynamically. Search this source code for ‘Lifer.register_settings` to see examples of settings being registered.)
[ :author, :description, {entries: [:default_title]}, {feed: [:builder, :uri]}, {global: GLOBAL_SETTINGS}, :language, :layout_file, :selections, :title, :uri_strategy ]
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#registered_settings ⇒ Object
Returns the value of attribute registered_settings.
Class Method Summary collapse
-
.build(file:, root: Lifer.root) ⇒ void
A configuration file must be present in order to bootstrap Lifer.
Instance Method Summary collapse
-
#collectionables ⇒ Array<Symbol>
Provides Lifer with a list of collections as interpreted by reading the configuration YAML file.
-
#register_settings(*settings) ⇒ void
This method allows user scripts and extensions to register arbitrary settings in their configuration YAML files.
-
#setting(*name, collection_name: nil, strict: false) ⇒ String, NilClass
Returns the best in-scope setting value, where the best is the current collection’s setting, then the root collection’s setting, and then Lifer’s default setting.
-
#settings(settings_hash = raw) ⇒ Hash
Provide a nice, readable, registered settings hash.
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
85 86 87 |
# File 'lib/lifer/config.rb', line 85 def file @file end |
#registered_settings ⇒ Object
Returns the value of attribute registered_settings.
84 85 86 |
# File 'lib/lifer/config.rb', line 84 def registered_settings @registered_settings end |
Class Method Details
.build(file:, root: Lifer.root) ⇒ void
This method returns an undefined value.
A configuration file must be present in order to bootstrap Lifer. If a configuration file cannot be found at the given path, then the default configuration file is used.
73 74 75 76 77 78 79 80 81 |
# File 'lib/lifer/config.rb', line 73 def build(file:, root: Lifer.root) if File.file? file new file: file, root: root else Lifer::Message.log("config.no_config_file_at", file:) new file: DEFAULT_CONFIG_FILE, root: root end end |
Instance Method Details
#collectionables ⇒ Array<Symbol>
Provides Lifer with a list of collections as interpreted by reading the configuration YAML file. Collectionables are used to generate collections.
91 92 93 94 |
# File 'lib/lifer/config.rb', line 91 def collectionables @collectionables ||= raw.keys.select { |setting| has_collection_settings? setting } end |
#register_settings(*settings) ⇒ void
This method returns an undefined value.
This method allows user scripts and extensions to register arbitrary settings in their configuration YAML files.
102 103 104 105 106 |
# File 'lib/lifer/config.rb', line 102 def register_settings(*settings) settings.each do |setting| registered_settings << setting end end |
#setting(*name, collection_name: nil, strict: false) ⇒ String, NilClass
Returns the best in-scope setting value, where the best is the current collection’s setting, then the root collection’s setting, and then Lifer’s default setting. If none these are available the method will return ‘nil`.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/lifer/config.rb', line 117 def setting(*name, collection_name: nil, strict: false) name_in_collection = name.dup.unshift(collection_name) if collection_name return if strict && collection_name.nil? return dig_from(settings, *name_in_collection) if (strict && collection_name) candidates = [ dig_from(settings, *name), dig_from(default_settings, *name), dig_from(DEFAULT_IMPLICIT_SETTINGS, *name) ] candidates.unshift dig_from(settings, *name_in_collection) if name_in_collection candidates.detect &:itself end |
#settings(settings_hash = raw) ⇒ Hash
Provide a nice, readable, registered settings hash. If given a subset of settings (like a collection’s settings), it will also provide a hash of registered settings within scope.
139 140 141 142 143 144 145 146 147 |
# File 'lib/lifer/config.rb', line 139 def settings(settings_hash = raw) settings_hash.select { |setting, value| value = settings(value) if value.is_a?(Hash) next unless registered_setting?(setting) [setting, value] }.compact.to_h end |