Class: Nanoc3::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc3/base/item.rb

Overview

Represents a compileable item in a site. It has content and attributes, as well as an identifier (which starts and ends with a slash). It can also store the modification time to speed up compilation.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Item) initialize(raw_content_or_raw_filename, attributes, identifier, params_or_mtime = nil)

Creates a new item with the given content or filename, attributes and identifier.

Note that the API in 3.1 has changed a bit since 3.0; the API remains backwards compatible, however. Passing the modification time as the 4th parameter is deprecated; pass it as the :mtime method option instead.

(if it is a textual item) or the path to the filename containing the content (if it is a binary item).

or the time when this item was last modified (deprecated).

was last modified

item is binary

Parameters:

  • raw_content_or_raw_filename (String)

    The uncompiled item content

  • attributes (Hash)

    A hash containing this item's attributes.

  • identifier (String)

    This item's identifier.

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

    Extra parameters for the item,

Options Hash (params_or_mtime):

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

    The time when this item

  • :binary (Symbol, nil) — default: true

    Whether or not this



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/nanoc3/base/item.rb', line 78

def initialize(raw_content_or_raw_filename, attributes, identifier, params_or_mtime=nil)
  # Get params and mtime
  # TODO [in nanoc 4.0] clean this up
  if params_or_mtime.nil? || params_or_mtime.is_a?(Time)
    params = {}
    @mtime = params_or_mtime
  elsif params_or_mtime.is_a?(Hash)
    params = params_or_mtime
    @mtime = params[:mtime]
  end

  # Get type and raw content or raw filename
  @is_binary = params.has_key?(:binary) ? params[:binary] : false
  if @is_binary
    @raw_filename = raw_content_or_raw_filename
  else
    @raw_content  = raw_content_or_raw_filename
  end

  # Get rest of params
  @attributes   = attributes.symbolize_keys
  @identifier   = identifier.cleaned_identifier.freeze

  @parent       = nil
  @children     = []

  @reps         = []
end

Instance Attribute Details

- (Hash) attributes

This item's attributes

Returns:

  • (Hash)

    This item's attributes



14
15
16
# File 'lib/nanoc3/base/item.rb', line 14

def attributes
  @attributes
end

- (Array<Nanoc3::Item>) children

The child items of this item

Returns:



48
49
50
# File 'lib/nanoc3/base/item.rb', line 48

def children
  @children
end

- (String) identifier

A string that uniquely identifies an item in a site.

Identifiers start and end with a slash. They are comparable to paths on the filesystem, with the difference that file system paths usually do not have a trailing slash. The item hierarchy (parent and children of items) is determined by the item identifier.

The root page (the home page) has the identifier “/”, which means that it is the ancestor of all other items.

Returns:

  • (String)

    This item's identifier



27
28
29
# File 'lib/nanoc3/base/item.rb', line 27

def identifier
  @identifier
end

- (Time) mtime (readonly)

The time when this item was last modified

Returns:

  • (Time)

    The time when this item was last modified



30
31
32
# File 'lib/nanoc3/base/item.rb', line 30

def mtime
  @mtime
end

- (Boolean) outdated_due_to_dependencies Also known as: outdated_due_to_dependencies?

dependencies are outdated

Returns:

  • (Boolean)

    Whether or not this item is outdated because of its



52
53
54
# File 'lib/nanoc3/base/item.rb', line 52

def outdated_due_to_dependencies
  @outdated_due_to_dependencies
end

- (Nanoc3::Item?) parent

nil even for non-root items.

Returns:

  • (Nanoc3::Item, nil)

    The parent item of this item. This can be



45
46
47
# File 'lib/nanoc3/base/item.rb', line 45

def parent
  @parent
end

- (String) raw_content (readonly)

available for textual items)

Returns:

  • (String)

    This item's raw, uncompiled content of this item (only



37
38
39
# File 'lib/nanoc3/base/item.rb', line 37

def raw_content
  @raw_content
end

- (String) raw_filename (readonly)

item’s content (only available for binary items)

Returns:

  • (String)

    The filename pointing to the file containing this



41
42
43
# File 'lib/nanoc3/base/item.rb', line 41

def raw_filename
  @raw_filename
end

- (Array<Nanoc3::ItemRep>) reps (readonly)

This item’s list of item reps

Returns:



33
34
35
# File 'lib/nanoc3/base/item.rb', line 33

def reps
  @reps
end

- (Nanoc3::Site) site

The site this item belongs to

Returns:



11
12
13
# File 'lib/nanoc3/base/item.rb', line 11

def site
  @site
end

Instance Method Details

- (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



175
176
177
178
179
180
# File 'lib/nanoc3/base/item.rb', line 175

def [](key)
  Nanoc3::NotificationCenter.post(:visit_started, self)
  Nanoc3::NotificationCenter.post(:visit_ended,   self)

  @attributes[key]
end

- (Object) []=(key, value)

Sets the attribute with the given key to the given value.

Parameters:

  • key (Symbol)

    The name of the attribute to set

  • value (Object)

    The value of the attribute to set



187
188
189
# File 'lib/nanoc3/base/item.rb', line 187

def []=(key, value)
  @attributes[key] = value
end

- (Boolean) binary?

True if the item is binary; false if it is not

Returns:

  • (Boolean)

    True if the item is binary; false if it is not



192
193
194
# File 'lib/nanoc3/base/item.rb', line 192

def binary?
  !!@is_binary
end

- (String) compiled_content(params = {})

Returns the compiled content from a given representation and a given snapshot. This is a convenience method that makes fetching compiled content easier.

from which the compiled content should be fetched. By default, the compiled content will be fetched from the default representation.

fetch the compiled content. By default, the returned compiled content will be the content compiled right before the first layout call (if any).

rep if no rep is specified) at the given snapshot (or the default snapshot if no snapshot is specified)

Parameters:

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

    a customizable set of options

Options Hash (params):

  • :rep (String) — default: :default

    The name of the representation

  • :snapshot (String)

    The name of the snapshot from which to

Returns:

  • (String)

    The compiled content of the given rep (or the default

See Also:



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/nanoc3/base/item.rb', line 134

def compiled_content(params={})
  # Get rep
  rep_name = params[:rep] || :default
  rep = reps.find { |r| r.name == rep_name }
  if rep.nil?
    raise Nanoc3::Errors::Generic,
      "No rep named #{rep_name.inspect} was found."
  end

  # Get rep's content
  rep.compiled_content(params)
end

- (Object) inspect



204
205
206
# File 'lib/nanoc3/base/item.rb', line 204

def inspect
  "<#{self.class}:0x#{self.object_id.to_s(16)} identifier=#{self.identifier} binary?=#{self.binary?}>"
end

- (Boolean) outdated?

Determines whether this item (or rather, its reps) is outdated and should be recompiled (or rather, its reps should be recompiled).

Returns:

  • (Boolean)

    true if any reps are outdated; false otherwise.



200
201
202
# File 'lib/nanoc3/base/item.rb', line 200

def outdated?
  @reps.any? { |r| r.outdated? }
end

- (String) path(params = {})

Returns the path from a given representation. This is a convenience method that makes fetching the path of a rep easier.

from which the path should be fetched. By default, the path will be fetched from the default representation.

rep is specified)

Parameters:

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

    a customizable set of options

Options Hash (params):

  • :rep (String) — default: :default

    The name of the representation

Returns:

  • (String)

    The path of the given rep ( or the default rep if no



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/nanoc3/base/item.rb', line 156

def path(params={})
  rep_name = params[:rep] || :default

  # Get rep
  rep = reps.find { |r| r.name == rep_name }
  if rep.nil?
    raise Nanoc3::Errors::Generic,
      "No rep named #{rep_name.inspect} was found."
  end

  # Get rep's path
  rep.path
end

- (Nanoc3::ItemRep) rep_named(rep_name)

Returns the rep with the given name.

Parameters:

  • rep_name (Symbol)

    The name of the representation to return

Returns:



112
113
114
# File 'lib/nanoc3/base/item.rb', line 112

def rep_named(rep_name)
  @reps.find { |r| r.name == rep_name }
end