Class: Usmu::Configuration
- Inherits:
-
Object
- Object
- Usmu::Configuration
- Defined in:
- lib/usmu/configuration.rb
Overview
This class is used to represent a configuration file. This file should be a YAML file and called usmu.yml
by default.
Instance Attribute Summary collapse
-
#config_dir ⇒ String
readonly
The folder that the configuration was loaded from.
-
#config_file ⇒ String
readonly
The name of the file used to load the configuration.
-
#destination_path ⇒ String
readonly
The full path to the destination folder.
-
#layouts_files ⇒ Array<String>
readonly
A list of renderable files in the layouts folder.
-
#layouts_path ⇒ String
readonly
The full path to the layouts folder.
-
#source_files ⇒ Array<String>
readonly
A list of renderable files in the source folder.
-
#source_path ⇒ String
readonly
The full path to the source folder.
Class Method Summary collapse
-
.from_file(filename) ⇒ Usmu::Configuration
Load a configuration from a YAML file on disk.
-
.from_hash(hash, config_path = nil) ⇒ Usmu::Configuration
Load a configuration from a hash.
Instance Method Summary collapse
-
#[](index) ⇒ Array, ...
An index accessor to directly access the configuration file.
-
#excluded?(filename) ⇒ Boolean
private
Helper to determine if a filename is excluded according to the exclude configuration parameter.
-
#get_files(directory) ⇒ Array<Usmu::Layout>, Array<Usmu::StaticFile>
private
Helper function to search a directory recursively and return a list of files that are renderable.
-
#get_path(path) ⇒ String
private
Helper function to transform a relative path in the configuration file to a relative path from the current working directory.
-
#initialize(hash, config_path) ⇒ Configuration
constructor
private
This class has a private constructor.
Constructor Details
#initialize(hash, config_path) ⇒ Configuration (private)
This class has a private constructor.
80 81 82 83 84 85 |
# File 'lib/usmu/configuration.rb', line 80 def initialize(hash, config_path) @log = Logging.logger[self] @config = hash @config_file = config_path @config_dir = config_path ? File.dirname(config_path) : nil end |
Instance Attribute Details
#config_dir ⇒ String (readonly)
Returns the folder that the configuration was loaded from.
14 15 16 |
# File 'lib/usmu/configuration.rb', line 14 def config_dir @config_dir end |
#config_file ⇒ String (readonly)
Returns the name of the file used to load the configuration.
11 12 13 |
# File 'lib/usmu/configuration.rb', line 11 def config_file @config_file end |
#destination_path ⇒ String (readonly)
Returns the full path to the destination folder.
45 46 47 |
# File 'lib/usmu/configuration.rb', line 45 def destination_path get_path @config['destination'] || 'site' end |
#layouts_files ⇒ Array<String> (readonly)
Returns a list of renderable files in the layouts folder.
57 58 59 |
# File 'lib/usmu/configuration.rb', line 57 def layouts_files get_files layouts_path end |
#layouts_path ⇒ String (readonly)
Returns the full path to the layouts folder.
51 52 53 |
# File 'lib/usmu/configuration.rb', line 51 def layouts_path get_path @config['layouts'] || 'layouts' end |
#source_files ⇒ Array<String> (readonly)
Returns a list of renderable files in the source folder.
39 40 41 |
# File 'lib/usmu/configuration.rb', line 39 def source_files get_files source_path end |
#source_path ⇒ String (readonly)
Returns the full path to the source folder.
33 34 35 |
# File 'lib/usmu/configuration.rb', line 33 def source_path get_path @config['source'] || 'src' end |
Class Method Details
.from_file(filename) ⇒ Usmu::Configuration
Load a configuration from a YAML file on disk.
19 20 21 22 |
# File 'lib/usmu/configuration.rb', line 19 def self.from_file(filename) @log.debug("Loading configuration from #{filename}") from_hash(YAML.load_file(filename), filename) end |
.from_hash(hash, config_path = nil) ⇒ Usmu::Configuration
Load a configuration from a hash.
27 28 29 |
# File 'lib/usmu/configuration.rb', line 27 def self.from_hash(hash, config_path = nil) self.new(hash, config_path) end |
Instance Method Details
#[](index) ⇒ Array, ...
An index accessor to directly access the configuration file. It should be noted that ['source']
and
#source_path
and other similar pairs will have different values. ['source']
is the raw value from the
configuration file while the latter is a path on the system, potentially altered by the path from the current
working directory to the configuration file and other factors. The accessor functions such as #source_path
should be preferred for most usages.
70 71 72 |
# File 'lib/usmu/configuration.rb', line 70 def [](index) @config[index] end |
#excluded?(filename) ⇒ Boolean (private)
Helper to determine if a filename is excluded according to the exclude configuration parameter.
102 103 104 105 106 107 108 109 |
# File 'lib/usmu/configuration.rb', line 102 def excluded?(filename) flags = defined?(File::FNM_EXTGLOB) ? File::FNM_EXTGLOB | File::FNM_PATHNAME : File::FNM_PATHNAME (@config['exclude'] || []).each do |f| f += '**/*' if f[-1] == '/' return true if File.fnmatch(f, filename, flags) end false end |
#get_files(directory) ⇒ Array<Usmu::Layout>, Array<Usmu::StaticFile> (private)
Helper function to search a directory recursively and return a list of files that are renderable.
116 117 118 119 120 |
# File 'lib/usmu/configuration.rb', line 116 def get_files(directory) Dir["#{directory}/**/*"].select {|f| !f.match(/\.meta.yml$/) }.map do |f| f[(directory.length + 1)..f.length] end.select {|f| not excluded? f} end |
#get_path(path) ⇒ String (private)
Helper function to transform a relative path in the configuration file to a relative path from the current working directory.
91 92 93 94 95 96 97 |
# File 'lib/usmu/configuration.rb', line 91 def get_path(path) if @config_dir.nil? path else File.join(@config_dir, path) end end |