Class: Dutiful::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/dutiful/storage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil, path: nil) ⇒ Storage

Returns a new instance of Storage.



7
8
9
10
11
12
13
14
# File 'lib/dutiful/storage.rb', line 7

def initialize(name: nil, path: nil)
  name ||= 'Custom folder'
  path ||= Tomlrb.load_file("#{Dutiful.dir}/config/#{name.downcase}.toml", symbolize_keys: true)[:storage][:path]

  @name         = name
  @storage_path = File.expand_path path
  @path         = "#{@storage_path}/dutiful"
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/dutiful/storage.rb', line 5

def name
  @name
end

#storage_pathObject (readonly)

Returns the value of attribute storage_path.



5
6
7
# File 'lib/dutiful/storage.rb', line 5

def storage_path
  @storage_path
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/dutiful/storage.rb', line 16

def available?
  File.exist? storage_path
end

#backup(file) ⇒ Object



37
38
39
40
# File 'lib/dutiful/storage.rb', line 37

def backup(file)
  create_dir file
  Rsync.run file.full_path.shellescape, file.backup_path.shellescape, '--recursive'
end

#create_dir(file) ⇒ Object



20
21
22
23
# File 'lib/dutiful/storage.rb', line 20

def create_dir(file)
  FileUtils.mkdir_p File.dirname "#{path}/#{file.path}"
  FileUtils.mkdir_p File.dirname "#{file.full_path}"
end

#exist?(file) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/dutiful/storage.rb', line 25

def exist?(file)
  File.exist? "#{path}/#{file.path}"
end

#in_sync?(file) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dutiful/storage.rb', line 59

def in_sync?(file)
  if file.directory?
    Dir.glob("#{file.full_path}*").all? do |file_path|
      filename = File.basename(file_path)
      file_backup_path = path "#{file.path}#{filename}"

      File.exist?(file_path) &&
        File.exist?(file_backup_path) &&
        FileUtils.identical?(file_path, file_backup_path)
    end
  else
    FileUtils.identical? file.full_path, "#{path}/#{file.path}"
  end
end

#path(path = nil) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/dutiful/storage.rb', line 29

def path(path = nil)
  if path
    "#{@path}/#{path}"
  else
    @path
  end
end

#restore(file) ⇒ Object



42
43
44
45
# File 'lib/dutiful/storage.rb', line 42

def restore(file)
  create_dir file
  Rsync.run file.backup_path.shellescape, file.full_path.shellescape, '--recursive'
end

#sync(file) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dutiful/storage.rb', line 47

def sync(file)
  if file.exist?
    if file.has_backup? && file.backup_timestamp > file.timestamp
      restore file
    else
      backup file
    end
  else
    restore file
  end
end