Module: Nanoc3::ItemRep::Private

Included in:
Nanoc3::ItemRep
Defined in:
lib/nanoc3/base/result_data/item_rep.rb

Overview

Contains all private methods. Mixed into Nanoc3::ItemRep.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#assignsHash

Returns A hash containing the assigns that will be used in the next filter or layout operation. The keys (symbols) will be made available during the next operation.

Returns:

  • (Hash)

    A hash containing the assigns that will be used in the next filter or layout operation. The keys (symbols) will be made available during the next operation.



67
68
69
# File 'lib/nanoc3/base/result_data/item_rep.rb', line 67

def assigns
  @assigns
end

#compiledBoolean Also known as: compiled?

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 true if this representation has already been compiled during the current or last compilation session; false otherwise.

Returns:

  • (Boolean)

    true if this representation has already been compiled during the current or last compilation session; false otherwise



74
75
76
# File 'lib/nanoc3/base/result_data/item_rep.rb', line 74

def compiled
  @compiled
end

#contentHash<Symbol,String>

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 A hash containing the content at all snapshots. The keys correspond with the snapshot names, and the values with the content.

Returns:

  • (Hash<Symbol,String>)

    A hash containing the content at all snapshots. The keys correspond with the snapshot names, and the values with the content.



108
109
110
# File 'lib/nanoc3/base/result_data/item_rep.rb', line 108

def content
  @content
end

#pathsHash<Symbol,String>

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 A hash containing the paths for all snapshots. The keys correspond with the snapshot names, and the values with the path.

Returns:

  • (Hash<Symbol,String>)

    A hash containing the paths for all snapshots. The keys correspond with the snapshot names, and the values with the path.



90
91
92
# File 'lib/nanoc3/base/result_data/item_rep.rb', line 90

def paths
  @paths
end

#raw_pathsHash<Symbol,String>

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 A hash containing the raw paths (paths including the path to the output directory and the filename) for all snapshots. The keys correspond with the snapshot names, and the values with the path.

Returns:

  • (Hash<Symbol,String>)

    A hash containing the raw paths (paths including the path to the output directory and the filename) for all snapshots. The keys correspond with the snapshot names, and the values with the path.



83
84
85
# File 'lib/nanoc3/base/result_data/item_rep.rb', line 83

def raw_paths
  @raw_paths
end

#temporary_filenamesHash<Symbol,String> (readonly)

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 A hash containing the paths to the temporary files that filters write binary content to. This is only used when the item representation is binary. The keys correspond with the snapshot names, and the values with the filename. When writing the item representation, the file corresponding with the requested snapshot (usually ‘:last`) will be copied from `filenames` to `raw_paths`.

Returns:

  • (Hash<Symbol,String>)

    A hash containing the paths to the temporary files that filters write binary content to. This is only used when the item representation is binary. The keys correspond with the snapshot names, and the values with the filename. When writing the item representation, the file corresponding with the requested snapshot (usually ‘:last`) will be copied from `filenames` to `raw_paths`.



101
102
103
# File 'lib/nanoc3/base/result_data/item_rep.rb', line 101

def temporary_filenames
  @temporary_filenames
end

Instance Method Details

#forget_progressvoid

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.

This method returns an undefined value.

Resets the compilation progress for this item representation. This is necessary when an unmet dependency is detected during compilation.



174
175
176
# File 'lib/nanoc3/base/result_data/item_rep.rb', line 174

def forget_progress
  initialize_content
end

#typeSymbol

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_rep`, because this is an item rep. For layouts, this method returns `:layout`.

Returns:

  • (Symbol)

    :item_rep



185
186
187
# File 'lib/nanoc3/base/result_data/item_rep.rb', line 185

def type
  :item_rep
end

#write(snapshot = :last) ⇒ void

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.

This method returns an undefined value.

Writes the item rep’s compiled content to the rep’s output file.

This method will send two notifications: one before writing the item representation, and one after. These notifications can be used for generating diffs, for example.

Parameters:

  • raw_path (String, nil)

    The raw path to write the compiled rep to. If nil, the default raw path will be used.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/nanoc3/base/result_data/item_rep.rb', line 122

def write(snapshot=:last)
  # Get raw path
  raw_path = self.raw_path(:snapshot => snapshot)
  return if raw_path.nil?

  # Create parent directory
  FileUtils.mkdir_p(File.dirname(raw_path))

  # Check if file will be created
  is_created = !File.file?(raw_path)

  # Calculate characteristics of old content
  if File.file?(raw_path)
    hash_old = Pathname.new(raw_path).checksum
    size_old = File.size(raw_path)
  end

  # Notify
  Nanoc3::NotificationCenter.post(:will_write_rep, self, snapshot)

  if self.binary?
    # Calculate characteristics of new content
    size_new = File.size(temporary_filenames[:last])
    hash_new = Pathname.new(temporary_filenames[:last]).checksum if size_old == size_new

    # Check whether content was modified
    is_modified = (size_old != size_new || hash_old != hash_new)

    # Copy
    if is_modified
      FileUtils.cp(temporary_filenames[:last], raw_path)
    end
  else
    # Check whether content was modified
    is_modified = (!File.file?(raw_path) || File.read(raw_path) != @content[:last])

    # Write
    if is_modified
      File.open(raw_path, 'w') { |io| io.write(@content[:last]) }
    end
  end

  # Notify
  Nanoc3::NotificationCenter.post(:rep_written, self, raw_path, is_created, is_modified)
end