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 folder that the configuration was loaded from.
-
#destination_path ⇒ String
readonly
The full path to the destination folder.
-
#layouts_path ⇒ String
readonly
The full path to the layouts 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.
-
#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.
65 66 67 68 69 |
# File 'lib/usmu/configuration.rb', line 65 def initialize(hash, config_path) @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.
12 13 14 |
# File 'lib/usmu/configuration.rb', line 12 def config_dir @config_dir end |
#config_file ⇒ String (readonly)
Returns the folder that the configuration was loaded from.
9 10 11 |
# File 'lib/usmu/configuration.rb', line 9 def config_file @config_file end |
#destination_path ⇒ String (readonly)
Returns the full path to the destination folder.
36 37 38 |
# File 'lib/usmu/configuration.rb', line 36 def destination_path get_path @config['destination'] || 'site' end |
#layouts_path ⇒ String (readonly)
Returns the full path to the layouts folder.
42 43 44 |
# File 'lib/usmu/configuration.rb', line 42 def layouts_path get_path @config['layouts'] || 'layouts' end |
#source_path ⇒ String (readonly)
Returns the full path to the source folder.
30 31 32 |
# File 'lib/usmu/configuration.rb', line 30 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.
17 18 19 |
# File 'lib/usmu/configuration.rb', line 17 def self.from_file(filename) from_hash(YAML.load_file(filename), filename) end |
.from_hash(hash, config_path = nil) ⇒ Usmu::Configuration
Load a configuration from a hash.
24 25 26 |
# File 'lib/usmu/configuration.rb', line 24 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.
55 56 57 |
# File 'lib/usmu/configuration.rb', line 55 def [](index) @config[index] 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.
75 76 77 78 79 80 81 |
# File 'lib/usmu/configuration.rb', line 75 def get_path(path) if @config_dir.nil? path else File.join(@config_dir, path) end end |