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(set) self.class.ogmanager.store.update_properties(self, set) end # 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 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_properties(set, options = nil) ogmanager.store.update_properties(self, set, options) end alias_method :pupdate, :update_properties alias_method :update_property, :update_properties alias_method :batch_update, :update_properties 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 transaction(&block) ogmanager.store.transaction(&block) 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 find_options(options) meta :find_options, options end # Set the default order option for this entity. def order(order_str) meta :find_options, :order => order_str end # Enable schema inheritance for this Entity class. # The Single Table Inheritance pattern is used. def schema_inheritance meta :schema_inheritance end # Is this entity a polymorphic parent? def polymorphic_parent? self.to_s == self.metadata.polymorphic.to_s end # :nodoc: # Used internally to fix the forward reference problem. def const_missing(sym) return sym end end end # A helper class. class Entity class << self def resolve_primary_key(klass) 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