Sha256: e1741015c0af74cc1056aa8ad8c29e4fc3fcc566c0cebab1f2dd5b8c43268c5c

Contents?: true

Size: 1 KB

Versions: 2

Compression:

Stored size: 1 KB

Contents

require 'pathname'
require 'fattr'
require 'fileutils'

# SourcePath represents an article source file on disk.
class Germinate::SourceFile
  fattr :path
  fattr(:backup_path) { Pathname(path.to_s + ".germ.bak") }
  fattr(:log) { Germinate.logger }

  def initialize(path)
    @path = Pathname(path)
  end

  def write!(lines)
    log.debug "Writing #{lines.size} lines to #{path}"
    file = File.new(path)
    flock_result = file.flock(File::LOCK_EX)
    if flock_result != 0
      raise "Unable to lock file #{path}"
    end
    FileUtils.cp(path, backup_path)
    unless path.read == backup_path.read
      raise "Error backup up #{path} to #{backup_path}"
    end
    begin
      path.open('w+') do |output|
        lines.each do |line|
          output.write(line)
        end
      end
    rescue Exception => error
      FileUtils.cp(backup_path, path)
      raise
    end
    log.info "Changes saved to #{path}."
    log.info "Previous state saved as #{backup_path}."
  ensure
    file.flock(File::LOCK_UN)
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
devver-germinate-1.2.0 lib/germinate/source_file.rb
germinate-1.2.0 lib/germinate/source_file.rb