Sha256: 4f15d0697f6f682451d76ba2d9c56281fdfcfa3d5b609dc1ead0a1b7693d7706

Contents?: true

Size: 1.54 KB

Versions: 6

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

require 'fileutils'

module Entangler
  class EntangledFile
    attr_accessor :desired_modtime, :action
    attr_reader :path, :contents

    def initialize(action, rel_path)
      @action = action
      @path = rel_path
      @desired_modtime = Time.now.to_i
      @contents = nil
    end

    def full_path
      Entangler.executor.generate_abs_path(@path)
    end

    def file_exists?
      File.exist?(full_path)
    end

    def process
      if action == :create || action == :update
        create_parent_directory
        write_contents
      elsif action == :delete
        delete_file
      end
    end

    private

    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

    def write_contents
      delete_file if file_exists? && File.directory?(full_path)
      File.write(full_path, contents)
      File.utime(File.atime(full_path), desired_modtime, full_path)
    end

    def delete_file
      FileUtils.rm_rf(full_path) if file_exists?
    end

    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

    def marshal_load(array)
      @action, @path, @desired_modtime, @contents = *array
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
entangler-1.6.1 lib/entangler/entangled_file.rb
entangler-1.6.0 lib/entangler/entangled_file.rb
entangler-1.5.1 lib/entangler/entangled_file.rb
entangler-1.5.0 lib/entangler/entangled_file.rb
entangler-1.4.1 lib/entangler/entangled_file.rb
entangler-1.4.0 lib/entangler/entangled_file.rb