Module: CsvRecord::Connector

Defined in:
lib/csv_record/connector.rb

Constant Summary collapse

DATABASE_FOLDER =
'db'.freeze
APPEND_MODE =
'a'.freeze
WRITE_MODE =
'wb'.freeze
READ_MODE =
'r'.freeze

Instance Method Summary collapse

Instance Method Details

#__initialize_db__Object Also known as: initialize_db

Initialize the database file with its headers



15
16
17
18
19
20
21
22
# File 'lib/csv_record/connector.rb', line 15

def __initialize_db__
  __initialize_db_directory__
  unless db_initialized?
    open_database_file WRITE_MODE do |csv|
      csv << doppelganger_fields
    end
  end
end

#__initialize_db_directory__Object Also known as: initialize_db_directory

Checks wheter the database directory exists



8
9
10
11
12
# File 'lib/csv_record/connector.rb', line 8

def __initialize_db_directory__
  unless Dir.exists? DATABASE_FOLDER
    Dir.mkdir DATABASE_FOLDER
  end
end

#__open_database_file__(mode = READ_MODE) ⇒ Object Also known as: open_database_file

Open the database file Params:

mode

the operation mode (defaults to READ_MODE)



32
33
34
35
36
37
38
# File 'lib/csv_record/connector.rb', line 32

def __open_database_file__(mode=READ_MODE)
  __initialize_db__ if mode == READ_MODE # fix this later
  db_location = self.const_get('DATABASE_LOCATION')
  CSV.open(db_location, mode, headers: true) do |csv|
    yield csv
  end
end

#__parse_database_file__Object Also known as: parse_database_file

Creates a modified copy of the database file with the new data and then replaces the original



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/csv_record/connector.rb', line 41

def __parse_database_file__
  open_database_file do |csv|
    CSV.open(self.const_get('DATABASE_LOCATION_TMP'), WRITE_MODE, headers: true) do |copy|
      copy << fields
      csv.entries.each do |entry|
        new_row = yield entry
        copy << new_row if new_row
      end
    end
  end
  rename_database
end

#db_initialized?Boolean

Checks wheter the database file exists

Returns:

  • (Boolean)


25
26
27
# File 'lib/csv_record/connector.rb', line 25

def db_initialized?
  File.exist? self.const_get 'DATABASE_LOCATION'
end