require 'og/relation' module Og # Include this module to classes to make them managable by Og. module EntityMixin def self.append_features(base) super base.extend(ClassMethods) base.module_eval <<-end_eval, __FILE__, __LINE__ def save(options = nil) self.class.ogmanager.store.save(self, options) # return self end alias_method :save!, :save def insert self.class.ogmanager.store.insert(self) return self end def update(options = nil) self.class.ogmanager.store.update(self, options) end def update_properties(*properties) self.class.ogmanager.store.update(self, :only => properties) end alias_method :update_property, :update_properties alias_method :pupdate, :update_properties def update_by_sql(set) self.class.ogmanager.store.update_by_sql(self, set) end alias_method :update_sql, :update_by_sql alias_method :supdate, :update_by_sql # Reload this entity instance from the store. def reload self.class.ogmanager.store.reload(self, self.pk) end alias_method :reload!, :reload # Delete this entity instance from the store. def delete self.class.ogmanager.store.delete(self) end alias_method :delete!, :delete def transaction(&block) self.class.ogmanager.store.transaction(&block) end def saved? not @oid.nil? end alias_method :serialized?, :saved? def og_quote(obj) self.class.ogmanager.store.quote(obj) end end_eval base.send(:include, RelationMacros) end module ClassMethods def create(*args) obj = self.new(*args) yield(obj) if block_given? ogmanager.store.save(obj) return obj end # Load an instance of this Entity class using the primary # key. def load(pk) ogmanager.store.load(pk, self) end alias_method :[], :load alias_method :exist?, :load def update(set, options = nil) ogmanager.store.update_by_sql(self, set, options) end def find(options = {}) # gmosx, FIXME: this code seems too complex, # improve this! if find_options = self.__meta[:find_options] options = find_options.first.dup.update(options) end options[:class] = self if self.metadata.superclass options[:type] = self end ogmanager.store.find(options) end alias_method :all, :find def find_one(options = {}) options[:class] = self ogmanager.store.find_one(options) end alias_method :one, :find_one alias_method :first, :find_one def select(sql) ogmanager.store.select(sql, self) end def select_one(sql) ogmanager.store.select_one(sql, self) end def count(options = {}) options[:class] = self ogmanager.store.count(options) end # Delete an instance of this Entity class using the actual # instance or the primary key. def delete(obj_or_pk) ogmanager.store.delete(obj_or_pk, self) end alias_method :delete!, :delete def escape(str) ogmanager.store.escape(str) end def transaction(&block) ogmanager.store.transaction(&block) end # Return the store (connection) for this class. def ogstore ogmanager.store end def primary_key unless @__meta and @__meta[:primary_key] self.meta :primary_key, Entity.resolve_primary_key(self) end @__meta[:primary_key].flatten end # Set the default find options for this entity. def set_find_options(options) meta :find_options, options end alias_method :find_options, :set_find_options # Enable schema inheritance for this Entity class. # The Single Table Inheritance pattern is used. def set_schema_inheritance meta :schema_inheritance end alias_method :schema_inheritance, :set_schema_inheritance # Set the default order option for this entity. def set_order(order_str) meta :find_options, :order => order_str end alias_method :order, :set_order # Set a custom table name. def set_sql_table(table) meta :sql_table, table.to_s end alias_method :set_table, :set_sql_table # Set the primary key. def set_primary_key(pk, pkclass = Fixnum) meta :primary_key, pk, pkclass end # Is this entity a polymorphic parent? def polymorphic_parent? self.to_s == self.metadata.polymorphic.to_s end # Used internally to fix the forward reference problem. def const_missing(sym) # :nodoc: all return sym end end end # A helper class. class Entity class << self def resolve_primary_key(klass) if pk = klass.__meta[:primary_key] return pk end for p in klass.properties if p.meta[:primary_key] return p.symbol, p.klass end end return :oid, Fixnum end end include EntityMixin end end