class IOStreams::Path

Attributes

path[RW]

Public Class Methods

new(path) click to toggle source
# 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

Public Instance Methods

<=>(other) click to toggle source

Paths are sortable by name

# File lib/io_streams/path.rb, line 182
def <=>(other)
  path <=> other.path
end
==(other) click to toggle source

Compare by path name, ignore streams

# File lib/io_streams/path.rb, line 187
def ==(other)
  path == other.path
end
absolute?() click to toggle source
# File lib/io_streams/path.rb, line 32
def absolute?
  !!(path.strip =~ %r{\A/})
end
children(*args, **kargs) click to toggle source

Returns [Array] of child files based on the supplied pattern

# File lib/io_streams/path.rb, line 49
def children(*args, **kargs)
  paths = []
  each_child(*args, **kargs) { |path| paths << path }
  paths
end
compressed?() click to toggle source

Returns [true|false] whether the file is compressed based on its file extensions.

# File lib/io_streams/path.rb, line 154
def compressed?
  # TODO: Look at streams?
  !(path =~ /\.(zip|gz|gzip|xlsx|xlsm|bz2)\z/i).nil?
end
copy_from(source, **args) click to toggle source

Cleanup an incomplete write to the target “file” if the copy fails. rubocop:disable Lint/SuppressedException

Calls superclass method IOStreams::Stream#copy_from
# 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() click to toggle source

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.

# File lib/io_streams/path.rb, line 136
def delete
  raise NotImplementedError
end
delete_all() click to toggle source

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.

# File lib/io_streams/path.rb, line 149
def delete_all
  raise NotImplementedError
end
directory() click to toggle source

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        #=> "."
# 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) click to toggle source

Runs the pattern from the current path, returning the complete path for located files.

See IOStreams::Paths::File.each for arguments.

# File lib/io_streams/path.rb, line 44
def each_child(pattern = "*", **args, &block)
  raise NotImplementedError
end
encrypted?() click to toggle source

Returns [true|false] whether the file is encrypted based on its file extensions.

# File lib/io_streams/path.rb, line 160
def encrypted?
  # TODO: Look at streams?
  !(path =~ /\.(enc|pgp|gpg)\z/i).nil?
end
exist?() click to toggle source

Returns [true|false] whether the file exists

# File lib/io_streams/path.rb, line 75
def exist?
  raise NotImplementedError
end
inspect() click to toggle source
# 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.options.inspect}" if builder.options
  str << " pipeline=#{pipeline.inspect}>"
end
join(*elements) click to toggle source

If elements already contains the current path then it is used as is without adding the current path for a second time

# 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() click to toggle source

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`

# File lib/io_streams/path.rb, line 70
def mkdir
  raise NotImplementedError
end
mkpath() click to toggle source

Removes the last element of the path, the file name, before creating the entire path. Returns self

# File lib/io_streams/path.rb, line 62
def mkpath
  raise NotImplementedError
end
move_to(target_path) click to toggle source

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.

# 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?() click to toggle source

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.

# File lib/io_streams/path.rb, line 169
def partial_files_visible?
  true
end
realpath() click to toggle source

By default realpath just returns self.

# File lib/io_streams/path.rb, line 37
def realpath
  self
end
relative?() click to toggle source
# File lib/io_streams/path.rb, line 28
def relative?
  !absolute?
end
size() click to toggle source

Returns [Integer] size of the file

# File lib/io_streams/path.rb, line 80
def size
  raise NotImplementedError
end
to_s() click to toggle source

Returns [String] the current path.

# File lib/io_streams/path.rb, line 56
def to_s
  path
end

Private Instance Methods

builder() click to toggle source
# File lib/io_streams/path.rb, line 200
def builder
  @builder ||= IOStreams::Builder.new(path)
end