module MongoPersist

Constants

NIL_OBJ

Attributes

mongo_id[RW]

Public Class Methods

included(mod) click to toggle source
Calls superclass method
# File lib/mongo_persist/base.rb, line 88
def self.included(mod)
  super(mod)
  mod.send(:include,FromHash)
  mod.send(:extend,ClassMethods)
end

Public Instance Methods

can_mongo_convert?() click to toggle source
# File lib/mongo_persist/base.rb, line 65
def can_mongo_convert?
  true
end
from_mongo_hash(h) click to toggle source
# File lib/mongo_persist/base.rb, line 59
def from_mongo_hash(h)
  h = h.map_value { |v| v.safe_to_mongo_object }
  from_hash(h)
end
mongo_addl_attributes() click to toggle source
# File lib/mongo_persist/base.rb, line 29
def mongo_addl_attributes
  []
end
mongo_attributes() click to toggle source

can be overriden by class. If not, assumes that all instance variables should be saved.

# File lib/mongo_persist/base.rb, line 26
def mongo_attributes
  (instance_variables.map { |x| x[1..-1] } - ['mongo','mongo_id']).select { |x| respond_to?(x) }
end
mongo_child_attributes() click to toggle source
# File lib/mongo_persist/base.rb, line 32
def mongo_child_attributes
  (mongo_attributes - self.class.mongo_reference_attributes + mongo_addl_attributes).uniq
end
new_hashx(attr,h,obj) click to toggle source
# File lib/mongo_persist/base.rb, line 38
def new_hashx(attr,h,obj)
  if obj.can_mongo_convert?
    h.merge(attr => obj.to_mongo_hash) 
  else
    h
  end
rescue
  return h
end
to_mongo_hash() click to toggle source
# File lib/mongo_persist/base.rb, line 47
def to_mongo_hash
  res = mongo_child_attributes.inject({}) do |h,attr| 
    obj = send(attr)
    #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
  end
  res
end
to_mongo_ref_hash() click to toggle source
# File lib/mongo_persist/base.rb, line 35
def to_mongo_ref_hash
  {'_mongo_class' => klass.to_s, '_id' => mongo_id}
end