Class: Dotbox::File

Inherits:
Object
  • Object
show all
Includes:
Actions
Defined in:
lib/dotbox/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Actions

#rm_empty_dir

Constructor Details

#initialize(path) ⇒ File

Returns a new instance of File.



10
11
12
13
# File 'lib/dotbox/file.rb', line 10

def initialize(path)
  @abs_path = ::File.expand_path(path)
  @rel_path = @abs_path.sub(/^#{Thor::Util.user_home}\//, '')
end

Instance Attribute Details

#abs_pathObject (readonly)

Returns the value of attribute abs_path.



6
7
8
# File 'lib/dotbox/file.rb', line 6

def abs_path
  @abs_path
end

#rel_pathObject (readonly)

Returns the value of attribute rel_path.



6
7
8
# File 'lib/dotbox/file.rb', line 6

def rel_path
  @rel_path
end

Instance Method Details

#backupObject



27
28
29
30
31
# File 'lib/dotbox/file.rb', line 27

def backup
  FileUtils.mkdir_p ::File.dirname(backup_path)
  FileUtils.mv @abs_path, backup_path
  FileUtils.ln_s backup_path, @abs_path
end

#backup_pathObject



19
20
21
22
23
24
25
# File 'lib/dotbox/file.rb', line 19

def backup_path
  if @backup_path.nil?
    backup_path = "#{Config.new(Dotbox::CONFIG_FILE).value}/Apps/Dotbox"
    @backup_path = ::File.expand_path("#{backup_path}/#{@rel_path}")
  end
  @backup_path
end

#backuped?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/dotbox/file.rb', line 46

def backuped?
  link? && link_of?(backup_path)
end

#directory?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/dotbox/file.rb', line 15

def directory?
  ::File.directory?(@abs_path)
end

#link?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/dotbox/file.rb', line 50

def link?
  ::File.symlink? @abs_path
end

Returns:

  • (Boolean)


54
55
56
# File 'lib/dotbox/file.rb', line 54

def link_of?(src)
  ::File.readlink(@abs_path) == src
end

#removeObject



33
34
35
36
37
38
39
# File 'lib/dotbox/file.rb', line 33

def remove
  # must get the backup path first
  backup_path
  FileUtils.rm @abs_path
  FileUtils.mv backup_path, @abs_path
  rm_empty_dir ::File.dirname(backup_path)
end

#restoreObject



41
42
43
44
# File 'lib/dotbox/file.rb', line 41

def restore
  FileUtils.mkdir_p ::File.dirname(@abs_path)
  FileUtils.ln_s backup_path, @abs_path
end

#typeObject



58
59
60
# File 'lib/dotbox/file.rb', line 58

def type
  @type = directory? ? 'directory' : 'file'
end