module FunWith::Files::FileManipulationMethods
Mostly just convenience methods for FileUtils
Public Instance Methods
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
# 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 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 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
Logic of link()
self is the target, link is the filepath entry linking to the file represented by self returns filepath of the new link. Will fall back to symbolic link if self is a directory. Necessary directories will be created.
# File lib/fun_with/files/file_manipulation_methods.rb, line 45 def link *args self.destination_and_options( args ) do |lnk, opts| symlink_requested = self.directory? || opts[:symbolic] || opts[:sym] || opts[:soft] if symlink_requested self.symlink lnk, opts else opts = narrow_options opts, FileUtils::OPT_TABLE["ln"] FileUtils.ln self, lnk, opts end lnk.fwf_filepath end end
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
File manipulation
# File lib/fun_with/files/file_manipulation_methods.rb, line 127 def rename( filename ) raise "NOT WORKING" end
# File lib/fun_with/files/file_manipulation_methods.rb, line 131 def rename_all( pattern, gsubbed ) raise "NOT WORKING" end
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
-
Where does the symlink live in the filesys.
-
What does it point to?
-
How does it point to the thing?
* absolutely * relatively * custom string (programmer error hilarity ensues?)
It can't What to return? The path of the symlink, or the path of the target?
# File lib/fun_with/files/file_manipulation_methods.rb, line 72 def symlink( *args ) lnk, opts = self.destination_and_options( args ) if opts[:absolute] lnk = lnk.fwf_filepath.expand else lnk = lnk.fwf_filepath end FileUtils.ln_s( self, lnk, narrow_options( opts, FileUtils::OPT_TABLE["ln_s"] ) ) lnk.fwf_filepath end
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
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
# 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