Class: Lifer::Config

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



85
86
87
# File 'lib/lifer/config.rb', line 85

def file
  @file
end

#registered_settingsObject

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.

Parameters:

  • file (String)

    The path to the user-provided configuration file.



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

#collectionablesArray<Symbol>

Provides Lifer with a list of collections as interpreted by reading the configuration YAML file. Collectionables are used to generate collections.

Returns:

  • (Array<Symbol>)

    A list of non-root collection names.



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.

Parameters:

  • settings (*Symbol, *Hash)

    A list of symbols and/or hashs to be added to Lifer’s registered settings.



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`.

Parameters:

  • name (Symbol)

    The configuration setting.

  • collection_name (Symbol) (defaults to: nil)

    A collection name.

  • strict (boolean) (defaults to: false)

    Strictly return the collection setting without falling back to higher-level settings.

Returns:

  • (String, NilClass)

    The value of the best in-scope setting.



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.

Parameters:

  • settings_hash (Hash) (defaults to: raw)

    A hash of settings.

Returns:

  • (Hash)

    A compact hash of registered settings.



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