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 same as for FileUtils.cp_r returns the destination path. How to detect failure? What to return on failure?



21
22
23
24
25
# File 'lib/fun_with/files/file_manipulation_methods.rb', line 21

def cp( *args )
  dest, opts = self.destination_and_options( args )
  FileUtils.cp_r( self, dest, opts )
  dest.fwf_filepath
end

#empty!Object



68
69
70
71
72
73
74
# File 'lib/fun_with/files/file_manipulation_methods.rb', line 68

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

#file_gsub(*args, &block) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/fun_with/files/file_manipulation_methods.rb', line 55

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

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



64
65
66
# File 'lib/fun_with/files/file_manipulation_methods.rb', line 64

def file_gsub!( *args, &block )
  self.write( self.file_gsub(*args,&block) )
end

#ln(*args) ⇒ Object

self is the target, link is the thing linking to self returns filepath of the new link. Will fall back to symbolic link if self is a directory



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fun_with/files/file_manipulation_methods.rb', line 32

def ln( *args )
  self.destination_and_options( args ) do |link, opts|
    symlink = self.directory? || opts[:symbolic] || opts[:sym] || opts[:soft]
  
    if symlink
      FileUtils.ln_s( self, link, opts )
    else
      FileUtils.ln( self, link, opts )
    end
  
    link.fwf_filepath
  end
end

#ln_s(*args) ⇒ Object Also known as: symlink



46
47
48
49
50
# File 'lib/fun_with/files/file_manipulation_methods.rb', line 46

def ln_s( *args )
  link, opts = self.destination_and_options( args )
  FileUtils.ln_s( self, link, opts )
  link.fwf_filepath
end

#truncate(len) ⇒ Object



76
77
78
# File 'lib/fun_with/files/file_manipulation_methods.rb', line 76

def truncate( len )
  self.write( self.read( len ) )
end