Class: Usmu::Configuration

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

Methods included from Helpers::Indexer

included

Constructor Details

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

This class has a private constructor.

See Also:



117
118
119
120
121
122
123
124
# File 'lib/usmu/configuration.rb', line 117

def initialize(hash, config_path)
  @log = Logging.logger[self]
  @config = hash
  @config_file = config_path
  @config_dir = if config_path
                  File.dirname(config_path)
                end
end

Instance Attribute Details

#configvoid (readonly, private)

Returns the value of attribute config.



111
112
113
# File 'lib/usmu/configuration.rb', line 111

def config
  @config
end

#config_dirString (readonly)

Returns the folder that the configuration was loaded from.

Returns:

  • (String)

    the folder that the configuration was loaded from.



20
21
22
# File 'lib/usmu/configuration.rb', line 20

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.



17
18
19
# File 'lib/usmu/configuration.rb', line 17

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



58
59
60
# File 'lib/usmu/configuration.rb', line 58

def destination_path
  get_path self['destination'] || 'site'
end

#includes_metadataMetadataService (readonly)

Returns a metadata service for retrieving metadata about includes.

Returns:

  • (MetadataService)

    a metadata service for retrieving metadata about includes



92
93
94
# File 'lib/usmu/configuration.rb', line 92

def 
  @includes_metadata ||= MetadataService.new(includes_path)
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



70
71
72
# File 'lib/usmu/configuration.rb', line 70

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



64
65
66
# File 'lib/usmu/configuration.rb', line 64

def layouts_path
  get_path self['layouts'] || 'layouts'
end

#logvoid (readonly, private)

Returns the value of attribute log.



110
111
112
# File 'lib/usmu/configuration.rb', line 110

def log
  @log
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



46
47
48
# File 'lib/usmu/configuration.rb', line 46

def source_files
  get_files source_path
end

#source_metadataMetadataService (readonly)

Returns a metadata service for retrieving metadata about pages in #source_path.

Returns:

  • (MetadataService)

    a metadata service for retrieving metadata about pages in #source_path



52
53
54
# File 'lib/usmu/configuration.rb', line 52

def 
  @source_metadata ||= MetadataService.new(source_path)
end

#source_pathString (readonly)

Returns the full path to the source folder.

Returns:

  • (String)

    the full path to the source folder



40
41
42
# File 'lib/usmu/configuration.rb', line 40

def source_path
  get_path self['source'] || 'src'
end

Class Method Details

.from_file(filename) ⇒ Usmu::Configuration

Load a configuration from a YAML file on disk.

Returns:

Raises:

  • (ArgumentError)


25
26
27
28
29
# File 'lib/usmu/configuration.rb', line 25

def self.from_file(filename)
  raise ArgumentError, "File not found: #{filename}" if filename.nil? || (not File.exist? filename)
  @log.debug("Loading configuration from #{filename}")
  new(YAML.load_file(filename), filename)
end

.from_hash(hash, config_path = nil) ⇒ Usmu::Configuration

Load a configuration from a hash.

Returns:



34
35
36
# File 'lib/usmu/configuration.rb', line 34

def self.from_hash(hash, config_path = nil)
  new(hash, config_path)
end

Instance Method Details

#excludeArray

Returns an array of exclusions

Returns:

  • (Array)


99
100
101
# File 'lib/usmu/configuration.rb', line 99

def exclude
  self['exclude'] || []
end

#excluded?(filename) ⇒ Boolean (private)

Helper to determine if a filename is excluded according to the exclude configuration parameter.

Returns:

  • (Boolean)


141
142
143
144
145
146
147
# File 'lib/usmu/configuration.rb', line 141

def excluded?(filename)
  exclude.each do |f|
    f += '**/*' if f.end_with? '/'
    return true if File.fnmatch(f, filename, File::FNM_EXTGLOB | File::FNM_PATHNAME)
  end
  false
end

#generatorUsmu::SiteGenerator

Returns:



104
105
106
# File 'lib/usmu/configuration.rb', line 104

def generator
  SiteGenerator.new(self)
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

Returns:

  • (Array<Usmu::Layout>, Array<Usmu::StaticFile>)

    Either an array of Layouts or StaticFiles in the directory



153
154
155
156
157
158
159
# File 'lib/usmu/configuration.rb', line 153

def get_files(directory)
  Dir["#{directory}/**/{*,.??*}"].
      select {|f| not File.directory? f }.
      select {|f| !f.match(/[\.\/]meta\.yml$/) }.
      map {|f| f[directory.length + 1, f.length] }.
      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)


130
131
132
133
134
135
136
# File 'lib/usmu/configuration.rb', line 130

def get_path(path)
  if @config_dir.nil?
    path
  else
    File.join(@config_dir, path)
  end
end

#includes_filesArray<String>

Returns a list of renderable files in the layouts folder.

Returns:

  • (Array<String>)

    a list of renderable files in the layouts folder



86
87
88
# File 'lib/usmu/configuration.rb', line 86

def includes_files
  get_files includes_path
end

#includes_pathString

Returns the full path to the layouts folder.

Returns:

  • (String)

    the full path to the layouts folder



80
81
82
# File 'lib/usmu/configuration.rb', line 80

def includes_path
  get_path self['includes'] || 'includes'
end

#layouts_metadatavoid



74
75
76
# File 'lib/usmu/configuration.rb', line 74

def 
  @layouts_metadata ||= MetadataService.new(layouts_path)
end