class Bezel::BezelrecordBase
Public Class Methods
all()
click to toggle source
# File lib/bezelrecord_base/bezelrecord_base.rb, line 41 def self.all results = DBConnection.execute(<<-SQL) SELECT #{table_name}.* FROM #{table_name} SQL parse_all(results) end
columns()
click to toggle source
# File lib/bezelrecord_base/bezelrecord_base.rb, line 11 def self.columns unless @columns @columns = DBConnection.columns(table_name) end @columns end
finalize!()
click to toggle source
# File lib/bezelrecord_base/bezelrecord_base.rb, line 18 def self.finalize! columns.each do |column| define_method("#{column}") do @attributes = self.attributes @attributes[column] end define_method("#{column}=") do |value| @attributes = self.attributes @attributes[column] = value end end end
find(id)
click to toggle source
# File lib/bezelrecord_base/bezelrecord_base.rb, line 57 def self.find(id) object = DBConnection.execute(<<-SQL, id) SELECT * FROM #{table_name} WHERE id = ? LIMIT 1 SQL nil || object.map {|obj| self.new(obj)}.first end
new(params = {})
click to toggle source
# File lib/bezelrecord_base/bezelrecord_base.rb, line 71 def initialize(params = {}) table_columns = self.class.columns params.each do |key, value| raise "unknown attribute '#{key}'" unless table_columns.include?(key.to_sym) send("#{key}=",value) end end
parse_all(results)
click to toggle source
# File lib/bezelrecord_base/bezelrecord_base.rb, line 51 def self.parse_all(results) objects = results.map do |object| self.new(object) end end
table_name()
click to toggle source
# File lib/bezelrecord_base/bezelrecord_base.rb, line 37 def self.table_name @table_name ||= self.to_s.tableize end
table_name=(table_name)
click to toggle source
# File lib/bezelrecord_base/bezelrecord_base.rb, line 33 def self.table_name=(table_name) @table_name = table_name end
Public Instance Methods
attribute_values()
click to toggle source
# File lib/bezelrecord_base/bezelrecord_base.rb, line 84 def attribute_values table_cols = self.class.columns table_cols.map{ |col| self.send("#{col}")} end
attributes()
click to toggle source
# File lib/bezelrecord_base/bezelrecord_base.rb, line 80 def attributes @attributes = @attributes || {} end
insert()
click to toggle source
# File lib/bezelrecord_base/bezelrecord_base.rb, line 89 def insert columns = self.class.columns columns = columns.reject { |col| col == :id } attr_vals = attribute_values attr_vals = attr_vals[1..-1] id = DBConnection.execute(<<-SQL, attr_vals) INSERT INTO #{ self.class.table_name } (#{ columns.join(", ") }) VALUES (#{ Array.new(columns.length, "?").join(", ") }) RETURNING id; SQL self.send(:id=, id) end
save()
click to toggle source
# File lib/bezelrecord_base/bezelrecord_base.rb, line 118 def save id.nil? ? insert : update end
update()
click to toggle source
# File lib/bezelrecord_base/bezelrecord_base.rb, line 106 def update DBConnection.execute(<<-SQL, attribute_values[1..-1].concat([attribute_values.first])) UPDATE #{ self.class.table_name } SET #{ self.class.columns[1..-1].map{ |col| "#{ col } = ?" }.join(", ") } WHERE id = ? SQL end