Class: Nanoc3::Item
- Inherits:
-
Object
- Object
- Nanoc3::Item
- Extended by:
- Memoization
- Defined in:
- lib/nanoc3/base/source_data/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
-
#attributes ⇒ Hash
This item’s attributes.
-
#children ⇒ Array<Nanoc3::Item>
The child items of this item.
-
#identifier ⇒ String
A string that uniquely identifies an item in a site.
-
#parent ⇒ Nanoc3::Item?
The parent item of this item.
-
#raw_content ⇒ String
readonly
This item’s raw, uncompiled content of this item (only available for textual items).
-
#raw_filename ⇒ String
readonly
The filename pointing to the file containing this item’s content (only available for binary items).
-
#reps ⇒ Array<Nanoc3::ItemRep>
readonly
This item’s list of item reps.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#[](key) ⇒ Object
Requests the attribute with the given key.
-
#[]=(key, value) ⇒ Object
Sets the attribute with the given key to the given value.
-
#binary? ⇒ Boolean
True if the item is binary; false if it is not.
-
#checksum ⇒ String
The checksum for this object.
-
#compiled_content(params = {}) ⇒ String
Returns the compiled content from a given representation and a given snapshot.
- #eql?(other) ⇒ Boolean
-
#freeze ⇒ void
Prevents all further modifications to its attributes.
- #hash ⇒ Object
-
#initialize(raw_content_or_raw_filename, attributes, identifier, params = nil) ⇒ Item
constructor
Creates a new item with the given content or filename, attributes and identifier.
- #inspect ⇒ Object
- #marshal_dump ⇒ Object
- #marshal_load(source) ⇒ Object
-
#mtime ⇒ Object
deprecated
Deprecated.
Access the modification time using ‘item` instead.
-
#path(params = {}) ⇒ String
Returns the path from a given representation.
-
#reference ⇒ Object
private
Returns an object that can be used for uniquely identifying objects.
-
#rep_named(rep_name) ⇒ Nanoc3::ItemRep
Returns the rep with the given name.
-
#type ⇒ Symbol
private
Returns the type of this object.
Methods included from Memoization
Constructor Details
#initialize(raw_content_or_raw_filename, attributes, identifier, params = nil) ⇒ Item
Creates a new item with the given content or filename, attributes and identifier.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/nanoc3/base/source_data/item.rb', line 67 def initialize(raw_content_or_raw_filename, attributes, identifier, params=nil) # Parse params params ||= {} params = { :mtime => params } if params.is_a?(Time) params[:binary] = false unless params.has_key?(:binary) if raw_content_or_raw_filename.nil? raise "attempted to create an item with no content/filename (identifier #{identifier})" end # Get type and raw content or raw filename @is_binary = params[:binary] 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 # Set mtime @attributes.merge!(:mtime => params[:mtime]) if params[:mtime] @parent = nil @children = [] @reps = [] end |
Instance Attribute Details
#attributes ⇒ Hash
Returns This item’s attributes.
13 14 15 |
# File 'lib/nanoc3/base/source_data/item.rb', line 13 def attributes @attributes end |
#children ⇒ Array<Nanoc3::Item>
Returns The child items of this item.
44 45 46 |
# File 'lib/nanoc3/base/source_data/item.rb', line 44 def children @children end |
#identifier ⇒ String
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.
26 27 28 |
# File 'lib/nanoc3/base/source_data/item.rb', line 26 def identifier @identifier end |
#parent ⇒ Nanoc3::Item?
Returns The parent item of this item. This can be nil even for non-root items.
41 42 43 |
# File 'lib/nanoc3/base/source_data/item.rb', line 41 def parent @parent end |
#raw_content ⇒ String (readonly)
Returns This item’s raw, uncompiled content of this item (only available for textual items).
33 34 35 |
# File 'lib/nanoc3/base/source_data/item.rb', line 33 def raw_content @raw_content end |
#raw_filename ⇒ String (readonly)
Returns The filename pointing to the file containing this item’s content (only available for binary items).
37 38 39 |
# File 'lib/nanoc3/base/source_data/item.rb', line 37 def raw_filename @raw_filename end |
#reps ⇒ Array<Nanoc3::ItemRep> (readonly)
Returns This item’s list of item reps.
29 30 31 |
# File 'lib/nanoc3/base/source_data/item.rb', line 29 def reps @reps end |
Instance Method Details
#==(other) ⇒ Object
272 273 274 |
# File 'lib/nanoc3/base/source_data/item.rb', line 272 def ==(other) self.eql?(other) end |
#[](key) ⇒ Object
Requests the attribute with the given key.
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/nanoc3/base/source_data/item.rb', line 166 def [](key) Nanoc3::NotificationCenter.post(:visit_started, self) Nanoc3::NotificationCenter.post(:visit_ended, self) # Get captured content (hax) # TODO [in nanoc 4.0] remove me if key.to_s =~ /^content_for_(.*)$/ @@_content_for_warning_issued ||= false @@_Nanoc3_Helpers_Capturing_included ||= false # Warn unless @@_content_for_warning_issued warn 'WARNING: Accessing captured content should happen using the #content_for method defined in the Capturing helper instead of using item[:content_for_something]. The latter way of accessing captured content will be removed in nanoc 4.0.' @@_content_for_warning_issued = true end # Include capturing helper if necessary unless @@_Nanoc3_Helpers_Capturing_included self.class.send(:include, ::Nanoc3::Helpers::Capturing) @@_Nanoc3_Helpers_Capturing_included = true end # Get content return content_for(self, $1.to_sym) end @attributes[key] end |
#[]=(key, value) ⇒ Object
Sets the attribute with the given key to the given value.
200 201 202 |
# File 'lib/nanoc3/base/source_data/item.rb', line 200 def []=(key, value) @attributes[key] = value end |
#binary? ⇒ Boolean
Returns True if the item is binary; false if it is not.
205 206 207 |
# File 'lib/nanoc3/base/source_data/item.rb', line 205 def binary? !!@is_binary end |
#checksum ⇒ String
Returns The checksum for this object. If its contents change, the checksum will change as well.
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/nanoc3/base/source_data/item.rb', line 245 def checksum content_checksum = if binary? if File.exist?(raw_filename) Pathname.new(raw_filename).checksum else ''.checksum end else @raw_content.checksum end attributes = @attributes.dup attributes.delete(:file) attributes_checksum = attributes.checksum content_checksum + ',' + attributes_checksum end |
#compiled_content(params = {}) ⇒ String
Returns the compiled content from a given representation and a given snapshot. This is a convenience method that makes fetching compiled content easier.
125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/nanoc3/base/source_data/item.rb', line 125 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 |
#eql?(other) ⇒ Boolean
268 269 270 |
# File 'lib/nanoc3/base/source_data/item.rb', line 268 def eql?(other) self.class == other.class && self.identifier == other.identifier end |
#freeze ⇒ void
This method returns an undefined value.
Prevents all further modifications to its attributes.
231 232 233 234 235 236 237 |
# File 'lib/nanoc3/base/source_data/item.rb', line 231 def freeze attributes.freeze_recursively children.freeze identifier.freeze raw_filename.freeze if raw_filename raw_content.freeze if raw_content end |
#hash ⇒ Object
264 265 266 |
# File 'lib/nanoc3/base/source_data/item.rb', line 264 def hash self.class.hash ^ self.identifier.hash end |
#inspect ⇒ Object
239 240 241 |
# File 'lib/nanoc3/base/source_data/item.rb', line 239 def inspect "<#{self.class}:0x#{self.object_id.to_s(16)} identifier=#{self.identifier} binary?=#{self.binary?}>" end |
#marshal_dump ⇒ Object
276 277 278 279 280 281 282 283 284 |
# File 'lib/nanoc3/base/source_data/item.rb', line 276 def marshal_dump [ @is_binary, @raw_filename, @raw_content, @attributes, @identifier ] end |
#marshal_load(source) ⇒ Object
286 287 288 289 290 291 292 |
# File 'lib/nanoc3/base/source_data/item.rb', line 286 def marshal_load(source) @is_binary, @raw_filename, @raw_content, @attributes, @identifier = *source end |
#mtime ⇒ Object
Access the modification time using ‘item` instead.
295 296 297 |
# File 'lib/nanoc3/base/source_data/item.rb', line 295 def mtime self[:mtime] end |
#path(params = {}) ⇒ String
Returns the path from a given representation. This is a convenience method that makes fetching the path of a rep easier.
147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/nanoc3/base/source_data/item.rb', line 147 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 |
#reference ⇒ Object
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.
224 225 226 |
# File 'lib/nanoc3/base/source_data/item.rb', line 224 def reference [ type, self.identifier ] end |
#rep_named(rep_name) ⇒ Nanoc3::ItemRep
Returns the rep with the given name.
103 104 105 |
# File 'lib/nanoc3/base/source_data/item.rb', line 103 def rep_named(rep_name) @reps.find { |r| r.name == rep_name } end |
#type ⇒ Symbol
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 ‘:item`, because this is an item. For layouts, this method returns `:layout`.
215 216 217 |
# File 'lib/nanoc3/base/source_data/item.rb', line 215 def type :item end |