class Panda::Record::Base

Public Class Methods

all() click to toggle source
# File lib/panda/record/base.rb, line 62
def self.all
  Database.execute_query(
    "SELECT * FROM #{table} ORDER BY id ASC"
  ).map(&method(:get_model_object))
end
count() click to toggle source
# File lib/panda/record/base.rb, line 94
def self.count
  Database.execute_query("SELECT COUNT (*) FROM #{table}")[0][0]
end
create(attributes) click to toggle source
# File lib/panda/record/base.rb, line 56
def self.create(attributes)
  model = new(attributes)
  model.save
  true
end
create_table() click to toggle source
# File lib/panda/record/base.rb, line 48
def self.create_table
  Database.execute_query(
    "CREATE TABLE IF NOT EXISTS #{table} "  \
    "(#{column_names_with_constraints.join(', ')})"
  )
  build_column_methods
end
destroy(id) click to toggle source
# File lib/panda/record/base.rb, line 98
def self.destroy(id)
  Database.execute_query("DELETE FROM #{table} WHERE id = ?", id)
end
destroy_all() click to toggle source
# File lib/panda/record/base.rb, line 102
def self.destroy_all
  Database.execute_query "DELETE FROM #{table}"
end
find(id) click to toggle source
# File lib/panda/record/base.rb, line 68
def self.find(id)
  row = Database.execute_query(
    "SELECT * FROM #{table} WHERE id = ?",
    id.to_i
  ).first
  get_model_object(row)
end
find_by(option) click to toggle source
# File lib/panda/record/base.rb, line 76
def self.find_by(option)
  row = Database.execute_query(
    "SELECT * FROM #{table} WHERE #{option.keys.first} = ?",
    option.values.first
  ).first
  get_model_object(row)
end
new(attributes = {}) click to toggle source
# File lib/panda/record/base.rb, line 9
def initialize(attributes = {})
  attributes.each { |column, value| send("#{column}=", value) }
end
property(column_name, constraints) click to toggle source
# File lib/panda/record/base.rb, line 43
def self.property(column_name, constraints)
  @properties ||= {}
  @properties[column_name] = constraints
end
to_table(name) click to toggle source
# File lib/panda/record/base.rb, line 39
def self.to_table(name)
  @table = name.to_s
end

Public Instance Methods

destroy() click to toggle source
# File lib/panda/record/base.rb, line 35
def destroy
  self.class.destroy(id)
end
save() click to toggle source
# File lib/panda/record/base.rb, line 13
def save
  query = if id
            "UPDATE #{model_table} SET " \
            "#{update_placeholders} WHERE id = ?"
          else
            "INSERT INTO #{model_table} (#{current_table_columns})" \
            " VALUES (#{current_table_placeholders})"
          end
  values = id ? record_values << id : record_values
  Database.execute_query(query, values)
  true
end
Also aliased as: save!
save!()
Alias for: save
update(attributes) click to toggle source
# File lib/panda/record/base.rb, line 26
def update(attributes)
  attributes.each do |key, value|
    send("#{key}=", value)
  end
  save
end