Module: DocumentRecord::Serializer
- Defined in:
- lib/document_record.rb
Constant Summary collapse
- MARSHALL_VERSION =
0- MESSAGEPACK_VERSION =
1- DEFAULT_VERSION =
1
Class Method Summary collapse
Class Method Details
.dump(object, options = {}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/document_record.rb', line 10 def self.dump object, = {} version = DEFAULT_VERSION version = [:force_version] if .has_key? :force_version binary_dump = case version when 0 then Marshal.dump object when 1 then object.to_hash.to_bson else raise "No serialization version found: #{ version }" end unless .has_key? :no_version and [:no_version] stamp = [68, 82, version].pack 'c2S' # DR<version> In 4 bits binary_dump.insert( 0, stamp ) end Base64.encode64 binary_dump end |
.load(data) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/document_record.rb', line 31 def self.load data decoded = Base64.decode64 data rescue nil version = 0 if decoded.start_with? "DR" # Has version Stamp version = decoded[2,2].unpack('S').pop # Gets the Version decoded[0...4] = '' # Removes version stamp end case version when 0 then Marshal.load decoded rescue nil when 1 then Hash.from_bson( StringIO.new( decoded ) ) else raise "Unrecognized version #{version}" end end |