module Algernon::Orm

Public Instance Methods

all() click to toggle source
# File lib/algernon/model/orm.rb, line 14
def all
  fields = columns.join(", ")
  data = DB.execute("SELECT id, #{fields} FROM #{table_name}")
  data.map! do |row|
    row_to_model(row)
  end

  data
end
create(params) click to toggle source
# File lib/algernon/model/orm.rb, line 5
def create(params)
  model = new
  params.each do |key, value|
    model.send("#{key}=", value)
  end

  model.save
end
destroy(id) click to toggle source
# File lib/algernon/model/orm.rb, line 43
def destroy(id)
  DB.execute("DELETE FROM #{table_name} WHERE id= ?", id)
end
destroy_all() click to toggle source
# File lib/algernon/model/orm.rb, line 47
def destroy_all
  DB.execute("DELETE FROM #{table_name}")
end
find(id) click to toggle source
# File lib/algernon/model/orm.rb, line 34
def find(id)
  fields = columns.join(", ")
  row = DB.execute(
    "SELECT id, #{fields} FROM #{table_name} WHERE id = ?",
    id
  ).first
  row_to_model(row) if row
end
row_to_model(row) click to toggle source
# File lib/algernon/model/orm.rb, line 24
def row_to_model(row)
  model = new

  columns.each_with_index do |field, index|
    model.send("#{field}=", row[index + 1]) if row
  end

  model
end