class FileDb::System::Table
Attributes
entries_index_by[RW]
filename[RW]
Public Class Methods
new(filename, database)
click to toggle source
# 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
Public Instance Methods
all()
click to toggle source
# File lib/file_db/system/table.rb, line 30 def all hashed_by_id.values end
delete(id)
click to toggle source
# File lib/file_db/system/table.rb, line 60 def delete id hashed_by_id.delete(id.to_s) save_records! end
find(id)
click to toggle source
# File lib/file_db/system/table.rb, line 26 def find id hashed_by_id[id.to_s] end
first()
click to toggle source
# File lib/file_db/system/table.rb, line 34 def first hashed_by_id[@specific_records[:first_id]] end
hashed_by_id()
click to toggle source
# File lib/file_db/system/table.rb, line 52 def hashed_by_id entries_index_by[:id] end
last()
click to toggle source
# File lib/file_db/system/table.rb, line 38 def last hashed_by_id[@specific_records[:last_id]] end
next_id()
click to toggle source
# File lib/file_db/system/table.rb, line 56 def next_id hashed_by_id.keys.last.to_i + 1 end
read_rows!()
click to toggle source
# 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!()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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
Private Instance Methods
remove_line_break(value)
click to toggle source
# File lib/file_db/system/table.rb, line 126 def remove_line_break value value.to_s.gsub "\n", "" end
set_entry(entry)
click to toggle source
# File lib/file_db/system/table.rb, line 106 def set_entry entry t_entry = {} entry.each_with_index do |column, column_index| t_entry[@fields[column_index].to_sym] = remove_line_break(column) end key_name = remove_line_break(entry[@fieldnames[:id]]) @entries_index_by[:id][key_name.to_s] = t_entry set_specific_index key_name end
set_fieldnames(fieldnames)
click to toggle source
# File lib/file_db/system/table.rb, line 98 def set_fieldnames fieldnames fieldnames.each_with_index do |column, column_index| symbol_column = remove_line_break(column).to_sym @fields[column_index] = symbol_column @fieldnames[symbol_column] = column_index end end
set_specific_index(id)
click to toggle source
# File lib/file_db/system/table.rb, line 118 def set_specific_index id @specific_records[:first_id] ||= id.to_s @specific_records[:last_id] ||= id.to_s if @specific_records[:last_id].to_i < id.to_i @specific_records[:last_id] = id.to_s end end