Class: Nanoc::Core::Document

Inherits:
Object
  • Object
show all
Includes:
ContractsSupport
Defined in:
lib/nanoc/core/document.rb

Direct Known Subclasses

Item, Layout

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ContractsSupport

enabled?, included, setup_once, warn_about_performance

Constructor Details

#initialize(content, attributes, identifier, checksum_data: nil, content_checksum_data: nil, attributes_checksum_data: nil) ⇒ Document

Returns a new instance of Document.

Parameters:



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/nanoc/core/document.rb', line 44

def initialize(content, attributes, identifier, checksum_data: nil, content_checksum_data: nil, attributes_checksum_data: nil)
  @content = Nanoc::Core::Content.create(content)
  @attributes = Nanoc::Core::LazyValue.new(attributes).map(&:__nanoc_symbolize_keys_recursively)
  @identifier = Nanoc::Core::Identifier.from(identifier)

  @checksum_data = checksum_data
  @content_checksum_data = content_checksum_data
  @attributes_checksum_data = attributes_checksum_data

  # Precalculate for performance
  @hash = [self.class, identifier].hash
  reference
end

Instance Attribute Details

#attributes_checksum_dataString?

Returns:



21
22
23
# File 'lib/nanoc/core/document.rb', line 21

def attributes_checksum_data
  @attributes_checksum_data
end

#checksum_dataString?

Returns:



15
16
17
# File 'lib/nanoc/core/document.rb', line 15

def checksum_data
  @checksum_data
end

#contentNanoc::Core::Content



9
10
11
# File 'lib/nanoc/core/document.rb', line 9

def content
  @content
end

#content_checksum_dataString?

Returns:



18
19
20
# File 'lib/nanoc/core/document.rb', line 18

def content_checksum_data
  @content_checksum_data
end

#identifierNanoc::Core::Identifier



12
13
14
# File 'lib/nanoc/core/document.rb', line 12

def identifier
  @identifier
end

Instance Method Details

#==(other) ⇒ Object



118
119
120
# File 'lib/nanoc/core/document.rb', line 118

def ==(other)
  other.respond_to?(:identifier) && identifier == other.identifier
end

#attributesHash

Returns:



59
60
61
# File 'lib/nanoc/core/document.rb', line 59

def attributes
  @attributes.value
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/nanoc/core/document.rb', line 123

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

#freezevoid

This method returns an undefined value.



65
66
67
68
69
70
# File 'lib/nanoc/core/document.rb', line 65

def freeze
  super
  @content.freeze
  @attributes.freeze
  self
end

#hashObject



113
114
115
# File 'lib/nanoc/core/document.rb', line 113

def hash
  @hash
end

#inspectObject



108
109
110
# File 'lib/nanoc/core/document.rb', line 108

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

#referenceObject

This method is abstract.

Returns Unique reference to this object.

Returns:

  • Unique reference to this object

Raises:

  • (NotImplementedError)


83
84
85
# File 'lib/nanoc/core/document.rb', line 83

def reference
  raise NotImplementedError
end

#set_attribute(key, value) ⇒ Object



100
101
102
103
104
105
# File 'lib/nanoc/core/document.rb', line 100

def set_attribute(key, value)
  attributes[key] = value

  @checksum_data = nil
  @attributes_checksum_data = nil
end

#with_identifier_prefix(prefix) ⇒ Object



73
74
75
76
77
# File 'lib/nanoc/core/document.rb', line 73

def with_identifier_prefix(prefix)
  other = dup
  other.identifier = @identifier.prefix(prefix)
  other
end