Class: Fast::File
- Inherits:
-
String
- Object
- String
- Fast::File
- Defined in:
- lib/fast/file.rb
Overview
File handling class.
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: #destroy, #unlink, #del)
Deletes the files (wrapper for ‘File.unlink <path>`) Fails if file does not exist.
-
#delete!(*args) ⇒ Object
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?(path = nil) ⇒ Boolean
(also: #exists?)
Returns true if file exists, false otherwise.
- #exist_all?(*args) ⇒ Boolean
- #exist_any?(*args) ⇒ Boolean
- #exist_which(*args) ⇒ Object
-
#expand(path = nil) ⇒ Object
(also: #absolute)
Expands the path if it’s a relative path.
-
#filter ⇒ Object
(also: #by)
Sends self to a FileFilter filter.
-
#initialize(source = nil) ⇒ File
constructor
Initializes the file.
-
#merge(*args) ⇒ Object
Appends the contents of the target file into self and erase the target.
-
#path ⇒ Object
Returns the path to the current file.
-
#read(path = nil) ⇒ Object
Returns the contents of the file, all at once.
-
#rename(*args) ⇒ Object
Renames the file (by Fast::File own means, it does not call the underlying OS).
-
#rename!(*args) ⇒ Object
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.
Constructor Details
#initialize(source = nil) ⇒ File
Initializes the file
6 7 8 9 10 11 12 13 |
# File 'lib/fast/file.rb', line 6 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
33 34 35 36 |
# File 'lib/fast/file.rb', line 33 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
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/fast/file.rb', line 18 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
229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/fast/file.rb', line 229 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
242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/fast/file.rb', line 242 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: destroy, unlink, del
Deletes the files (wrapper for ‘File.unlink <path>`) Fails if file does not exist
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/fast/file.rb', line 56 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
Deletes the file(s) if it exists, does nothing otherwise
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/fast/file.rb', line 75 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
222 223 224 225 226 |
# File 'lib/fast/file.rb', line 222 def empty? path = nil @path = normalize path if path return true if not exist? read.empty? end |
#exist?(path = nil) ⇒ Boolean Also known as: exists?
Returns true if file exists, false otherwise
113 114 115 116 |
# File 'lib/fast/file.rb', line 113 def exist? path = nil @path = normalize path if path do_check_existence @path end |
#exist_all?(*args) ⇒ Boolean
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/fast/file.rb', line 120 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
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/fast/file.rb', line 132 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
144 145 146 147 148 149 150 151 |
# File 'lib/fast/file.rb', line 144 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 |
#expand(path = nil) ⇒ Object Also known as: absolute
Expands the path if it’s a relative path
161 162 163 164 |
# File 'lib/fast/file.rb', line 161 def path = nil @path = normalize path if path ::File. @path end |
#filter ⇒ Object Also known as: by
Sends self to a FileFilter filter
154 155 156 |
# File 'lib/fast/file.rb', line 154 def filter FileFilter.new self end |
#merge(*args) ⇒ Object
Appends the contents of the target file into self and erase the target
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/fast/file.rb', line 204 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 |
#path ⇒ Object
Returns the path to the current file
199 200 201 |
# File 'lib/fast/file.rb', line 199 def path @path if @path end |
#read(path = nil) ⇒ Object
Returns the contents of the file, all at once
107 108 109 110 |
# File 'lib/fast/file.rb', line 107 def read path = nil @path = normalize path if path ::File.read @path end |
#rename(*args) ⇒ Object
Renames the file (by Fast::File own means, it does not call the underlying OS). Fails if the new path is an existent file
170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/fast/file.rb', line 170 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
Like #rename, but overwrites the new file if is exist
185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/fast/file.rb', line 185 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
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/fast/file.rb', line 91 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!
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/fast/file.rb', line 40 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 |