Class: Fast::File
- Inherits:
-
String
- Object
- String
- Fast::File
- Includes:
- FilesystemObject
- Defined in:
- lib/fast/file.rb
Overview
File handling class.
Inherits from String in order to be usable as a String Includes the module Fast::FilesystemObject for common functionality with Fast::Dir
Instance Method Summary collapse
-
#<<(content) ⇒ Object
Appends the passed content to the file Creates the file if it doesn’t exist.
-
#append(*args) ⇒ Object
Appends the passed content to the file ‘path` Creates the file if it doesn’t exist.
-
#copy(*args) ⇒ Object
Copies current file into target file.
- #copy!(*args) ⇒ Object
-
#delete(*args) ⇒ Object
(also: #remove, #destroy, #del, #unlink)
Deletes the files (wrapper for ‘File.unlink <path>`) Fails if file does not exist.
-
#delete!(*args) ⇒ Object
(also: #remove!, #destroy!, #del!, #unlink!)
Deletes the file(s) if it exists, does nothing otherwise.
-
#empty?(path = nil) ⇒ Boolean
Returns true if the file is empty or does not exist.
- #exist_all?(*args) ⇒ Boolean
- #exist_any?(*args) ⇒ Boolean
- #exist_which(*args) ⇒ Object
-
#initialize(source = nil) ⇒ File
constructor
Initializes the file.
-
#merge(*args) ⇒ Object
Appends the contents of the target file into self and erase the target.
-
#read(path = nil, &block) ⇒ Object
Returns the contents of the file, all at once.
-
#rename(*args) ⇒ Object
(also: #move)
Renames the file (by Fast::File own means, it does not call the underlying OS).
-
#rename!(*args) ⇒ Object
(also: #move!)
Like #rename, but overwrites the new file if is exist.
-
#touch(*args) ⇒ Object
(also: #create, #create!)
Touches the file passed.
-
#write(*args) ⇒ Object
Writes data into the file.
Methods included from FilesystemObject
#exist?, #expand, #normalize, #path
Constructor Details
#initialize(source = nil) ⇒ File
Initializes the file
13 14 15 16 17 18 19 20 |
# File 'lib/fast/file.rb', line 13 def initialize source = nil unless source.nil? super( "#{source}" ) @path = normalize source else super() end end |
Instance Method Details
#<<(content) ⇒ Object
Appends the passed content to the file Creates the file if it doesn’t exist. Creates all the necesary folders if they don’t exist Fails if file path is not defined
40 41 42 43 |
# File 'lib/fast/file.rb', line 40 def << content raise "No path specified in the file" unless @path do_append content end |
#append(*args) ⇒ Object
Appends the passed content to the file ‘path` Creates the file if it doesn’t exist. Creates all the necesary folders if they don’t exist
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/fast/file.rb', line 25 def append *args if args.length > 1 path, content = *args @path = normalize path else content = args.first end do_append content end |
#copy(*args) ⇒ Object
Copies current file into target file. Does not rely on OS nor FileUtils
224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/fast/file.rb', line 224 def copy *args if args.length > 1 current, target = *args @path = normalize current target = File.new target else target = File.new args.first end raise ArgumentError, "Target '#{target.path}' already exists." if target.exist? do_copy target end |
#copy!(*args) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/fast/file.rb', line 237 def copy! *args if args.length > 1 current, target = *args @path = normalize current target = File.new target else target = File.new args.first end do_copy target end |
#delete(*args) ⇒ Object Also known as: remove, destroy, del, unlink
Deletes the files (wrapper for ‘File.unlink <path>`) Fails if file does not exist
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/fast/file.rb', line 63 def delete *args unless args.empty? return_me = nil args.each do |path| return_me = normalize path ::File.unlink return_me end return return_me else ::File.unlink @path @path end end |
#delete!(*args) ⇒ Object Also known as: remove!, destroy!, del!, unlink!
Deletes the file(s) if it exists, does nothing otherwise
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/fast/file.rb', line 83 def delete! *args unless args.empty? return_me = nil args.each do |path| return_me = normalize path ::File.unlink return_me if File.new.exist? path end return return_me else ::File.unlink @path if exist? @path end end |
#empty?(path = nil) ⇒ Boolean
Returns true if the file is empty or does not exist
217 218 219 220 221 |
# File 'lib/fast/file.rb', line 217 def empty? path = nil @path = normalize path if path return true if not exist? read.empty? end |
#exist_all?(*args) ⇒ Boolean
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/fast/file.rb', line 131 def exist_all? *args unless args.empty? return_me = true args.each do |path| return_me &= do_check_existence path end return return_me else do_check_existence @path end end |
#exist_any?(*args) ⇒ Boolean
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/fast/file.rb', line 143 def exist_any? *args unless args.empty? return_me = false args.each do |path| return_me |= do_check_existence path end return return_me else do_check_existence @path end end |
#exist_which(*args) ⇒ Object
155 156 157 158 159 160 161 162 |
# File 'lib/fast/file.rb', line 155 def exist_which *args raise ArgumentError, "Wrong number of arguments (at least one is needed)" if args.empty? return_list = [] args.each do |path| return_list << path if do_check_existence path end return_list end |
#merge(*args) ⇒ Object
Appends the contents of the target file into self and erase the target
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/fast/file.rb', line 199 def merge *args if args.length > 1 source, target = *args @path = normalize source target = File.new target else target = File.new args.first end raise Errno::ENOENT, "No such file - #{@path}" unless exist? raise Errno::ENOENT, "No such file - #{target.path}" unless target.exist? append target.read target.delete! self end |
#read(path = nil, &block) ⇒ Object
Returns the contents of the file, all at once
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/fast/file.rb', line 120 def read path = nil, &block @path = normalize path if path unless block ::File.read @path else ::File.open @path, "r" do |the_file| block.call the_file end end end |
#rename(*args) ⇒ Object Also known as: move
Renames the file (by Fast::File own means, it does not call the underlying OS). Fails if the new path is an existent file
166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/fast/file.rb', line 166 def rename *args if args.length > 1 path, new_path = *args @path = normalize path new_path = normalize new_path else new_path = normalize args.first end raise ArgumentError, "The file '#{new_path}' already exists" if File.new.exists? new_path renamed = File.new.write new_path, self.read self.delete! return renamed end |
#rename!(*args) ⇒ Object Also known as: move!
Like #rename, but overwrites the new file if is exist
183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/fast/file.rb', line 183 def rename! *args if args.length > 1 path, new_path = *args @path = normalize path new_path = normalize new_path else new_path = normalize args.first end renamed = File.new.write new_path, self.read self.delete! return renamed end |
#touch(*args) ⇒ Object Also known as: create, create!
Touches the file passed. Like bash ‘touch`, but creates all required directories if they don’t exist
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/fast/file.rb', line 104 def touch *args if args.length > 0 return_me = nil args.each do |path| return_me = do_create path end return return_me else do_create @path end end |
#write(*args) ⇒ Object
Writes data into the file. If is does not exist, creates it if it already exists, overwrites it!
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/fast/file.rb', line 47 def write *args if args.length > 1 path, content = *args @path = normalize path else content = args.first end Fast::Dir.new.create! ::File.dirname @path if ::File.dirname(@path) != "." ::File.open @path, "w" do |handler| handler.write content end self end |