Module: MongoPersist

Included in:
Customer, Order, OrderProduct, Product
Defined in:
lib/mongo_persist.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#mongo_idObject

Returns the value of attribute mongo_id.



74
75
76
# File 'lib/mongo_persist.rb', line 74

def mongo_id
  @mongo_id
end

Class Method Details

.included(mod) ⇒ Object



112
113
114
115
116
# File 'lib/mongo_persist.rb', line 112

def self.included(mod)
  super(mod)
  mod.send(:include,FromHash)
  mod.send(:extend,ClassMethods)
end

Instance Method Details

#from_mongo_hash(h) ⇒ Object



93
94
95
96
# File 'lib/mongo_persist.rb', line 93

def from_mongo_hash(h)
  h = h.map_value { |v| v.safe_to_mongo_object }
  from_hash(h)
end

#mongo_attributesObject

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



76
77
78
# File 'lib/mongo_persist.rb', line 76

def mongo_attributes
  instance_variables.map { |x| x[1..-1] } - ['mongo']
end

#mongo_child_attributesObject



79
80
81
# File 'lib/mongo_persist.rb', line 79

def mongo_child_attributes
  mongo_attributes - self.class.mongo_reference_attributes
end

#to_mongo_hashObject



85
86
87
88
89
90
91
92
# File 'lib/mongo_persist.rb', line 85

def to_mongo_hash
  res = mongo_child_attributes.inject({}) { |h,attr| h.merge(attr => send(attr).safe_to_mongo_hash) }.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_hashObject



82
83
84
# File 'lib/mongo_persist.rb', line 82

def to_mongo_ref_hash
  {'_mongo_class' => klass.to_s, '_id' => mongo_id}
end