class TADB::Table

Public Class Methods

new(name, clear_if_content = false) click to toggle source
# File lib/tadb/table.rb, line 6
def initialize(name, clear_if_content = false)
  @name = name
  clear_file_contents if clear_if_content
end

Public Instance Methods

clear() click to toggle source
# File lib/tadb/table.rb, line 41
def clear
  File.open(db_path, 'w') do |f|
    f.sync = true
  end
end
delete(id) click to toggle source
# File lib/tadb/table.rb, line 31
def delete(id)
  remaining = entries.reject {|entry| entry[:id] === id}

  file('w') do |f|
    remaining.each do |entry|
      f.puts(JSON.generate(entry))
    end
  end
end
entries() click to toggle source
# File lib/tadb/table.rb, line 11
def entries
  file('r') do |f|
    f.readlines.map do |line|
      JSON.parse(line, symbolize_names: true)
    end
  end
end
insert(_entry) click to toggle source
# File lib/tadb/table.rb, line 19
def insert(_entry)
  entry = _entry.clone
  entry[:id] ||= SecureRandom.uuid

  validate_types(entry)

  file('a') do |f|
    f.puts(JSON.generate(entry))
  end
  entry[:id]
end

Private Instance Methods

clear_file_contents() click to toggle source
# File lib/tadb/table.rb, line 49
def clear_file_contents
  return unless file_present?
  clear
end
db_path() click to toggle source
# File lib/tadb/table.rb, line 69
def db_path
  "./db/#{@name}"
end
file(mode, &block) click to toggle source
# File lib/tadb/table.rb, line 58
def file(mode, &block)
  Dir.mkdir('./db') unless File.exists?('./db')
  clear unless File.exists?(db_path)

  File.open(db_path, mode) do |file|
    # The new File object is buffered mode (or non-sync mode), unless filename is a tty. See IO#flush, IO#fsync, IO#fdatasync, and IO#sync= about sync mode.
    file.sync = true # https://ruby-doc.org/core-3.0.0/File.html#method-c-new-label-Examples
    block.call(file)
  end
end
file_present?() click to toggle source
# File lib/tadb/table.rb, line 54
def file_present?
  File.file?(db_path)
end
validate_types(entry) click to toggle source
# File lib/tadb/table.rb, line 73
def validate_types(entry)
  invalid_pairs = entry.select do |_, value|
    !value.is_a?(String) && !value.is_a?(Numeric) && value != true && value != false
  end

  unless invalid_pairs.empty?
    separator = '/n/t'
    pairs_description = invalid_pairs
                            .map {|key, value| " #{key}: #{value}"}
                            .join(",#{separator}")
    raise TypeError.new("Can't persist field(s) because they contain non-primitive values at" + separator + pairs_description)
  end
end