Class: Fast::File

Inherits:
String
  • Object
show all
Defined in:
lib/fast/file.rb

Overview

File handling class.

Instance Method Summary collapse

Constructor Details

#initialize(source = nil) ⇒ File

Initializes the file



8
9
10
11
12
13
14
15
# File 'lib/fast/file.rb', line 8

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



35
36
37
38
# File 'lib/fast/file.rb', line 35

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



20
21
22
23
24
25
26
27
28
29
# File 'lib/fast/file.rb', line 20

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

Raises:

  • (ArgumentError)


241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/fast/file.rb', line 241

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



254
255
256
257
258
259
260
261
262
263
264
# File 'lib/fast/file.rb', line 254

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



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/fast/file.rb', line 58

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



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fast/file.rb', line 78

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

Returns:

  • (Boolean)


234
235
236
237
238
# File 'lib/fast/file.rb', line 234

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

Returns:

  • (Boolean)


121
122
123
124
# File 'lib/fast/file.rb', line 121

def exist? path = nil
  @path = normalize path if path
  do_check_existence @path
end

#exist_all?(*args) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
131
132
133
134
135
136
137
138
# File 'lib/fast/file.rb', line 128

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

Returns:

  • (Boolean)


140
141
142
143
144
145
146
147
148
149
150
# File 'lib/fast/file.rb', line 140

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

Raises:

  • (ArgumentError)


152
153
154
155
156
157
158
159
# File 'lib/fast/file.rb', line 152

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



169
170
171
172
# File 'lib/fast/file.rb', line 169

def expand path = nil
  @path = normalize path if path
  ::File.expand_path @path
end

#filterObject Also known as: by

Sends self to a SubSetter for Fast::File



162
163
164
# File 'lib/fast/file.rb', line 162

def filter
  SubSetter::Fast::File.new self
end

#merge(*args) ⇒ Object

Appends the contents of the target file into self and erase the target

Raises:

  • (Errno::ENOENT)


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/fast/file.rb', line 216

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

#pathObject

Returns the path to the current file



211
212
213
# File 'lib/fast/file.rb', line 211

def path
  @path if @path
end

#read(path = nil) ⇒ Object

Returns the contents of the file, all at once



115
116
117
118
# File 'lib/fast/file.rb', line 115

def read path = nil
  @path = normalize path if path
  ::File.read @path
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

Raises:

  • (ArgumentError)


178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/fast/file.rb', line 178

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



195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/fast/file.rb', line 195

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



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/fast/file.rb', line 99

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!



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fast/file.rb', line 42

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