Module: PopCap::Fileable
- Included in:
- AudioFile
- Defined in:
- lib/pop_cap/fileable.rb
Overview
Public: This is a wrapper for the File & FileUtils Ruby Standard Libraries. The module requires it be included in a class which has a #filepath method that returns a filepath.
Instance Method Summary collapse
-
#backup(backup_dir = '/tmp') ⇒ Object
Public: This will backup a file to a specified directory.
-
#backup_path ⇒ Object
Public: This will return the backup path.
-
#destroy ⇒ Object
Public: This will remove the file from the system.
-
#directory ⇒ Object
Public: This will return the directory, excluding the filename.
-
#filename ⇒ Object
Public: This will return the filename, excluding directory.
-
#move(destination) ⇒ Object
Public: This method moves a file to destination.
-
#rename(new_name) ⇒ Object
Public: This method renames a file.
-
#restore(from_path = nil) ⇒ Object
Public: This will restore a file from the backup path.
-
#tmppath ⇒ Object
Public: This returns a temporary path for the file.
Instance Method Details
#backup(backup_dir = '/tmp') ⇒ Object
Public: This will backup a file to a specified directory.
backup_dir - path to a directory on the filesystem. It defaults to ‘/tmp’.
Examples
audio_file.backup('/usr')
# => file is copied to '/usr'
21 22 23 24 |
# File 'lib/pop_cap/fileable.rb', line 21 def backup(backup_dir='/tmp') @backup_dir = backup_dir FileUtils.cp(self.filepath, backup_path) end |
#backup_path ⇒ Object
Public: This will return the backup path. It will raise an error if file was not backed up previously.
Examples
klass = SomeClass.new('path/to/file.txt').backup('/tmp')
klass.backup_path
# => '/tmp/file.txt'
34 35 36 37 |
# File 'lib/pop_cap/fileable.rb', line 34 def backup_path raise(PathError, ) unless @backup_dir "#{@backup_dir}/" + filename end |
#destroy ⇒ Object
Public: This will remove the file from the system.
Examples
klass = SomeClass.new('path/to/file.txt')
klass.destroy
45 46 47 48 |
# File 'lib/pop_cap/fileable.rb', line 45 def destroy FileUtils.rm_f(self.filepath) self.filepath = nil end |
#directory ⇒ Object
Public: This will return the directory, excluding the filename.
Examples
SomeClass.new('path/to/file.txt').directory
# => 'path/to/'
56 57 58 |
# File 'lib/pop_cap/fileable.rb', line 56 def directory File.dirname(self.filepath) end |
#filename ⇒ Object
Public: This will return the filename, excluding directory.
Examples
SomeClass.new('path/to/file.txt').filename
# => 'file.txt'
66 67 68 |
# File 'lib/pop_cap/fileable.rb', line 66 def filename File.basename(self.filepath) end |
#move(destination) ⇒ Object
Public: This method moves a file to destination.
destination - The folder/directory to move the file.
Examples
klass = SomeClass.new('path/to/file.txt')
klass.move('/tmp')
# => '/tmp/file.txt'
79 80 81 82 |
# File 'lib/pop_cap/fileable.rb', line 79 def move(destination) FileUtils.mv(self.filepath, destination) self.filepath = destination + '/' + filename end |
#rename(new_name) ⇒ Object
Public: This method renames a file.
new_name - The new name of the file.
Examples
klass = SomeClass.new('path/to/file.txt')
klass.rename('rename.txt')
# => 'path/to/rename.txt'
93 94 95 96 97 |
# File 'lib/pop_cap/fileable.rb', line 93 def rename(new_name) new_path = self.directory + '/' + new_name FileUtils.mv(self.filepath, new_path) self.filepath = new_path end |
#restore(from_path = nil) ⇒ Object
Public: This will restore a file from the backup path. It will raise an error if file has no backup path.
from_path - The path from which to restore. It defaults to the #backup_path.
Examples
klass = SomeClass.new('path/to/file.txt').backup('/tmp')
klass.restore
109 110 111 112 |
# File 'lib/pop_cap/fileable.rb', line 109 def restore(from_path=nil) @from_path = from_path FileUtils.mv(restore_path, self.filepath) end |
#tmppath ⇒ Object
Public: This returns a temporary path for the file.
Examples
klass = SomeClass.new('path/to/file.txt').tmppath
# => '/tmp/file.txt'
120 121 122 |
# File 'lib/pop_cap/fileable.rb', line 120 def tmppath '/tmp/' + filename end |