lib/nanoc/base/source_data/item.rb in nanoc-3.8.0 vs lib/nanoc/base/source_data/item.rb in nanoc-4.0.0a1
- old
+ new
@@ -1,49 +1,41 @@
# encoding: utf-8
-module Nanoc
+module Nanoc::Int
# 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.
+ #
+ # @api private
class Item
- extend Nanoc::Memoization
+ extend Nanoc::Int::Memoization
# @return [Hash] This item's attributes
attr_accessor :attributes
- # 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.
- #
- # @return [String] This item's identifier
+ # @return [Nanoc::Identifier] This item's identifier
attr_accessor :identifier
- # @return [Array<Nanoc::ItemRep>] This item’s list of item reps
+ # @return [Array<Nanoc::Int::ItemRep>] This item’s list of item reps
attr_reader :reps
# @return [String] This item's raw, uncompiled content of this item (only
# available for textual items)
attr_reader :raw_content
# @return [String] The filename pointing to the file containing this
# item’s content
attr_reader :raw_filename
- # @return [Nanoc::Site] The site this item belongs to
+ # @return [Nanoc::Int::Site] The site this item belongs to
attr_accessor :site
- # @return [Nanoc::Item, nil] The parent item of this item. This can be
+ # @return [Nanoc::Int::Item, nil] The parent item of this item. This can be
# nil even for non-root items.
attr_accessor :parent
- # @return [Array<Nanoc::Item>] The child items of this item
+ # @return [Array<Nanoc::Int::Item>] The child items of this item
attr_accessor :children
# Creates a new item with the given content or filename, attributes and
# identifier.
#
@@ -53,25 +45,17 @@
#
# @param [Hash] attributes A hash containing this item's attributes.
#
# @param [String] identifier This item's identifier.
#
- # @param [Time, Hash] params Extra parameters. For backwards
- # compatibility, this can be a Time instance indicating the time when
- # this item was last modified (mtime).
+ # @param [Hash] params Extra parameters.
#
- # @option params [Time, nil] :mtime (nil) The time when this item was last
- # modified. Deprecated; pass the modification time as the `:mtime`
- # attribute instead.
- #
# @option params [Symbol, nil] :binary (true) Whether or not this item is
# binary
- def initialize(raw_content_or_raw_filename, attributes, identifier, params = nil)
+ def initialize(raw_content_or_raw_filename, attributes, identifier, params = {})
# Parse params
- params ||= {}
- params = { mtime: params } if params.is_a?(Time)
- params[:binary] = false unless params.key?(:binary)
+ params = params.merge(binary: false) unless params.key?(:binary)
if raw_content_or_raw_filename.nil?
raise "attempted to create an item with no content/filename (identifier #{identifier})"
end
@@ -83,12 +67,12 @@
@raw_filename = attributes[:content_filename]
@raw_content = raw_content_or_raw_filename
end
# Get rest of params
- @attributes = attributes.symbolize_keys_recursively
- @identifier = identifier.cleaned_identifier.freeze
+ @attributes = attributes.__nanoc_symbolize_keys_recursively
+ @identifier = Nanoc::Identifier.new(identifier)
# Set mtime
@attributes.merge!(mtime: params[:mtime]) if params[:mtime]
@parent = nil
@@ -99,11 +83,11 @@
# Returns the rep with the given name.
#
# @param [Symbol] rep_name The name of the representation to return
#
- # @return [Nanoc::ItemRep] The representation with the given name
+ # @return [Nanoc::Int::ItemRep] The representation with the given name
def rep_named(rep_name)
@reps.find { |r| r.name == rep_name }
end
# Returns the compiled content from a given representation and a given
@@ -127,11 +111,11 @@
def compiled_content(params = {})
# Get rep
rep_name = params[:rep] || :default
rep = reps.find { |r| r.name == rep_name }
if rep.nil?
- raise Nanoc::Errors::Generic,
+ raise Nanoc::Int::Errors::Generic,
"No rep named #{rep_name.inspect} was found."
end
# Get rep's content
rep.compiled_content(params)
@@ -150,11 +134,11 @@
rep_name = params[:rep] || :default
# Get rep
rep = reps.find { |r| r.name == rep_name }
if rep.nil?
- raise Nanoc::Errors::Generic,
+ raise Nanoc::Int::Errors::Generic,
"No rep named #{rep_name.inspect} was found."
end
# Get rep's path
rep.path
@@ -164,35 +148,13 @@
#
# @param [Symbol] key The name of the attribute to fetch
#
# @return [Object] The value of the requested attribute
def [](key)
- Nanoc::NotificationCenter.post(:visit_started, self)
- Nanoc::NotificationCenter.post(:visit_ended, self)
+ Nanoc::Int::NotificationCenter.post(:visit_started, self)
+ Nanoc::Int::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
- @@_capturing_helper_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 @@_capturing_helper_included
- self.class.send(:include, ::Nanoc::Helpers::Capturing)
- @@_capturing_helper_included = true
- end
-
- # Get content
- return content_for(self, $1.to_sym)
- end
-
@attributes[key]
end
# Sets the attribute with the given key to the given value.
#
@@ -222,18 +184,18 @@
#
# @api private
#
# @return [Object] An unique reference to this object
def reference
- [type, identifier]
+ [type, identifier.to_s]
end
# Prevents all further modifications to its attributes.
#
# @return [void]
def freeze
- attributes.freeze_recursively
+ attributes.__nanoc_freeze_recursively
children.freeze
identifier.freeze
raw_filename.freeze if raw_filename
raw_content.freeze if raw_content
end
@@ -242,14 +204,14 @@
"<#{self.class} identifier=\"#{identifier}\" binary?=#{self.binary?}>"
end
# @return [String] The checksum for this object. If its contents change,
# the checksum will change as well.
- def checksum
- Nanoc::Checksummer.calc(self)
+ def __nanoc_checksum
+ Nanoc::Int::Checksummer.calc(self)
end
- memoize :checksum
+ memoize :__nanoc_checksum
def hash
self.class.hash ^ identifier.hash
end
@@ -285,13 +247,8 @@
end
# @api private
def forced_outdated?
@forced_outdated || false
- end
-
- # @deprecated Access the modification time using `item[:mtime]` instead.
- def mtime
- self[:mtime]
end
end
end