Class: Nanoc::Layout

Inherits:
Object
  • Object
show all
Extended by:
Memoization
Defined in:
lib/nanoc/base/source_data/layout.rb

Overview

Represents a layout in a nanoc site. It has content, attributes, an identifier and a modification time (to speed up compilation).

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Memoization

memoize

Constructor Details

- (Layout) initialize(raw_content, attributes, identifier, params = nil)

Creates a new layout.

Parameters:

  • raw_content (String)

    The raw content of this layout.

  • attributes (Hash)

    A hash containing this layout’s attributes.

  • identifier (String)

    This layout’s identifier.

  • params (Time, Hash) (defaults to: nil)

    Extra parameters. For backwards compatibility, this can be a Time instance indicating the time when this layout was last modified (mtime).

Options Hash (params):

  • :mtime (Time, nil) — default: nil

    The time when this layout was last modified. Deprecated; pass the modification time as the :mtime attribute instead.



34
35
36
37
38
39
40
41
42
43
# File 'lib/nanoc/base/source_data/layout.rb', line 34

def initialize(raw_content, attributes, identifier, params = nil)
  @raw_content  = raw_content
  @attributes   = attributes.symbolize_keys_recursively
  @identifier   = identifier.cleaned_identifier.freeze

  # Set mtime
  params ||= {}
  params = { mtime: params } if params.is_a?(Time)
  @attributes.merge(mtime: params[:mtime]) if params[:mtime]
end

Instance Attribute Details

- (Hash) attributes (readonly)

Returns This layout’s attributes

Returns:

  • (Hash)

    This layout’s attributes



13
14
15
# File 'lib/nanoc/base/source_data/layout.rb', line 13

def attributes
  @attributes
end

- (String) identifier

Returns This layout’s identifier, starting and ending with a slash

Returns:

  • (String)

    This layout’s identifier, starting and ending with a slash



17
18
19
# File 'lib/nanoc/base/source_data/layout.rb', line 17

def identifier
  @identifier
end

- (String) raw_content (readonly)

Returns The raw content of this layout

Returns:

  • (String)

    The raw content of this layout



10
11
12
# File 'lib/nanoc/base/source_data/layout.rb', line 10

def raw_content
  @raw_content
end

Instance Method Details

- (Object) ==(other)



101
102
103
# File 'lib/nanoc/base/source_data/layout.rb', line 101

def ==(other)
  self.eql?(other)
end

- (Object) [](key)

Requests the attribute with the given key.

Parameters:

  • key (Symbol)

    The name of the attribute to fetch.

Returns:

  • (Object)

    The value of the requested attribute.



50
51
52
# File 'lib/nanoc/base/source_data/layout.rb', line 50

def [](key)
  @attributes[key]
end

- (String) checksum

Returns The checksum for this object. If its contents change, the checksum will change as well.

Returns:

  • (String)

    The checksum for this object. If its contents change, the checksum will change as well.



88
89
90
# File 'lib/nanoc/base/source_data/layout.rb', line 88

def checksum
  Nanoc::Checksummer.calc(self)
end

- (Boolean) eql?(other)

Returns:

  • (Boolean)


97
98
99
# File 'lib/nanoc/base/source_data/layout.rb', line 97

def eql?(other)
  self.class == other.class && identifier == other.identifier
end

- (void) freeze

This method returns an undefined value.

Prevents all further modifications to the layout.



67
68
69
70
71
# File 'lib/nanoc/base/source_data/layout.rb', line 67

def freeze
  attributes.freeze_recursively
  identifier.freeze
  raw_content.freeze
end

- (Object) hash



93
94
95
# File 'lib/nanoc/base/source_data/layout.rb', line 93

def hash
  self.class.hash ^ identifier.hash
end

- (Object) inspect



82
83
84
# File 'lib/nanoc/base/source_data/layout.rb', line 82

def inspect
  "<#{self.class} identifier=\"#{identifier}\">"
end

- (Object) marshal_dump



105
106
107
108
109
110
111
# File 'lib/nanoc/base/source_data/layout.rb', line 105

def marshal_dump
  [
    @raw_content,
    @attributes,
    @identifier
  ]
end

- (Object) marshal_load(source)



113
114
115
116
117
# File 'lib/nanoc/base/source_data/layout.rb', line 113

def marshal_load(source)
  @raw_content,
  @attributes,
  @identifier = *source
end

- (Object) mtime

Deprecated.

Access the modification time using layout[:mtime] instead.



120
121
122
# File 'lib/nanoc/base/source_data/layout.rb', line 120

def mtime
  self[:mtime]
end

- (Object) reference

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns an object that can be used for uniquely identifying objects.

Returns:

  • (Object)

    An unique reference to this object



78
79
80
# File 'lib/nanoc/base/source_data/layout.rb', line 78

def reference
  [type, identifier]
end

- (Symbol) type

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the type of this object. Will always return :layout, because this is a layout. For items, this method returns :item.

Returns:

  • (Symbol)

    :layout



60
61
62
# File 'lib/nanoc/base/source_data/layout.rb', line 60

def type
  :layout
end