Sha256: c57adbe5950da9b1fccf8789ff6e7769a39ac5a4410677e71df7710251851a29

Contents?: true

Size: 1.65 KB

Versions: 2

Compression:

Stored size: 1.65 KB

Contents

# * George Moschovitis  <gm@navel.gr>
# (c) 2004-2005 Navel, all rights reserved.
# $Id$

module Og

module Enchant

	# Enchant a managed class. Add useful DB related methods 
	# to the class and its instances. 
	
	def enchant(klass)
		klass.module_eval <<-"end_eval", __FILE__, __LINE__
			def self.create(*params, &block)
				obj = #{klass}.new(*params, &block)
				obj.save!
			end
				
			def self.save(obj)
				Og.db << obj
			end
			
			def self.load(oid_or_name)
				Og.db.load(oid_or_name, #{klass})
			end

			def self.get(oid_or_name)
				Og.db.load(oid_or_name, #{klass})
			end
			
			def self.[](oid_or_name)
				Og.db.load(oid_or_name, #{klass})
			end
			
			def self.load_all(extra_sql = nil)
				Og.db.load_all(#{klass}, extra_sql)
			end
			
			def self.all(extra_sql = nil)
				Og.db.load_all(#{klass}, extra_sql)
			end

			def self.count(sql = "SELECT COUNT(*) FROM #{klass::DBTABLE}")
				Og.db.count(sql, #{klass})
			end

			def self.select(sql)
				Og.db.select(sql, #{klass})
			end

			def self.select_one(sql)
				Og.db.select_one(sql, #{klass})
			end

			def self.one(sql)
				Og.db.select_one(sql, #{klass})
			end
			
			def self.delete(obj_or_oid)
				Og.db.delete(obj_or_oid, #{klass})
			end

			def self.properties_and_relations
				@@__meta[:props_and_relations]
			end

			def each(&block)
				all.each(&block)
			end
			include Enumerable
			
			def save
				Og.db << self
				return self
			end
			alias_method :save!, :save
			
			def update_properties(updatesql)
				Og.db.pupdate(updatesql, self.oid, #{klass})
			end
			alias_method :pupdate!, :update_properties
			
			def delete!
				Og.db.delete(@oid, #{klass})
			end
		end_eval
	end

end
	
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
nitro-0.11.0 lib/og/enchant.rb
og-0.11.0 lib/og/enchant.rb