Class: Distillery::ROM::Path::File
- Inherits:
-
Distillery::ROM::Path
- Object
- Distillery::ROM::Path
- Distillery::ROM::Path::File
- Defined in:
- lib/distillery/rom/path/file.rb
Overview
Path from a file
Instance Method Summary collapse
-
#basename ⇒ String
Get path basename.
-
#copy(to, length = nil, offset = 0, force: false, link: :hard) ⇒ Boolean
Copy ROM content to the filesystem, possibly using link if requested.
-
#delete! ⇒ Boolean
Delete physical content.
-
#entry ⇒ String
Entry.
-
#file ⇒ String
File directly accessible on the file system.
-
#initialize(entry, basedir = nil) ⇒ File
constructor
Returns a new instance of File.
-
#reader {|io| ... } ⇒ Object
ROM reader.
-
#rename(path, force: false) {|old, new| ... } ⇒ Boolean
Rename ROM and physical content.
-
#storage ⇒ String
File or directory that is considered the storage space for entries.
-
#to_s ⇒ String
Path value as string.
Constructor Details
#initialize(entry, basedir = nil) ⇒ File
Returns a new instance of File.
14 15 16 17 18 19 20 21 |
# File 'lib/distillery/rom/path/file.rb', line 14 def initialize(entry, basedir=nil) if entry.start_with?('/') raise ArgumentError, "entry must be relative to basedir" end @entry = entry @basedir = basedir || '.' end |
Instance Method Details
#basename ⇒ String
Get path basename
47 48 49 |
# File 'lib/distillery/rom/path/file.rb', line 47 def basename ::File.basename(@entry) end |
#copy(to, length = nil, offset = 0, force: false, link: :hard) ⇒ Boolean
Copy ROM content to the filesystem, possibly using link if requested.
57 58 59 60 61 62 |
# File 'lib/distillery/rom/path/file.rb', line 57 def copy(to, length = nil, offset = 0, force: false, link: :hard) (!force && length.nil? && offset.zero? && ::File.exists?(to) && self.same?(ROM.from_file(to))) || ROM.filecopy(self.file, to, length, offset, force: force, link: link) end |
#delete! ⇒ Boolean
Delete physical content.
91 92 93 94 95 |
# File 'lib/distillery/rom/path/file.rb', line 91 def delete! ::File.unlink(self.file) == 1 rescue SystemCallError false end |
#entry ⇒ String
Entry
42 43 44 |
# File 'lib/distillery/rom/path/file.rb', line 42 def entry @entry end |
#file ⇒ String
File directly accessible on the file system
29 30 31 32 33 34 |
# File 'lib/distillery/rom/path/file.rb', line 29 def file if @basedir == '.' then @entry else ::File.join(@basedir, @entry) end end |
#reader {|io| ... } ⇒ Object
Can be costly, prefer existing #copy if possible
ROM reader
52 53 54 |
# File 'lib/distillery/rom/path/file.rb', line 52 def reader(&block) ::File.open(self.file, ::File::RDONLY, binmode: true, &block) end |
#rename(path, force: false) {|old, new| ... } ⇒ Boolean
Renaming could lead to silent removing if same ROM is on its way
Rename ROM and physical content.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/distillery/rom/path/file.rb', line 65 def rename(path, force: false) case path when String else raise ArgumentError, "unsupport path type (#{path.class})" end file = if path.start_with?('/') then path else ::File.join(@basedir, path) end if !::File.exists?(file) ::File.rename(self.file, file) == 0 elsif self.same?(ROM.from_file(file)) ::File.unlink(self.file) == 1 elsif force ::File.rename(self.file, file) == 0 else false end rescue SystemCallError false end |
#storage ⇒ String
File or directory that is considered the storage space for entries
37 38 39 |
# File 'lib/distillery/rom/path/file.rb', line 37 def storage @basedir end |
#to_s ⇒ String
Path value as string.
24 25 26 |
# File 'lib/distillery/rom/path/file.rb', line 24 def to_s self.file end |