Class: FileDb::System::Table

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, database) ⇒ Table

Returns a new instance of Table.



5
6
7
8
9
10
11
12
13
14
# File 'lib/file_db/system/table.rb', line 5

def initialize filename, database
  @filename = filename
  @database = database
  @specific_records = {}
  @entries_index_by = {}
  @entries_index_by[:id] = {}
  @fields = {}
  @fieldnames = {}
  read_rows!
end

Instance Attribute Details

#entries_index_byObject

Returns the value of attribute entries_index_by.



4
5
6
# File 'lib/file_db/system/table.rb', line 4

def entries_index_by
  @entries_index_by
end

#filenameObject

Returns the value of attribute filename.



4
5
6
# File 'lib/file_db/system/table.rb', line 4

def filename
  @filename
end

Instance Method Details

#allObject



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

def all
  hashed_by_id.values
end

#delete(id) ⇒ Object



60
61
62
63
# File 'lib/file_db/system/table.rb', line 60

def delete id
  hashed_by_id.delete(id.to_s)
  save_records!
end

#find(id) ⇒ Object



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

def find id
  hashed_by_id[id.to_s]
end

#firstObject



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

def first
  hashed_by_id[@specific_records[:first_id]]
end

#hashed_by_idObject



52
53
54
# File 'lib/file_db/system/table.rb', line 52

def hashed_by_id
  entries_index_by[:id]
end

#lastObject



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

def last
  hashed_by_id[@specific_records[:last_id]]
end

#next_idObject



56
57
58
# File 'lib/file_db/system/table.rb', line 56

def next_id
  hashed_by_id.keys.last.to_i + 1
end

#read_rows!Object



16
17
18
19
20
21
22
23
24
# File 'lib/file_db/system/table.rb', line 16

def read_rows!
  File.foreach(@filename).with_index do |row, row_index|
    if row_index == 0
      set_fieldnames remove_line_break(row).split(',')
    else
      set_entry row.split(',')
    end
  end
end

#save_records!Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/file_db/system/table.rb', line 81

def save_records!
  headline = @fields.keys.map { |field_key| @fields[field_key] }.join(',')

  content = hashed_by_id.map do |index, entry|
    @fields.keys.map do |identifier|
      field = @fields[identifier]
      entry[field.to_sym]
    end.join(',')
  end.join("\n")

  @database.save_to_disk self, [headline, content].join("\n")
end

#update_record(object) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/file_db/system/table.rb', line 65

def update_record object
  set_fieldnames(object.class.columns) if @fields.empty?
  data_to_save = {}
  @fieldnames.each_with_index do |column, column_index|
    field = @fields[column_index]
    data_to_save[field] = object.send(field)
  end
  unless object.persisted?
    data_to_save[:id] = next_id.to_s
    object.id = data_to_save[:id]
  end
  @entries_index_by[:id][object.id.to_s] = data_to_save
  set_specific_index object.id
  save_records!
end

#where(conditions) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/file_db/system/table.rb', line 42

def where conditions
  found_elements = all
  conditions.each do |key, value|
    found_elements = found_elements.select do |entry|
      entry[key.to_sym].eql?(value.to_s)
    end
  end
  found_elements
end