Class: Runoff::FileWriter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db_handler) ⇒ FileWriter

Returns a new instance of FileWriter.



8
9
10
11
# File 'lib/runoff/file_writer.rb', line 8

def initialize(db_handler)
  @db_handler = db_handler
  @selected_entries = []
end

Instance Attribute Details

#selected_entriesObject

Returns the value of attribute selected_entries.



6
7
8
# File 'lib/runoff/file_writer.rb', line 6

def selected_entries
  @selected_entries
end

Instance Method Details

#export_database(data_format, export_path, create_archive) ⇒ Object

Public: Exports all the chats from the database.

data_format - an object that defines how the data should be parsed. export_path - a string that points to the directory where exported files must be saved.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/runoff/file_writer.rb', line 17

def export_database(data_format, export_path, create_archive)
  @export_path = export_path

  schema  = data_format.get_schema
  dataset = @db_handler[schema[:table]]
  dataset = dataset.select(*schema[:columns])

  dataset.each do |row|
    write data_format.build_entry(row)
  end

  archive unless create_archive == false
end

#export_database_partially(data_format, export_path, create_archive, &block) ⇒ Object

Public: Exports specific chats from the database.

data_format - an object that defines how the data should be parsed. export_path - a string that points to the directory where exported files must be saved.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/runoff/file_writer.rb', line 35

def export_database_partially(data_format, export_path, create_archive, &block)
  @export_path = export_path

  schema  = data_format.get_schema
  dataset = @db_handler[schema[:table]]
  indices = block.call self, dataset
  clean_indices = indices.split(',').map { |e| e.strip }

  clean_indices.each do |i|
    chatname = @selected_entries[i.to_i]
    approximation = data_format.denormalize chatname
    dataset = @db_handler[schema[:table]]
    dataset = dataset.where(Sequel.like(:chatname, "#{approximation}%"))

    dataset.each { |row| write data_format.build_entry(row) }
  end

  archive unless create_archive == false
end