Class: Harbor::File
- Inherits:
-
Object
- Object
- Harbor::File
- Defined in:
- lib/harbor/file.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#path ⇒ Object
Returns the value of attribute path.
Class Method Summary collapse
-
.move(from, to, mode = 0666 - ::File.umask) ⇒ Object
Moves a file and gives it the default file permissions minus the declared umask unless another mode is specified.
-
.move_safely(from, to, mode = 0666 - ::File.umask) ⇒ Object
The file is first copied, and then the provided block is run.
-
.rmdir_p(directory) ⇒ Object
Recursively delete empty directories as mkdir -p recursively creates directories.
Instance Method Summary collapse
-
#initialize(path, name = nil) ⇒ File
constructor
A new instance of File.
- #read(block_size) {|@io.read(block_size)| ... } ⇒ Object
- #rewind ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(path, name = nil) ⇒ File
Returns a new instance of File.
9 10 11 12 |
# File 'lib/harbor/file.rb', line 9 def initialize(path, name = nil) @path = path @name = name || ::File.basename(@path) end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/harbor/file.rb', line 7 def name @name end |
#path ⇒ Object
Returns the value of attribute path.
7 8 9 |
# File 'lib/harbor/file.rb', line 7 def path @path end |
Class Method Details
.move(from, to, mode = 0666 - ::File.umask) ⇒ Object
Moves a file and gives it the default file permissions minus the declared umask unless another mode is specified.
61 62 63 64 |
# File 'lib/harbor/file.rb', line 61 def self.move(from, to, mode = 0666 - ::File.umask) FileUtils.mv(from, to) FileUtils.chmod(mode, to) end |
.move_safely(from, to, mode = 0666 - ::File.umask) ⇒ Object
The file is first copied, and then the provided block is run. If no errors occur, the source file is deleted. If an error occurs, the copied file is removed and the directory cleaned.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/harbor/file.rb', line 33 def self.move_safely(from, to, mode = 0666 - ::File.umask) raise ArgumentError.new("no block given") unless block_given? FileUtils.mkdir_p(::File.dirname(to)) FileUtils.cp(from, to) FileUtils.chmod(mode, to) begin yield FileUtils.rm(from) rescue FileUtils.rm(to) rmdir_p(::File.dirname(to)) raise $! end end |
.rmdir_p(directory) ⇒ Object
Recursively delete empty directories as mkdir -p recursively creates directories.
53 54 55 |
# File 'lib/harbor/file.rb', line 53 def self.rmdir_p(directory) `rmdir -p #{Shellwords.escape(directory)} &> /dev/null` end |
Instance Method Details
#read(block_size) {|@io.read(block_size)| ... } ⇒ Object
14 15 16 17 |
# File 'lib/harbor/file.rb', line 14 def read(block_size) @io ||= File.open(@path, "rb") yield @io.read(block_size) end |
#rewind ⇒ Object
19 20 21 22 |
# File 'lib/harbor/file.rb', line 19 def rewind @io ||= File.open(@path, "rb") @io.rewind end |
#size ⇒ Object
24 25 26 |
# File 'lib/harbor/file.rb', line 24 def size ::File.size(@path) end |