class Entangler::EntangledFile

Attributes

action[RW]
contents[R]
desired_modtime[RW]
path[R]

Public Class Methods

new(action, rel_path) click to toggle source
# File lib/entangler/entangled_file.rb, line 10
def initialize(action, rel_path)
  @action = action
  @path = rel_path
  @desired_modtime = Time.now.to_i
  @contents = nil
end

Public Instance Methods

file_exists?() click to toggle source
# File lib/entangler/entangled_file.rb, line 21
def file_exists?
  File.exist?(full_path)
end
full_path() click to toggle source
# File lib/entangler/entangled_file.rb, line 17
def full_path
  Entangler.executor.generate_abs_path(@path)
end
process() click to toggle source
# File lib/entangler/entangled_file.rb, line 25
def process
  if action == :create || action == :update
    create_parent_directory
    write_contents
  elsif action == :delete
    delete_file
  end
end

Private Instance Methods

create_parent_directory() click to toggle source
# File lib/entangler/entangled_file.rb, line 36
def create_parent_directory
  dirname = File.dirname(full_path)
  if File.exist?(dirname)
    unless File.directory?(dirname)
      FileUtils.rm dirname
      FileUtils.mkdir_p dirname
    end
  else
    FileUtils.mkdir_p dirname
  end
end
delete_file() click to toggle source
# File lib/entangler/entangled_file.rb, line 54
def delete_file
  FileUtils.rm_rf(full_path) if file_exists?
end
marshal_dump() click to toggle source
# File lib/entangler/entangled_file.rb, line 58
def marshal_dump
  if file_exists? && (action == :create || action == :update)
    @desired_modtime = File.mtime(full_path).to_i
    @contents = File.read(full_path)
  end

  [action, path, desired_modtime, contents]
end
marshal_load(array) click to toggle source
# File lib/entangler/entangled_file.rb, line 67
def marshal_load(array)
  @action, @path, @desired_modtime, @contents = *array
end
write_contents() click to toggle source
# File lib/entangler/entangled_file.rb, line 48
def write_contents
  delete_file if file_exists? && File.directory?(full_path)
  File.open(full_path, 'w') { |f| f.write(contents) }
  File.utime(File.atime(full_path), desired_modtime, full_path)
end