Class: Adrift::Storage::Filesystem
- Inherits:
-
Object
- Object
- Adrift::Storage::Filesystem
- Defined in:
- lib/adrift/storage.rb
Overview
Stores and removes files to and from the filesystem using queues.
Instance Attribute Summary collapse
-
#removed ⇒ Object
readonly
Returns the value of attribute removed.
-
#stored ⇒ Object
readonly
Returns the value of attribute stored.
Instance Method Summary collapse
-
#dirty? ⇒ Boolean
Indicates whether or not there are files that need to be stored or removed.
-
#flush ⇒ Object
Removes and then stores the files placed in the removal and storage queues, repectively.
-
#initialize ⇒ Filesystem
constructor
Creates a new Filesystem object.
-
#remove(path) ⇒ Object
Adds the file
path
to the removal queue. -
#remove! ⇒ Object
Removes the files placed in the removal queue.
-
#store(source_path, destination_path) ⇒ Object
Adds the file
source_path
to the storage queue, that will be saved indestination_path
. -
#store! ⇒ Object
Stores the files placed in the storage queue.
Constructor Details
#initialize ⇒ Filesystem
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
#removed ⇒ Object (readonly)
Returns the value of attribute removed.
25 26 27 |
# File 'lib/adrift/storage.rb', line 25 def removed @removed end |
#stored ⇒ Object (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.
37 38 39 |
# File 'lib/adrift/storage.rb', line 37 def dirty? @queue_for_storage.any? || @queue_for_removal.any? end |
#flush ⇒ Object
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 |