lib/nanoc3/base/source_data/item.rb in nanoc3-3.2.0a3 vs lib/nanoc3/base/source_data/item.rb in nanoc3-3.2.0a4

- old
+ new

@@ -5,10 +5,12 @@ # 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. class Item + extend Nanoc3::Memoization + # @return [Hash] This item's attributes attr_accessor :attributes # A string that uniquely identifies an item in a site. # @@ -76,14 +78,14 @@ @raw_content = raw_content_or_raw_filename end # Get rest of params @attributes = attributes.symbolize_keys - @identifier = identifier.cleaned_identifier + @identifier = identifier.cleaned_identifier.freeze # Set mtime - @attributes.merge(:mtime => params[:mtime]) if params[:mtime] + @attributes.merge!(:mtime => params[:mtime]) if params[:mtime] @parent = nil @children = [] @reps = [] @@ -162,10 +164,16 @@ Nanoc3::NotificationCenter.post(:visit_ended, self) # Get captured content (hax) # TODO [in nanoc 4.0] remove me if key.to_s =~ /^content_for_(.*)$/ + # 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 @@ -208,18 +216,35 @@ # @return [Object] An unique reference to this object def reference [ type, self.identifier ] end - # Prevents all further modifications to itself, its items, its layouts etc. + # Prevents all further modifications to its attributes. # # @return [void] def freeze - attributes.freeze + attributes.freeze_recursively + children.freeze + identifier.freeze + raw_filename.freeze if raw_filename + raw_content.freeze if raw_content end + def inspect "<#{self.class}:0x#{self.object_id.to_s(16)} identifier=#{self.identifier} binary?=#{self.binary?}>" end + + # TODO document + def checksum + if binary? + Pathname.new(raw_filename).checksum + else + attributes = @attributes.dup + attributes.delete(:file) + @raw_content.checksum + ',' + attributes.checksum + end + end + memoize :checksum # @deprecated Access the modification time using `item[:mtime]` instead. def mtime self[:mtime] end