Module: Og::Enchant
- Included in:
- Database, MockDatabase
- Defined in:
- lib/og/enchant.rb
Instance Method Summary collapse
-
#enchant(klass) ⇒ Object
Enchant a managed class.
Instance Method Details
#enchant(klass) ⇒ Object
Enchant a managed class. Add useful DB related methods to the class and its instances.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/og/enchant.rb', line 12 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 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 |