Class: File

Inherits:
Object show all
Defined in:
lib/ruuuby/class/io/file.rb

Overview

add various aliases/functions to existing class File, (aliased globally by Kernel‘s function+📁+)

Defined Under Namespace

Modules: CSV, YAML

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.insert_line_before_expr(the_path, the_line, expression) ⇒ Object

Parameters:



36
37
38
39
# File 'lib/ruuuby/class/io/file.rb', line 36

def self.insert_line_before_expr(the_path, the_line, expression)
  🛑strs❓([the_path, the_line, expression], :∉∅)
  ::File.replace_expr_with!(the_path, expression, "#{the_line}\n#{expression}", 1)
end

.insert_lines_before_expr(the_path, the_lines, expression) ⇒ Object

Parameters:



44
45
46
47
48
49
50
51
52
# File 'lib/ruuuby/class/io/file.rb', line 44

def self.insert_lines_before_expr(the_path, the_lines, expression)
  🛑strs❓([the_path, expression], :∉∅)
  🛑ary❓('the_lines', the_lines)
  combined_lines = ''
  the_lines.each do |line|
    combined_lines << "#{line}\n"
  end
  ::File.replace_expr_with!(the_path, expression, "#{combined_lines}#{expression}", 1)
end

.replace_expr_with(the_path, expression, replacement, num_matches = 1) ⇒ Integer

original source referenced from:

Parameters:

Returns:

  • (Integer)

    -1 if an error occurred, 0 if there were no matches, otherwise n, (a positive int), for number of matches

See Also:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ruuuby/class/io/file.rb', line 63

def self.replace_expr_with(the_path, expression, replacement, num_matches=1)
  🛑strs❓([the_path, expression, replacement], :∉∅)
  🛑int❓('num_matches', num_matches, :∈ℕ)
  num_matched = 0

  # TODO: NEED TO OPEN THE FILES WITH ENCODING SPECIFIED

  ::Tempfile.open(".#{::File.basename(the_path)}", ::File.dirname(the_path)) do |temp_file|
    ::File.open(the_path).each do |line|
      if num_matched < num_matches
        if line.∋?(expression)
          temp_file.puts(line.gsub(expression, replacement))
          num_matched += 1
        else
          temp_file.puts(line)
        end
      else
        temp_file.puts(line)
      end
    end
    temp_file.fdatasync
    temp_file.close
    if num_matched > 0
      stats = ::File.stat(the_path)
      ::FileUtils.chown(stats.uid, stats.gid, temp_file.path)
      ::FileUtils.chmod(stats.mode, temp_file.path)
      ::FileUtils.mv(temp_file.path, the_path)
    else
      temp_file.delete
    end
  end
  return num_matched
end

.replace_expr_with!(the_path, expression, replacement, num_matches) ⇒ Integer

Returns 0 if there were no matches, otherwise n, (a positive int), for number of matches.

Parameters:

Returns:

  • (Integer)

    0 if there were no matches, otherwise n, (a positive int), for number of matches

Raises:

  • (RuntimeError)

    if the expression provided was not found in any line of the file provided



105
106
107
108
109
110
111
112
# File 'lib/ruuuby/class/io/file.rb', line 105

def self.replace_expr_with!(the_path, expression, replacement, num_matches)
  num_matched = ::File.replace_expr_with(the_path, expression, replacement, num_matches)
  if num_matched == 0
    🛑 ::RuntimeError.🆕("| c{File}-> m{replace_expr_with!} did not end up replacing any content |")
  else
    num_matched
  end
end

.∃?(path) ⇒ Boolean

TODO: missing coverage

Parameters:

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


26
27
28
29
# File 'lib/ruuuby/class/io/file.rb', line 26

def self.∃?(path)
  🛑str❓('path', path)
  ::File.file?(path)
end

Instance Method Details

#∅?Boolean

Returns true, if this file does not exist or has zero contents.

Returns:

  • (Boolean)

    true, if this file does not exist or has zero contents



15
# File 'lib/ruuuby/class/io/file.rb', line 15

def ∅?; ::File.∅?(self.path); end