class Gnomika::FileWriter

FileWriter is responsible for writing quotes to files

Public Class Methods

new(output_directory, single_file, single_file_name: "gnomika") click to toggle source

@param output_directory Path to directory that the files will be stored, must already exist @param single_file true if all quotes will be written in a single file @param single_file_name Name of the single file

# File lib/gnomikologikon/file_writer.rb, line 36
def initialize(output_directory, single_file, single_file_name: "gnomika")
  @output_directory = output_directory
  @single_file_mode = single_file
  @files_written = []
  # If single file output is specified, create the file now and use it in all future writes
  if single_file
    single_file_path = "#{output_directory}/#{single_file_name}"
    @single_file = File.new(single_file_path,File::CREAT|File::TRUNC|File::WRONLY)
    @files_written << single_file_path
  end
end

Public Instance Methods

generate_strfiles() click to toggle source

Runs the strfile command for every written file.

# File lib/gnomikologikon/file_writer.rb, line 67
def generate_strfiles
  until @files_written.empty?
    file_path = @files_written.shift
    system("strfile",file_path)
  end
end
write_quotes(subcategory_name, quotes) click to toggle source

Writes the given quotes to the file. @param quotes Array of quotes to write

# File lib/gnomikologikon/file_writer.rb, line 51
def write_quotes(subcategory_name, quotes)
  # If single file output is used, use the existing file. Otherwise create a new one for this category
  file = if @single_file_mode
           @single_file
         else
           # Write filename in files_written array
           new_file_name = "#{@output_directory}/#{subcategory_name}"
           @files_written << new_file_name
           File.new(new_file_name,File::CREAT|File::TRUNC|File::WRONLY)
         end
  # Write quotes separated by "%" sign
  file.write(quotes.join("\n%\n"))
end