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
32
33
34
# File 'lib/dotbox/file.rb', line 27

def backup
  if ::File.exist?(backup_path)
    raise "#{@rel_path} has been backuped."
  end
  FileUtils.mkdir_p ::File.dirname(backup_path)
  FileUtils.mv @abs_path, backup_path, verbose: true
  FileUtils.ln_s backup_path, @abs_path, verbose: true
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)


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

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

#exist?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/dotbox/file.rb', line 73

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

#link?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/dotbox/file.rb', line 61

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

Returns:

  • (Boolean)


65
66
67
# File 'lib/dotbox/file.rb', line 65

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

#removeObject



36
37
38
39
40
41
42
# File 'lib/dotbox/file.rb', line 36

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

#restoreObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dotbox/file.rb', line 44

def restore
  if exist?
    if link? && link_of?(backup_path)
      # Dont need restore
      return
    else
      FileUtils.mv @abs_path, "#{@abs_path}.bak", verbose: true
    end
  end
  FileUtils.mkdir_p ::File.dirname(@abs_path), verbose: true
  FileUtils.ln_s backup_path, @abs_path, verbose: true
end

#typeObject



69
70
71
# File 'lib/dotbox/file.rb', line 69

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