Class: FileDb::System::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/file_db/system/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_directory) ⇒ Database

Returns a new instance of Database.



7
8
9
10
11
# File 'lib/file_db/system/database.rb', line 7

def initialize data_directory
  @data_directory = data_directory
  create_database_if_not_exist!
  load_tables!
end

Instance Attribute Details

#tablesObject

Returns the value of attribute tables.



5
6
7
# File 'lib/file_db/system/database.rb', line 5

def tables
  @tables
end

Instance Method Details

#check_table!(table_name) ⇒ Object



38
39
40
41
# File 'lib/file_db/system/database.rb', line 38

def check_table! table_name
  return if @tables[table_name]
  create_table_file! table_name
end

#create_database_if_not_exist!Object



25
26
27
28
# File 'lib/file_db/system/database.rb', line 25

def create_database_if_not_exist!
  return if exist_data_directory?
  create_data_directory!
end

#drop_data_directory!Object



30
31
32
# File 'lib/file_db/system/database.rb', line 30

def drop_data_directory!
  ::FileUtils.rm_rf @data_directory
end

#load_table!(filename) ⇒ Object



21
22
23
# File 'lib/file_db/system/database.rb', line 21

def load_table! filename
  @tables[cleared_tablename(filename)] = Table.new(filename, self)
end

#load_tables!Object



13
14
15
16
17
18
19
# File 'lib/file_db/system/database.rb', line 13

def load_tables!
  @tables = {}
  Dir[File.join(@data_directory, "*.csv")].each do |filename|
    next unless File.file?(filename)
    load_table! filename
  end
end

#save_to_disk(table, content) ⇒ Object



34
35
36
# File 'lib/file_db/system/database.rb', line 34

def save_to_disk table, content
  File.write(table.filename, content)
end