module FunWith::Files::FileManipulationMethods

Mostly just convenience methods for FileUtils

Public Instance Methods

copy( *args )
Alias for: cp
cp( *args ) click to toggle source

opts are the last argument, and are passed to FileUtils.cp_r returns the destination path. How to detect failure? What to return on failure?

# File lib/fun_with/files/file_manipulation_methods.rb, line 23
def cp( *args )
  destination_and_options( args ) do |dest, opts|
    FileUtils.cp_r( self, dest, narrow_options( opts, FileUtils::OPT_TABLE["cp_r"] ) )
    dest.fwf_filepath
  end
end
Also aliased as: copy
empty!() click to toggle source
# File lib/fun_with/files/file_manipulation_methods.rb, line 106
def empty!
  if self.directory?
    FileUtils.rm_rf( self.entries, secure: true )
  else
    self.write( "" )
  end
end
file_gsub( *args, &block ) click to toggle source
# File lib/fun_with/files/file_manipulation_methods.rb, line 88
def file_gsub( *args, &block )
  _must_be_a_file
  
  lines = []
  self.each_line do |line|
    lines << line.gsub( *args, &block )
  end
  
  lines.compact.join( "" )
end
file_gsub!( *args, &block ) click to toggle source
# File lib/fun_with/files/file_manipulation_methods.rb, line 99
def file_gsub!( *args, &block )
  _must_be_a_file      # raises error
  _must_be_writable    # raises error
  
  self.write( self.file_gsub( *args, &block ) )
end
ln(*args)
Alias for: link
ln_s( *args )
Alias for: symlink
move( *args )
Alias for: mv
mv( *args ) click to toggle source

Treat as a copy then a delete? Nah, that's a lot slower in some cases. Should be much more in tune with what the command line program does

# File lib/fun_with/files/file_manipulation_methods.rb, line 33
def mv( *args )
  
end
Also aliased as: move
rename( filename ) click to toggle source

File manipulation

# File lib/fun_with/files/file_manipulation_methods.rb, line 127
def rename( filename )
  raise "NOT WORKING"
end
rename_all( pattern, gsubbed ) click to toggle source
# File lib/fun_with/files/file_manipulation_methods.rb, line 131
def rename_all( pattern, gsubbed )
  raise "NOT WORKING"
end
rm( secure = false ) click to toggle source

pass options?

# File lib/fun_with/files/file_manipulation_methods.rb, line 136
def rm( secure = false )
  if self.file?
    FileUtils.rm( self )
  elsif self.directory?
    FileUtils.rmtree( self )
  end
end
truncate( len ) click to toggle source

TODO: If it's truncated to a longer length than the original file, pad with zeros? That's how the UNIX truncate command works.

# File lib/fun_with/files/file_manipulation_methods.rb, line 116
def truncate( len )
  _must_be_a_file     # raises error
  _must_be_writable   # raises error
  
  old_size = self.size
  padding = len > old_size ? "\0" * (len - old_size) : ""
  
  self.write( self.read( len ) + padding )
end

Protected Instance Methods

_find_destination_from_args( args ) click to toggle source

logic should be shared by various manipulators

You can describe the destination as either a filepath or a bunch of strings for arguments. If the FilePath is relative, or if string args are given, then the destination will be relative to the path being copied (or in the case of a file, its parent directory).

If dest doesn't exist, and src (self) is a file, dest is taken to be the complete path. If dest doesn't exist, and src (self) is a directory, then dest is taken to be If dest is a directory and the source is a file, then the file will be copied into dest with the src's basename

# File lib/fun_with/files/file_manipulation_methods.rb, line 167
def _find_destination_from_args( args )
  raise ArgumentError.new("File #{self} must exist.") unless self.exist?
  
  if args.first.is_a?(Pathname) 
    raise ArgumentError.new( "accepts a FilePath or string args, not both" ) unless args.length == 1
    dest = args.first
    dest = dest.directory.join( dest ).expand if dest.relative?
  else
    dest = self.directory.join(*args).expand  # expand gets rid of /../ (parent_dir)
  end
  
  if self.file? && dest.directory?
    dest = dest.join( self.basename )
  elsif self.directory? && dest.file?
    raise ArgumentError.new( "cannot overwrite a file with a directory" )
  end
          
  dest
end
destination_and_options( args ) { |destination, options| ... } click to toggle source
# File lib/fun_with/files/file_manipulation_methods.rb, line 146
def destination_and_options( args, &block )
  options = args.last.is_a?(Hash) ? args.pop : {}
  destination = self._find_destination_from_args( args )
  
  if block_given?
    yield [destination, options]
  else
    [destination, options]
  end
end