Module: MongoHash
- Included in:
- Hash
- Defined in:
- lib/mongo_persist/hash_ext.rb
Instance Method Summary collapse
- #can_mongo_convert? ⇒ Boolean
- #get_mongo_id ⇒ Object
- #mongo_class ⇒ Object
- #naked_reference? ⇒ Boolean
- #to_mongo_hash ⇒ Object
- #to_mongo_hash_for_obj ⇒ Object
- #to_mongo_object ⇒ Object
Instance Method Details
#can_mongo_convert? ⇒ Boolean
31 32 33 |
# File 'lib/mongo_persist/hash_ext.rb', line 31 def can_mongo_convert? all? { |k,x| x.can_mongo_convert? && x } end |
#get_mongo_id ⇒ Object
2 3 4 5 |
# File 'lib/mongo_persist/hash_ext.rb', line 2 def get_mongo_id ['_id','mongo_id'].each { |x| return self[x] if self[x] } nil end |
#mongo_class ⇒ Object
9 10 11 |
# File 'lib/mongo_persist/hash_ext.rb', line 9 def mongo_class self['_mongo_class'] ? eval(self['_mongo_class']) : nil end |
#naked_reference? ⇒ Boolean
6 7 8 |
# File 'lib/mongo_persist/hash_ext.rb', line 6 def naked_reference? (keys - ['_id','_mongo_class','mongo_id']).empty? && !!get_mongo_id end |
#to_mongo_hash ⇒ Object
25 26 27 28 29 |
# File 'lib/mongo_persist/hash_ext.rb', line 25 def to_mongo_hash res = {} each { |k,v| res[k.safe_to_mongo_hash.to_mongo_key] = v.safe_to_mongo_hash } res end |
#to_mongo_hash_for_obj ⇒ Object
12 13 14 15 16 |
# File 'lib/mongo_persist/hash_ext.rb', line 12 def to_mongo_hash_for_obj h = reject { |k,v| k == '_mongo_class' } h['mongo_id'] = h['_id'] if h['_id'] h.reject { |k,v| k == '_id' } end |
#to_mongo_object ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/mongo_persist/hash_ext.rb', line 17 def to_mongo_object return map_value { |v| v.safe_to_mongo_object }.map_key { |k| k.from_mongo_key } unless mongo_class if naked_reference? mongo_class.collection.find_one_object('_id' => get_mongo_id) else mongo_class.from_mongo_hash(to_mongo_hash_for_obj) end end |