# File lib/grimm/orm/grimm_record.rb, line 37 def self.primary_key_query(value = false) "PRIMARY KEY AUTOINCREMENT" if value end
class Grimm::GrimmRecord
Public Class Methods
all()
click to toggle source
# File lib/grimm/orm/queries.rb, line 10 def self.all data = DatabaseConnector.execute "SELECT #{properties_keys.join(',')} FROM #{@table}" data.map do |row| map_object(row) end end
autoincrement_query(value = false)
click to toggle source
# File lib/grimm/orm/grimm_record.rb, line 41 def self.autoincrement_query(value = false) "AUTOINCREMENT" if value end
create_table()
click to toggle source
# File lib/grimm/orm/grimm_record.rb, line 17 def self.create_table prop_array = [] @properties.each do |key, value| properties ||= [] properties << key.to_s value.each do |name, type| properties << send("#{name.downcase}_query", type) end prop_array << properties.join(" ") end query = "CREATE TABLE IF NOT EXISTS #{@table} (#{prop_array.join(', ')})" DatabaseConnector.execute(query) make_methods end
delete(id)
click to toggle source
# File lib/grimm/orm/queries.rb, line 26 def self.delete(id) DatabaseConnector.execute "DELETE FROM #{@table} WHERE id = ?", id end
delete_all()
click to toggle source
# File lib/grimm/orm/queries.rb, line 30 def self.delete_all DatabaseConnector.execute "DELETE FROM #{@table}" end
find(id)
click to toggle source
# File lib/grimm/orm/queries.rb, line 4 def self.find(id) row = DatabaseConnector.execute("SELECT #{properties_keys.join(',')} FROM #{@table} WHERE id = ?", id).first map_object(row) end
first()
click to toggle source
# File lib/grimm/orm/queries.rb, line 39 def self.first query = "SELECT * FROM #{@table} ORDER BY id LIMIT 1" (DatabaseConnector.execute query).first end
last()
click to toggle source
# File lib/grimm/orm/queries.rb, line 34 def self.last query = "SELECT * FROM #{@table} ORDER BY id DESC LIMIT 1" (DatabaseConnector.execute query).first end
latest()
click to toggle source
# File lib/grimm/orm/queries.rb, line 18 def self.latest data = DatabaseConnector.execute "SELECT #{properties_keys.join(',')} FROM #{@table} ORDER BY created_at DESC" data.map do |row| map_object(row) end end
make_methods()
click to toggle source
# File lib/grimm/orm/grimm_record.rb, line 32 def self.make_methods mtds = @properties.keys.map(&:to_sym) mtds.each { |mtd| attr_accessor mtd } end
map_object(row)
click to toggle source
# File lib/grimm/orm/grimm_record.rb, line 88 def self.map_object(row) model_name = new @properties.each_key.with_index do |value, index| model_name.send("#{value}=", row[index]) end model_name end
nullable_query(value = true)
click to toggle source
# File lib/grimm/orm/grimm_record.rb, line 45 def self.nullable_query(value = true) "NOT NULL" unless value end
primary_key_query(value = false)
click to toggle source
properties_keys()
click to toggle source
# File lib/grimm/orm/grimm_record.rb, line 53 def self.properties_keys @properties.keys end
property(column_name, args)
click to toggle source
# File lib/grimm/orm/grimm_record.rb, line 12 def self.property(column_name, args) @properties ||= {} @properties[column_name] = args end
table_name()
click to toggle source
# File lib/grimm/orm/queries.rb, line 44 def self.table_name @table end
to_table(table_name)
click to toggle source
# File lib/grimm/orm/grimm_record.rb, line 8 def self.to_table(table_name) @table = table_name end
type_query(value)
click to toggle source
# File lib/grimm/orm/grimm_record.rb, line 49 def self.type_query(value) value.to_s end
Public Instance Methods
get_columns()
click to toggle source
# File lib/grimm/orm/grimm_record.rb, line 69 def get_columns columns = self.class.properties_keys columns.delete(:id) columns.join(",") end
get_values()
click to toggle source
# File lib/grimm/orm/grimm_record.rb, line 57 def get_values attributes = self.class.properties_keys attributes.delete(:id) attributes.map { |method| send(method) } end
new_record_placeholders()
click to toggle source
# File lib/grimm/orm/grimm_record.rb, line 83 def new_record_placeholders properties = self.class.properties_keys (["?"] * (properties.size - 1)).join(",") end
new_record_value()
click to toggle source
# File lib/grimm/orm/grimm_record.rb, line 79 def new_record_value get_values end
save()
click to toggle source
# File lib/grimm/orm/queries.rb, line 48 def save table_name = self.class.table_name if id DatabaseConnector.execute "UPDATE #{table_name} SET #{update_records_placeholders} WHERE id = ?", update_records else DatabaseConnector.execute "INSERT INTO #{table_name} (#{get_columns}) VALUES (#{new_record_placeholders})", new_record_value end end
update_records()
click to toggle source
# File lib/grimm/orm/grimm_record.rb, line 75 def update_records get_values << send(:id) end
update_records_placeholders()
click to toggle source
# File lib/grimm/orm/grimm_record.rb, line 63 def update_records_placeholders columns = self.class.properties_keys columns.delete(:id) columns.map { |col| "#{col}=?" }.join(",") end