Sha256: d4cb142d8b616beceaf884332df957b34af60a38114d6ff52bfd6c41f2c96686

Contents?: true

Size: 1.62 KB

Versions: 13

Compression:

Stored size: 1.62 KB

Contents

module Dots
  class DotFile
    include FileUtils
    include ActiveModel::Validations
    
    DOTS_HOME = File.expand_path "~/.dots"
    USER_HOME = File.expand_path "~/"

    validate :untracked_path_is_a_file

    attr_reader :tracked_path

    # Instantiate the file and make sure it exists.
    def initialize with_file_name
      @untracked_path = File.expand_path with_file_name
      @file_name = File.basename(with_file_name)[1..-1]
      @tracked_path = "#{DOTS_HOME}/config/#{@file_name}"
    end

    def to_s
      @untracked_path
    end

    def self.find by_file_name
      new by_file_name
    end

    # Persist the file in the ~/.dots repository, and symlink to its old
    # path.
    def save
      if valid?
        move_to_dots and symlink_old_path
      else
        false
      end
    end

    def saved?
      File.exists? @tracked_path and File.exists? @untracked_path
    end

    # "Forget" the file, remove its symlink and restore its location.
    def destroy
      delete_symlink and restore_to_original_path
    end

  private
    def move_to_dots
      mv @untracked_path, @tracked_path
      File.exists? @tracked_path
    end

    def symlink_old_path
      File.symlink(@tracked_path, @untracked_path) == 0
    end

    def restore_to_original_path
      mv @tracked_path, @untracked_path
      File.exists? @untracked_path and not File.symlink? @untracked_path
    end

    def delete_symlink
      rm @untracked_path
      not File.exists? @untracked_path
    end

    def untracked_path_is_a_file
      unless File.exists? @untracked_path
        errors.add :untracked_file, "does not exist"
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
zsh_dots-0.6.5 lib/ruby/dots/dot_file.rb
zsh_dots-0.6.3 lib/ruby/dots/dot_file.rb
zsh_dots-0.6.2 lib/ruby/dots/dot_file.rb
zsh_dots-0.6.0 lib/ruby/dots/dot_file.rb
zsh_dots-0.5.9 lib/ruby/dots/dot_file.rb
zsh_dots-0.5.8 lib/ruby/dots/dot_file.rb
zsh_dots-0.5.7 lib/ruby/dots/dot_file.rb
zsh_dots-0.5.6 lib/ruby/dots/dot_file.rb
zsh_dots-0.5.5 lib/ruby/dots/dot_file.rb
zsh_dots-0.5.4 lib/ruby/dots/dot_file.rb
zsh_dots-0.5.3 lib/ruby/dots/dot_file.rb
zsh_dots-0.5.1 lib/ruby/dots/dot_file.rb
zsh_dots-0.5.0 lib/ruby/dots/dot_file.rb