Skip to content

Instantly share code, notes, and snippets.

@pocari
Created September 11, 2012 13:19
Show Gist options
  • Save pocari/3698399 to your computer and use it in GitHub Desktop.
Save pocari/3698399 to your computer and use it in GitHub Desktop.
class Object
def eigenclass
class << self
self
end
end
end
module Util
module_function
def to_underscore(str)
str.split(/(?![a-z])(?=[A-Z])/).map(&:downcase).join("_")
end
end
class Table
class << self
attr_reader :primary_columns, :columns, :all_columns, :table_symbol
def def_columns_accessor(clazz)
[:primary_columns, :columns, :all_columns].each do |sym_col|
clazz.module_eval %{
def #{sym_col}
self.class.#{sym_col}.map do |sym|
[sym, send(sym)]
end
end
}
end
end
def inherited(clazz)
def_columns_accessor(clazz)
clazz.module_eval do
@primary_columns = []
@columns = []
@all_columns = []
@table_symbol = Util.to_underscore(clazz.to_s)
def insert_statement_helper(columns)
syms, dummy = columns.transpose
"insert into #{self.class.table_symbol} (#{syms.join(',')}) values (#{(['?'] * syms.size).join(',')})"
end
def update_satatement_helper(columns)
set_part = columns.map{|sym, value|
"#{sym}=?"
}.join(",")
"update #{self.class.table_symbol} set #{set_part} where #{primary_where}"
end
def unless_nil_columns(cols)
cols.find_all do |e|
e[1]
end
end
def primary_where
primary_columns.map{|sym, value|
"#{sym}=?"
}.join(" and ")
end
def insert_statement
[insert_statement_helper(all_columns), all_columns.transpose[1]]
end
def insert_statement_unless_nil
cols = unless_nil_columns(all_columns)
[insert_statement_helper(cols), cols.transpose[1]]
end
def update_statement
[update_satatement_helper(columns), columns.transpose[1] + primary_columns.transpose[1]]
end
def update_statement_unless_nil
cols = unless_nil_columns(columns)
[update_satatement_helper(cols), cols.transpose[1] + primary_columns.transpose[1]]
end
def delete_statement
["delete from #{self.class.table_symbol} where #{primary_where}", primary_columns.transpose[1]]
end
def to_hash
all_columns.inject({}) do |acc, col|
acc[col[0]] = col[1]
acc
end
end
end
clazz.eigenclass.module_eval do
def from_hash(hash)
obj = self.new
hash.each do |sym, value|
obj.send(sym.to_s + "=", value)
end
obj
end
end
end
def def_table_symbol(sym)
@table_symbol = sym
end
def def_primary_columns(*syms)
syms.each do |s|
@primary_columns << s
@all_columns << s
attr_accessor s
end
end
def def_columns(*syms)
syms.each do |s|
@columns << s
@all_columns << s
attr_accessor s
end
end
end
end
#------------------------------------------------------------------------------
class Hoge < Table
def_primary_columns :key1, :key2
def_columns :f1, :f2, :f3
end
obj = Hoge.new
obj.key1 = "hoge1"
obj.key2 = "hoge2"
obj.f1 = "piyo1"
obj.f3 = "piyo3"
p obj
p Hoge.table_symbol
p obj.primary_columns
p obj.columns
p obj.all_columns
p obj.insert_statement
p obj.insert_statement_unless_nil
p obj.update_statement
p obj.update_statement_unless_nil
p obj.delete_statement
obj2 = Hoge.from_hash(key1:"HOGE1", key2:"HOGE2", f1:"PIYO1", f3:"PIYO3")
p obj2
p obj2.to_hash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment