Sha256: 6a77648f65dba1ea2bf72ab174473027783142db5bc02cad36ed56d3db8926dd

Contents?: true

Size: 1.08 KB

Versions: 19

Compression:

Stored size: 1.08 KB

Contents

module Nanoc::Int
  # @api private
  class ItemRepWriter
    TMP_TEXT_ITEMS_DIR = 'text_items'.freeze

    def write(item_rep, raw_path)
      # Create parent directory
      FileUtils.mkdir_p(File.dirname(raw_path))

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

      # Notify
      Nanoc::Int::NotificationCenter.post(
        :will_write_rep, item_rep, raw_path
      )

      content = item_rep.snapshot_contents[:last]
      if content.binary?
        temp_path = content.filename
      else
        temp_path = temp_filename
        File.write(temp_path, content.string)
      end

      # Check whether content was modified
      is_modified = is_created || !FileUtils.identical?(raw_path, temp_path)

      # Write
      FileUtils.cp(temp_path, raw_path) if is_modified

      item_rep.modified = is_modified

      # Notify
      Nanoc::Int::NotificationCenter.post(
        :rep_written, item_rep, raw_path, is_created, is_modified
      )
    end

    def temp_filename
      Nanoc::Int::TempFilenameFactory.instance.create(TMP_TEXT_ITEMS_DIR)
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
nanoc-4.4.4 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.4.3 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.4.2 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.4.1 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.4.0 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.3.8 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.3.7 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.3.6 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.3.5 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.3.4 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.3.3 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.3.2 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.3.1 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.3.0 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.2.4 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.2.3 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.2.2 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.2.1 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.2.0 lib/nanoc/base/services/item_rep_writer.rb