Class: IOStreams::Path
Direct Known Subclasses
IOStreams::Paths::File, IOStreams::Paths::HTTP, IOStreams::Paths::S3, IOStreams::Paths::SFTP
Instance Attribute Summary collapse
-
#path ⇒ Object
Returns the value of attribute path.
Attributes inherited from Stream
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Paths are sortable by name.
-
#==(other) ⇒ Object
Compare by path name, ignore streams.
- #absolute? ⇒ Boolean
-
#children(*args, **kargs) ⇒ Object
Returns [Array] of child files based on the supplied pattern.
-
#compressed? ⇒ Boolean
Returns [true|false] whether the file is compressed based on its file extensions.
-
#copy_from(source, **args) ⇒ Object
Cleanup an incomplete write to the target “file” if the copy fails.
-
#delete ⇒ Object
When path is a file, deletes this file.
-
#delete_all ⇒ Object
When path is a directory ,deletes this directory and all its children.
-
#directory ⇒ Object
Returns [IOStreams::Path] the directory for this file.
-
#each_child(pattern = "*", **args, &block) ⇒ Object
Runs the pattern from the current path, returning the complete path for located files.
-
#encrypted? ⇒ Boolean
Returns [true|false] whether the file is encrypted based on its file extensions.
-
#exist? ⇒ Boolean
Returns [true|false] whether the file exists.
-
#initialize(path) ⇒ Path
constructor
A new instance of Path.
- #inspect ⇒ Object
-
#join(*elements) ⇒ Object
If elements already contains the current path then it is used as is without adding the current path for a second time.
-
#mkdir ⇒ Object
Assumes the current path does not include a file name, and creates all elements in the path.
-
#mkpath ⇒ Object
Removes the last element of the path, the file name, before creating the entire path.
-
#move_to(target_path) ⇒ Object
Moves the file by copying it to the new path and then deleting the current path.
-
#partial_files_visible? ⇒ Boolean
Returns [true|false] whether partially created files are visible on this path.
-
#realpath ⇒ Object
By default realpath just returns self.
- #relative? ⇒ Boolean
-
#size ⇒ Object
Returns [Integer] size of the file.
-
#to_s ⇒ Object
Returns [String] the current path.
Methods inherited from Stream
#basename, #copy_to, #dirname, #each, #extension, #extname, #file_name, #file_name=, #option, #option_or_stream, #pipeline, #read, #reader, #setting, #stream, #write, #writer
Constructor Details
#initialize(path) ⇒ Path
Returns a new instance of Path.
5 6 7 8 9 10 11 12 |
# File 'lib/io_streams/path.rb', line 5 def initialize(path) raise(ArgumentError, "Path cannot be nil") if path.nil? raise(ArgumentError, "Path must be a string: #{path.inspect}, class: #{path.class}") unless path.is_a?(String) @path = path.frozen? ? path : path.dup.freeze @io_stream = nil @builder = nil end |
Instance Attribute Details
#path ⇒ Object
Returns the value of attribute path.
3 4 5 |
# File 'lib/io_streams/path.rb', line 3 def path @path end |
Instance Method Details
#<=>(other) ⇒ Object
Paths are sortable by name
182 183 184 |
# File 'lib/io_streams/path.rb', line 182 def <=>(other) path <=> other.path end |
#==(other) ⇒ Object
Compare by path name, ignore streams
187 188 189 |
# File 'lib/io_streams/path.rb', line 187 def ==(other) path == other.path end |
#absolute? ⇒ Boolean
32 33 34 |
# File 'lib/io_streams/path.rb', line 32 def absolute? !!(path.strip =~ %r{\A/}) end |
#children(*args, **kargs) ⇒ Object
Returns [Array] of child files based on the supplied pattern
49 50 51 52 53 |
# File 'lib/io_streams/path.rb', line 49 def children(*args, **kargs) paths = [] each_child(*args, **kargs) { |path| paths << path } paths end |
#compressed? ⇒ Boolean
Returns [true|false] whether the file is compressed based on its file extensions.
154 155 156 157 |
# File 'lib/io_streams/path.rb', line 154 def compressed? # TODO: Look at streams? !(path =~ /\.(zip|gz|gzip|xls.|)\z/i).nil? end |
#copy_from(source, **args) ⇒ Object
Cleanup an incomplete write to the target “file” if the copy fails. rubocop:disable Lint/SuppressedException
86 87 88 89 90 91 92 93 94 |
# File 'lib/io_streams/path.rb', line 86 def copy_from(source, **args) super(source, **args) rescue StandardError => e begin delete rescue NotImplementedError end raise(e) end |
#delete ⇒ Object
When path is a file, deletes this file. When path is a directory, attempts to delete this directory. If the directory contains any children it will fail.
Returns self
Notes:
-
No error is raised if the file or directory is not present.
-
Only the file is removed, not any of the parent paths.
136 137 138 |
# File 'lib/io_streams/path.rb', line 136 def delete raise NotImplementedError end |
#delete_all ⇒ Object
When path is a directory ,deletes this directory and all its children. When path is a file ,deletes this file.
Returns self
Notes:
-
No error is raised if the file is not present.
-
Only the file is removed, not any of the parent paths.
-
All children paths and files will be removed.
149 150 151 |
# File 'lib/io_streams/path.rb', line 149 def delete_all raise NotImplementedError end |
#directory ⇒ Object
Returns [IOStreams::Path] the directory for this file.
If ‘path` does not include a directory name then “.” is returned.
IOStreams.path("test.rb").directory #=> "."
IOStreams.path("a/b/d/test.rb").directory #=> "a/b/d"
IOStreams.path(".a/b/d/test.rb").directory #=> ".a/b/d"
IOStreams.path("foo.").directory #=> "."
IOStreams.path("test").directory #=> "."
IOStreams.path(".profile").directory #=> "."
120 121 122 123 124 125 |
# File 'lib/io_streams/path.rb', line 120 def directory new_path = dup new_path.builder = nil new_path.path = ::File.dirname(path) new_path end |
#each_child(pattern = "*", **args, &block) ⇒ Object
Runs the pattern from the current path, returning the complete path for located files.
See IOStreams::Paths::File.each for arguments.
44 45 46 |
# File 'lib/io_streams/path.rb', line 44 def each_child(pattern = "*", **args, &block) raise NotImplementedError end |
#encrypted? ⇒ Boolean
Returns [true|false] whether the file is encrypted based on its file extensions.
160 161 162 163 |
# File 'lib/io_streams/path.rb', line 160 def encrypted? # TODO: Look at streams? !(path =~ /\.(enc|pgp|gpg)\z/i).nil? end |
#exist? ⇒ Boolean
Returns [true|false] whether the file exists
75 76 77 |
# File 'lib/io_streams/path.rb', line 75 def exist? raise NotImplementedError end |
#inspect ⇒ Object
191 192 193 194 195 196 |
# File 'lib/io_streams/path.rb', line 191 def inspect str = "#<#{self.class.name}:#{path}" str << " @builder=#{builder.streams.inspect}" if builder.streams str << " @options=#{builder..inspect}" if builder. str << " pipeline=#{pipeline.inspect}>" end |
#join(*elements) ⇒ Object
If elements already contains the current path then it is used as is without adding the current path for a second time
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/io_streams/path.rb', line 16 def join(*elements) return self if elements.empty? elements = elements.collect(&:to_s) relative = ::File.join(*elements) new_path = dup new_path.builder = nil new_path.path = relative.start_with?(path) ? relative : ::File.join(path, relative) new_path end |
#mkdir ⇒ Object
Assumes the current path does not include a file name, and creates all elements in the path. Returns self
Note: Do not call this method if the path contains a file name, see ‘#mkpath`
70 71 72 |
# File 'lib/io_streams/path.rb', line 70 def mkdir raise NotImplementedError end |
#mkpath ⇒ Object
Removes the last element of the path, the file name, before creating the entire path. Returns self
62 63 64 |
# File 'lib/io_streams/path.rb', line 62 def mkpath raise NotImplementedError end |
#move_to(target_path) ⇒ Object
Moves the file by copying it to the new path and then deleting the current path. Returns [IOStreams::Path] the target path.
Notes:
-
Currently only supports moving individual files, not directories.
102 103 104 105 106 107 108 |
# File 'lib/io_streams/path.rb', line 102 def move_to(target_path) target = IOStreams.new(target_path) target.mkpath target.copy_from(self, convert: false) delete target end |
#partial_files_visible? ⇒ Boolean
Returns [true|false] whether partially created files are visible on this path.
With local file systems a file that is still being written to is visbile. On AWS S3 a file is not visible until it is completely written to the bucket.
169 170 171 |
# File 'lib/io_streams/path.rb', line 169 def partial_files_visible? true end |
#realpath ⇒ Object
By default realpath just returns self.
37 38 39 |
# File 'lib/io_streams/path.rb', line 37 def realpath self end |
#relative? ⇒ Boolean
28 29 30 |
# File 'lib/io_streams/path.rb', line 28 def relative? !absolute? end |
#size ⇒ Object
Returns [Integer] size of the file
80 81 82 |
# File 'lib/io_streams/path.rb', line 80 def size raise NotImplementedError end |
#to_s ⇒ Object
Returns [String] the current path.
56 57 58 |
# File 'lib/io_streams/path.rb', line 56 def to_s path end |