Class: Usmu::Template::StaticFile

Inherits:
Object
  • Object
show all
Includes:
Helpers::Indexer
Defined in:
lib/usmu/template/static_file.rb

Overview

Represents a static file which should be transferred to the destination unchanged. This also acts as the base class for all layouts and page types. The basic interface defined here is used to process all types of files.

Direct Known Subclasses

GeneratedFile, Layout

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Indexer

included

Constructor Details

#initialize(configuration, name, metadata, type = nil, content = nil) ⇒ StaticFile

Returns a new instance of StaticFile.

Parameters:

  • configuration (Usmu::Configuration)

    The configuration for the website we're generating.

  • name (String)

    The name of the file in the source directory.

  • metadata (Hash)

    The metadata for the file.

  • type (String) (defaults to: nil)

    The type of template to use with the file. Not used for StaticFile. Used for testing purposes.

  • content (String) (defaults to: nil)

    The content of the file. Used for testing purposes.



22
23
24
25
26
27
28
29
30
31
# File 'lib/usmu/template/static_file.rb', line 22

def initialize(configuration, name, , type = nil, content = nil)
  @log = Logging.logger[self]
  @log.debug("Creating <##{self.class.name} @name=\"#{name}\">")

  @configuration = configuration
  @name = name
  @metadata = 
  @type = type
  @content = content
end

Instance Attribute Details

#input_pathString (readonly)

Returns the full path to the file in the source directory.

Returns:

  • (String)

    the full path to the file in the source directory



43
44
45
# File 'lib/usmu/template/static_file.rb', line 43

def input_path
  File.join(@configuration.source_path, @name)
end

#mtimeFixnum (readonly)

Returns The modification time of this file.

Returns:

  • (Fixnum)

    The modification time of this file.



61
62
63
# File 'lib/usmu/template/static_file.rb', line 61

def mtime
  input_path.nil? ? Time.now.to_i : File.stat(input_path).mtime
end

#nameString (readonly)

Returns the name of the file in the source directory.

Returns:

  • (String)

    the name of the file in the source directory



12
13
14
# File 'lib/usmu/template/static_file.rb', line 12

def name
  @name
end

#output_filenameString (readonly)

Returns the filename to use for the output directory with any modifications to the input filename required.

Returns:

  • (String)

    the filename to use in the output directory.



55
56
57
# File 'lib/usmu/template/static_file.rb', line 55

def output_filename
  @name
end

Instance Method Details

#==(other) ⇒ void



69
70
71
72
73
# File 'lib/usmu/template/static_file.rb', line 69

def ==(other)
  return false unless self.class == other.class

  self.input_path == other.input_path && self.output_filename == other.output_filename
end

#inspectvoid



65
66
67
# File 'lib/usmu/template/static_file.rb', line 65

def inspect
  "\#<#{self.class}:#{'0x%08x' % __id__} #{@name} => #{output_filename}>"
end

#metadatavoid



47
48
49
# File 'lib/usmu/template/static_file.rb', line 47

def 
  {}
end

#render(variables = {}) ⇒ String

Renders the file with any templating language required and returns the result

Parameters:

  • variables (Hash) (defaults to: {})

    Variables to be used in the template.

Returns:

  • (String)

    The rendered file



37
38
39
# File 'lib/usmu/template/static_file.rb', line 37

def render(variables = {})
  @content || File.read(input_path, mode: 'rb')
end