module CsvRecord::Connector

Constants

APPEND_MODE
DATABASE_FOLDER
READ_MODE
WRITE_MODE

Public Instance Methods

__initialize_db__() click to toggle source

Initialize the database file with its headers

# File lib/csv_record/connector.rb, line 17
def __initialize_db__
  __initialize_db_directory__
  unless db_initialized?
    open_database_file(WRITE_MODE) do |csv|
      csv << doppelganger_fields
    end
  end
end
Also aliased as: initialize_db
__initialize_db_directory__() click to toggle source

Checks wheter the database directory exists

# File lib/csv_record/connector.rb, line 10
def __initialize_db_directory__
  unless Dir.exist?(DATABASE_FOLDER)
    Dir.mkdir DATABASE_FOLDER
  end
end
Also aliased as: initialize_db_directory
__open_database_file__(mode=READ_MODE) { |csv| ... } click to toggle source

Open the database file Params:

mode

the operation mode (defaults to READ_MODE)

# File lib/csv_record/connector.rb, line 34
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
Also aliased as: open_database_file
__parse_database_file__() { |entry| ... } click to toggle source

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

# File lib/csv_record/connector.rb, line 43
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
Also aliased as: parse_database_file
db_initialized?() click to toggle source

Checks wheter the database file exists

# File lib/csv_record/connector.rb, line 27
def db_initialized?
  File.exist? self.const_get('DATABASE_LOCATION')
end

Private Instance Methods

initialize_db()
Alias for: __initialize_db__
initialize_db_directory()
open_database_file(mode=READ_MODE)
parse_database_file()
rename_database() click to toggle source

Rename the TMP database file to replace the original

# File lib/csv_record/connector.rb, line 59
def rename_database
  old_file = self.const_get 'DATABASE_LOCATION'
  tmp_file = self.const_get 'DATABASE_LOCATION_TMP'
  while not File.exist?(old_file) ; sleep(10) ; end
  File.delete old_file
  File.rename tmp_file, old_file
end