Sha256: 7d786402ac6ff88aff7b713f2da1b32b61cbee859659547718080c167c0ee1a0
Contents?: true
Size: 1.46 KB
Versions: 1
Compression:
Stored size: 1.46 KB
Contents
require "sqlite3" module Rollerskates module DatabaseTableHelper def table_name to_s.downcase.pluralize end def database @db ||= SQLite3::Database.new File.join("db", "app.db") end def model_name to_s.downcase end def all_columns columns = database.prepare "SELECT * FROM #{table_name}" columns.columns.map(&:to_sym) end private def add_property(property) @properties ||= [ "id integer PRIMARY KEY AUTOINCREMENT", "created_at datetime NOT NULL", "updated_at datetime NOT NULL" ] @properties << property end def property(field, options) add_property "#{field} #{parse_constraints(options)}" end def create_table query = "CREATE TABLE IF NOT EXISTS #{table_name}\ (#{@properties.join(', ')})" database.execute(query) all_columns.each { |var| attr_accessor var } end def parse_constraints(constraints) attributes = "" constraints.each do |attr, value| attributes += send(attr.to_s, value) end attributes end def type(value) "#{value.to_s.upcase} " end def primary_key(value) return "PRIMARY KEY " if value " " end def nullable(value) return "NOT NULL " if value "NULL " end def default(value) "DEFAULT `#{value}` " end def auto_increment(value) "AUTOINCREMENT " if value end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
rollerskates-0.1.1 | lib/rollerskates/orm/helpers/database_table_helper.rb |