Class: Adrift::Storage::Filesystem

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

Overview

Stores and removes files to and from the filesystem using queues.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFilesystem

Creates a new Filesystem object.



28
29
30
31
32
33
# File 'lib/adrift/storage.rb', line 28

def initialize
  @queue_for_storage = []
  @queue_for_removal = []
  @stored  = []
  @removed = []
end

Instance Attribute Details

#removedObject (readonly)

Returns the value of attribute removed.



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

def removed
  @removed
end

#storedObject (readonly)

Returns the value of attribute stored.



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

def stored
  @stored
end

Instance Method Details

#dirty?Boolean

Indicates whether or not there are files that need to be stored or removed.

Returns:

  • (Boolean)


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

def dirty?
  @queue_for_storage.any? || @queue_for_removal.any?
end

#flushObject

Removes and then stores the files placed in the removal and storage queues, repectively.



76
77
78
79
# File 'lib/adrift/storage.rb', line 76

def flush
  remove!
  store!
end

#remove(path) ⇒ Object

Adds the file path to the removal queue. Note that in order to actually remove the file you need to call #flush.



61
62
63
# File 'lib/adrift/storage.rb', line 61

def remove(path)
  @queue_for_removal << path
end

#remove!Object

Removes the files placed in the removal queue.



66
67
68
69
70
71
72
# File 'lib/adrift/storage.rb', line 66

def remove!
  @queue_for_removal.each do |path|
    FileUtils.rm(path) if File.exist?(path)
  end
  @removed = @queue_for_removal.dup
  @queue_for_removal.clear
end

#store(source_path, destination_path) ⇒ Object

Adds the file source_path to the storage queue, that will be saved in destination_path. Note that in order to actually store the file you need to call #flush.



44
45
46
# File 'lib/adrift/storage.rb', line 44

def store(source_path, destination_path)
  @queue_for_storage << [source_path, destination_path]
end

#store!Object

Stores the files placed in the storage queue.



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

def store!
  @queue_for_storage.each do |source_path, destination_path|
    FileUtils.mkdir_p(File.dirname(destination_path))
    FileUtils.cp(source_path, destination_path)
    FileUtils.chmod(0644, destination_path)
  end
  @stored = @queue_for_storage.dup
  @queue_for_storage.clear
end