Module: FunWith::Files::FileManipulationMethods

Defined in:
lib/fun_with/files/file_manipulation_methods.rb

Overview

Mostly just convenience methods for FileUtils

Instance Method Summary collapse

Instance Method Details

#cp(*args) ⇒ Object Also known as: copy

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?



29
30
31
32
33
34
# File 'lib/fun_with/files/file_manipulation_methods.rb', line 29

def cp( *args )
  destination_and_options( args ) do |dest, opts|
    FileUtils.cp_r( self, dest, ** Utils::Opts.narrow_file_utils_options( opts, :cp_r ) )
    dest.fwf_filepath
  end
end

#empty!Object



116
117
118
119
120
121
122
# File 'lib/fun_with/files/file_manipulation_methods.rb', line 116

def empty!
  if self.directory?
    FileUtils.rm_rf( self.entries, secure: true )
  else
    self.write( "" )
  end
end

#file_gsub(*args, &block) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/fun_with/files/file_manipulation_methods.rb', line 98

def file_gsub( *args, &block )
  must_be_file
  
  lines = []
  self.each_line do |line|
    lines << line.gsub( *args, &block )
  end
  
  lines.compact.join( "" )
end

#file_gsub!(*args, &block) ⇒ Object



109
110
111
112
113
114
# File 'lib/fun_with/files/file_manipulation_methods.rb', line 109

def file_gsub!( *args, &block )
  must_be_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.



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fun_with/files/file_manipulation_methods.rb', line 57

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
      FileUtils.ln self, lnk, ** Utils::Opts.narrow_file_utils_options( opts, :ln )
    end
    
    lnk.fwf_filepath
  end
end

#mv(dst, options = {}) ⇒ Object Also known as: move

Treat as a copy then a delete? Nah, that’s a lot slower especially for larger files. Should be much more in tune with what the command line program does. Treat it as syntactic sugar for FileUtils.mv? Also want to update the path to the new location - not implemented yet



43
44
45
46
47
# File 'lib/fun_with/files/file_manipulation_methods.rb', line 43

def mv( dst, options = {} )
  # what does FileUtils.rm actually return?  Glancing an the source, it
  # seems to only throw errors.
  FileUtils.mv( self, dst, **options )
end

#rename(filename) ⇒ Object

File manipulation



137
138
139
# File 'lib/fun_with/files/file_manipulation_methods.rb', line 137

def rename( filename )
  raise "NOT WORKING"
end

#rename_all(pattern, gsubbed) ⇒ Object



141
142
143
# File 'lib/fun_with/files/file_manipulation_methods.rb', line 141

def rename_all( pattern, gsubbed )
  raise "NOT WORKING"
end

#rm(secure = false) ⇒ Object

pass options?



146
147
148
149
150
151
152
# File 'lib/fun_with/files/file_manipulation_methods.rb', line 146

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?



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/fun_with/files/file_manipulation_methods.rb', line 82

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, ** Utils::Opts.narrow_file_utils_options( opts, :ln_s ) )
  lnk.fwf_filepath
end

#truncate(len = 0) ⇒ Object

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



126
127
128
129
130
131
132
133
134
# File 'lib/fun_with/files/file_manipulation_methods.rb', line 126

def truncate( len = 0 )
  must_be_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