Class: Schron::Datastore::Mongo::Serializer
- Inherits:
-
Object
- Object
- Schron::Datastore::Mongo::Serializer
- Defined in:
- lib/schron/datastore/mongo/serializer.rb
Instance Method Summary collapse
- #serialize_document(hash) ⇒ Object
- #serialize_id(uuid) ⇒ Object
- #symbolize_keys(hash) ⇒ Object
- #unserialize_document(doc) ⇒ Object
- #unserialize_id(bin) ⇒ Object
Instance Method Details
#serialize_document(hash) ⇒ Object
6 7 8 9 10 |
# File 'lib/schron/datastore/mongo/serializer.rb', line 6 def serialize_document(hash) doc = hash.merge(_id: serialize_id(hash[:id])) doc.delete :id doc end |
#serialize_id(uuid) ⇒ Object
19 20 21 |
# File 'lib/schron/datastore/mongo/serializer.rb', line 19 def serialize_id(uuid) BSON::Binary.new(uuid, BSON::Binary::SUBTYPE_UUID) end |
#symbolize_keys(hash) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/schron/datastore/mongo/serializer.rb', line 27 def symbolize_keys(hash) hash.reduce({}) do |symbolized, (k, v)| val = v.kind_of?(Hash) ? symbolize_keys(v) : v symbolized[k.to_sym] = val symbolized end end |
#unserialize_document(doc) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/schron/datastore/mongo/serializer.rb', line 12 def unserialize_document(doc) hash = symbolize_keys(doc) hash.merge!(id: unserialize_id(hash[:_id])) hash.delete :_id hash end |
#unserialize_id(bin) ⇒ Object
23 24 25 |
# File 'lib/schron/datastore/mongo/serializer.rb', line 23 def unserialize_id(bin) bin.to_s end |