Class: Usmu::Configuration

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, config_path) ⇒ Configuration (private)

This class has a private constructor.

See Also:



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_dirString (readonly)

Returns the folder that the configuration was loaded from.

Returns:

  • (String)

    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_fileString (readonly)

Returns the folder that the configuration was loaded from.

Returns:

  • (String)

    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_pathString (readonly)

Returns the full path to the destination folder.

Returns:

  • (String)

    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_pathString (readonly)

Returns the full path to the layouts folder.

Returns:

  • (String)

    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_pathString (readonly)

Returns the full path to the source folder.

Returns:

  • (String)

    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.

Returns:



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.

Returns:



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.

Parameters:

  • index (String, Symbol)

    The index to return.

Returns:

  • (Array, Hash, String, Symbol)

    Returns a value from the hash loaded from YAML. The type of value will ultimately depend on the configuration file and the index provided.



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.

Returns:

  • (String)


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