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
-
#cp(*args) ⇒ Object
(also: #copy)
opts are the last argument, and are passed to FileUtils.cp_r returns the destination path.
- #empty! ⇒ Object
- #file_gsub(*args, &block) ⇒ Object
- #file_gsub!(*args, &block) ⇒ Object
-
#link(*args) ⇒ Object
(also: #ln)
Logic of link().
-
#mv(*args) ⇒ Object
(also: #move)
Treat as a copy then a delete? Nah, that’s a lot slower in some cases.
-
#rename(filename) ⇒ Object
File manipulation.
- #rename_all(pattern, gsubbed) ⇒ Object
-
#rm(secure = false) ⇒ Object
pass options?.
-
#symlink(*args) ⇒ Object
(also: #ln_s)
-
Where does the symlink live in the filesys.
-
-
#truncate(len) ⇒ 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.
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?
23 24 25 26 27 28 |
# File 'lib/fun_with/files/file_manipulation_methods.rb', line 23 def cp( *args ) ( args ) do |dest, opts| FileUtils.cp_r( self, dest, ( opts, FileUtils::OPT_TABLE["cp_r"] ) ) dest.fwf_filepath end end |
#empty! ⇒ Object
106 107 108 109 110 111 112 |
# 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) ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# 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) ⇒ Object
99 100 101 102 103 104 |
# 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 |
#link(*args) ⇒ Object Also known as: ln
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.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/fun_with/files/file_manipulation_methods.rb', line 45 def link *args self.( args ) do |lnk, opts| symlink_requested = self.directory? || opts[:symbolic] || opts[:sym] || opts[:soft] if symlink_requested self.symlink lnk, opts else opts = opts, FileUtils::OPT_TABLE["ln"] FileUtils.ln self, lnk, opts end lnk.fwf_filepath end end |
#mv(*args) ⇒ Object Also known as: move
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
33 34 35 |
# File 'lib/fun_with/files/file_manipulation_methods.rb', line 33 def mv( *args ) end |
#rename(filename) ⇒ Object
File manipulation
127 128 129 |
# File 'lib/fun_with/files/file_manipulation_methods.rb', line 127 def rename( filename ) raise "NOT WORKING" end |
#rename_all(pattern, gsubbed) ⇒ Object
131 132 133 |
# File 'lib/fun_with/files/file_manipulation_methods.rb', line 131 def rename_all( pattern, gsubbed ) raise "NOT WORKING" end |
#rm(secure = false) ⇒ Object
pass options?
136 137 138 139 140 141 142 |
# 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 |
#symlink(*args) ⇒ Object Also known as: ln_s
-
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?
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/fun_with/files/file_manipulation_methods.rb', line 72 def symlink( *args ) lnk, opts = self.( args ) if opts[:absolute] lnk = lnk.fwf_filepath. else lnk = lnk.fwf_filepath end FileUtils.ln_s( self, lnk, ( opts, FileUtils::OPT_TABLE["ln_s"] ) ) lnk.fwf_filepath end |
#truncate(len) ⇒ 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.
116 117 118 119 120 121 122 123 124 |
# 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 |