Module: F

Defined in:
lib/f.rb

Overview

Provides a wrapper around common calls that interact with the file system.

Class Method Summary collapse

Class Method Details

.concat_files(source_path, destination_path) ⇒ Object

Concatenates together the contents of all the files in the source_path into the destination_path.



10
11
12
13
14
15
16
# File 'lib/f.rb', line 10

def concat_files(source_path, destination_path)
  File.open(destination_path, "a") do |output|
    Dir[source_path].each do |path|
      output.puts File.read(path)
    end
  end
end

.execute(command, params = {}) ⇒ Object

Execute a system command. If the parameter :return is true, execute the command with the backtick (‘) command and return the results. Otherwise, just execute the command and let the output go to the screen.



56
57
58
59
60
61
62
# File 'lib/f.rb', line 56

def execute(command, params={})
  if params[:return]
    `#{command}`
  else
    Kernel.system command
  end
end

.get_line_from_file(path, line_number) ⇒ Object

Given a path and line_number, returns the line and two lines previous

Used for displaying validation errors.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/f.rb', line 38

def get_line_from_file(path, line_number)
  line_number = line_number.to_i
  output = "\n"
  lines = File.readlines(path)

  3.times do |i|
    line = lines[line_number-(3-i)]
    output += line if line
  end

  output += "\n"
  output
end

.save_to_file(output, destination_path) ⇒ Object

Saves the output string to the destination_path given.

Returns true if the destination file was newly created, false if it already existed.



23
24
25
26
27
28
29
30
31
32
# File 'lib/f.rb', line 23

def save_to_file(output, destination_path)
  if File.exists?(destination_path)      
    false
  else
    File.open(destination_path, "w") do |w|
      w.write(output)
    end
    true
  end
end