Class: File

Inherits:
Object
  • Object
show all
Defined in:
lib/helpers/file_extension.rb

Class Method Summary collapse

Class Method Details

.add_to_file_data(path, data) ⇒ Object



31
32
33
34
35
# File 'lib/helpers/file_extension.rb', line 31

def self.add_to_file_data(path, data)
  File.open(path, "a") do |f|
    f.write(data)
  end
end

.create_directory(path: string) ⇒ Object



21
22
23
# File 'lib/helpers/file_extension.rb', line 21

def self.create_directory(path: string)
  FileUtils.mkdir_p(path) unless Dir.exist?(path)
end

.create_new_file_with_data(path, data) ⇒ Object



25
26
27
28
29
# File 'lib/helpers/file_extension.rb', line 25

def self.create_new_file_with_data(path, data)
  File.open(path, "w") do |f|
    f.write(data)
  end
end

.create_or_add_to_file_data(path, data) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/helpers/file_extension.rb', line 37

def self.create_or_add_to_file_data(path, data)
  if exist?(path)
    data = data.empty? ? "" : "\n#{data}"
    add_to_file_data(path, data)
  else
    create_new_file_with_data(path, data)
  end
end

.delete_directory(path: string) ⇒ Object



9
10
11
12
13
# File 'lib/helpers/file_extension.rb', line 9

def self.delete_directory(path: string)
  return unless Dir.exist?(path)

  FileUtils.rm_r(path)
end

.delete_file(path: string) ⇒ Object



15
16
17
18
19
# File 'lib/helpers/file_extension.rb', line 15

def self.delete_file(path: string)
  return unless exist?(path)

  delete(path)
end

.open_file_with_data(path, data) ⇒ Object



53
54
55
56
57
# File 'lib/helpers/file_extension.rb', line 53

def self.open_file_with_data(path, data)
  File.open(path, "w") do |f|
    f.write(data)
  end
end

.read_file(path) ⇒ Object



46
47
48
49
50
51
# File 'lib/helpers/file_extension.rb', line 46

def self.read_file(path)
  File.open(path, "r") do |f|
    data = f.read.to_s
    return data || ""
  end
end

.write_to_file(path, data) ⇒ Object



5
6
7
# File 'lib/helpers/file_extension.rb', line 5

def self.write_to_file(path, data)
  open_file_with_data(path, data)
end