lib/mongo_persist/base.rb in mongo_persist-0.1.0 vs lib/mongo_persist/base.rb in mongo_persist-0.2.0

- old
+ new

@@ -11,10 +11,14 @@ rescue => exp require 'pp' pp obj.to_mongo_hash raise exp end + + def get_fresh + obj.klass.collection.find_one_object("_id" => obj.mongo_id) + end end module MongoPersist NIL_OBJ = 99999 attr_accessor :mongo_id @@ -41,11 +45,11 @@ return h end def to_mongo_hash res = mongo_child_attributes.inject({}) do |h,attr| obj = send(attr) - raise "#{attr} is nil" unless obj + #raise "#{attr} is nil" unless obj new_hashx(attr,h,obj) end.merge("_mongo_class" => self.class.to_s) klass.mongo_reference_attributes.each do |attr| val = send(attr) res[attr] = val.to_mongo_ref_hash if val @@ -55,10 +59,14 @@ def from_mongo_hash(h) h = h.map_value { |v| v.safe_to_mongo_object } from_hash(h) end fattr(:mongo) { MongoWrapper.new(:obj => self) } + + def can_mongo_convert? + true + end module ClassMethods dsl_method(:mongo_reference_attributes) { [] } def default_collection_name to_s.downcase.pluralize @@ -67,10 +75,12 @@ def new_with_nil_args args = (1..(instance_method(:initialize).arity)).map { |x| nil } new(*args) end def from_mongo_hash(h) - new_with_nil_args.tap { |x| x.from_mongo_hash(h) } + res = new_with_nil_args.tap { |x| x.from_mongo_hash(h) } + res.after_mongo_load if res.respond_to?(:after_mongo_load) + res end def mongo_connection(ops) ops.each { |k,v| send("#{k}=",v) } end end