Class: Gnomika::FileWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/gnomikologikon/file_writer.rb

Overview

FileWriter is responsible for writing quotes to files

Instance Method Summary collapse

Constructor Details

#initialize(output_directory, single_file, single_file_name: "gnomika") ⇒ FileWriter

Returns a new instance of FileWriter.

Parameters:

  • output_directory

    Path to directory that the files will be stored, must already exist

  • single_file

    true if all quotes will be written in a single file

  • single_file_name (defaults to: "gnomika")

    Name of the single file



36
37
38
39
40
41
42
43
44
45
46
# 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

Instance Method Details

#generate_strfilesObject

Runs the strfile command for every written file.



67
68
69
70
71
72
# 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) ⇒ Object

Writes the given quotes to the file.

Parameters:

  • quotes

    Array of quotes to write



51
52
53
54
55
56
57
58
59
60
61
62
63
# 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