module Rollerskates::DatabaseTableHelper

Public Instance Methods

all_columns() click to toggle source
# File lib/rollerskates/orm/helpers/database_table_helper.rb, line 17
def all_columns
  columns = database.prepare "SELECT * FROM #{table_name}"
  columns.columns.map(&:to_sym)
end
database() click to toggle source
# File lib/rollerskates/orm/helpers/database_table_helper.rb, line 9
def database
  @db ||= SQLite3::Database.new File.join("db", "app.db")
end
model_name() click to toggle source
# File lib/rollerskates/orm/helpers/database_table_helper.rb, line 13
def model_name
  to_s.downcase
end
table_name() click to toggle source
# File lib/rollerskates/orm/helpers/database_table_helper.rb, line 5
def table_name
  to_s.downcase.pluralize
end

Private Instance Methods

add_property(property) click to toggle source
# File lib/rollerskates/orm/helpers/database_table_helper.rb, line 24
def add_property(property)
  @properties ||= [
    "id integer PRIMARY KEY AUTOINCREMENT",
    "created_at datetime NOT NULL",
    "updated_at datetime NOT NULL"
  ]
  @properties << property
end
auto_increment(value) click to toggle source
# File lib/rollerskates/orm/helpers/database_table_helper.rb, line 72
def auto_increment(value)
  "AUTOINCREMENT " if value
end
create_table() click to toggle source
# File lib/rollerskates/orm/helpers/database_table_helper.rb, line 37
def create_table
  query = "CREATE TABLE IF NOT EXISTS #{table_name}\
          (#{@properties.join(', ')})"
  database.execute(query)

  all_columns.each { |var| attr_accessor var }
end
default(value) click to toggle source
# File lib/rollerskates/orm/helpers/database_table_helper.rb, line 68
def default(value)
  "DEFAULT `#{value}` "
end
nullable(value) click to toggle source
# File lib/rollerskates/orm/helpers/database_table_helper.rb, line 63
def nullable(value)
  return "NOT NULL " if value
  "NULL "
end
parse_constraints(constraints) click to toggle source
# File lib/rollerskates/orm/helpers/database_table_helper.rb, line 45
def parse_constraints(constraints)
  attributes = ""
  constraints.each do |attr, value|
    attributes += send(attr.to_s, value)
  end

  attributes
end
primary_key(value) click to toggle source
# File lib/rollerskates/orm/helpers/database_table_helper.rb, line 58
def primary_key(value)
  return "PRIMARY KEY " if value
  " "
end
property(field, options) click to toggle source
# File lib/rollerskates/orm/helpers/database_table_helper.rb, line 33
def property(field, options)
  add_property "#{field} #{parse_constraints(options)}"
end
type(value) click to toggle source
# File lib/rollerskates/orm/helpers/database_table_helper.rb, line 54
def type(value)
  "#{value.to_s.upcase} "
end