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:



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

Returns the folder that the configuration was loaded from.

Returns:

  • (String)

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

Returns the name of the file used to load the configuration.

Returns:

  • (String)

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

Returns the full path to the destination folder.

Returns:

  • (String)

    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_filesArray<String> (readonly)

Returns a list of renderable files in the layouts folder.

Returns:

  • (Array<String>)

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

Returns the full path to the layouts folder.

Returns:

  • (String)

    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_filesArray<String> (readonly)

Returns a list of renderable files in the source folder.

Returns:

  • (Array<String>)

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

Returns the full path to the source folder.

Returns:

  • (String)

    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.

Returns:



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.

Returns:



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.

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.



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.

Returns:

  • (Boolean)


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.

Parameters:

  • directory (String)

    the directory to search

  • layout (Boolean)

    is this directory a layouts_path

Returns:



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.

Returns:

  • (String)


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