Class: Dotfile

Inherits:
Object
  • Object
show all
Defined in:
lib/polka/dotfile.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, home_path) ⇒ Dotfile

Returns a new instance of Dotfile.



4
5
6
7
8
# File 'lib/polka/dotfile.rb', line 4

def initialize(path, home_path)
  raise FileNotPresentError, "Could not find file at #{path}." unless File.exists?(path)
  @path = path
  @home_path = home_path
end

Class Method Details

.backup_dir(home_path) ⇒ Object



30
31
32
33
# File 'lib/polka/dotfile.rb', line 30

def self.backup_dir(home_path)
  dir = File.join(".polka_backup", Time.now.strftime("%F_%T"))
  @@backup_dir ||= File.join(home_path, dir)
end

Instance Method Details

#backupObject



16
17
18
19
20
# File 'lib/polka/dotfile.rb', line 16

def backup
  Polka.log "    Backing up: ".light_yellow + "#{@home_path} => #{backup_dir}"
  FileUtils.mkdir_p(backup_dir) unless File.exists?(backup_dir)
  FileUtils.mv(@home_path, backup_dir)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/polka/dotfile.rb', line 26

def eql?(other)
  hash == other.hash
end

#hashObject



22
23
24
# File 'lib/polka/dotfile.rb', line 22

def hash
  @path.hash
end

#setup(operation) ⇒ Object



10
11
12
13
14
# File 'lib/polka/dotfile.rb', line 10

def setup(operation)
  backup if File.exists?(@home_path)
  operation.call(@path, @home_path)
  Polka.log "    #{@path} set up as #{@home_path}"
end