Sha256: 1509720a90b466af36fadcc398c529039e47665d3479399825c3fa01d6843b71

Contents?: true

Size: 1.11 KB

Versions: 62

Compression:

Stored size: 1.11 KB

Contents

module Workarea
  module Elasticsearch
    module Serializer
      class << self
        def serialize(model)
          serialized = if model.is_a?(Mongoid::Document)
                         serialize_mongoid(model)
                       else
                         serialize_object(model)
                       end

          { 'model_class' => model.class.name, 'model' => serialized }
        end

        def deserialize(source)
          klass = source['model_class'].constantize

          if klass < Mongoid::Document
            deserialize_mongoid(klass, source['model'])
          else
            deserialize_object(source['model'])
          end
        end

        def serialize_mongoid(model)
          serialize_object(model.as_document)
        end

        def serialize_object(object)
          Base64.encode64(Marshal.dump(object))
        end

        def deserialize_mongoid(klass, serialized)
          Mongoid::Factory.from_db(klass, deserialize_object(serialized))
        end

        def deserialize_object(object)
          Marshal.load(Base64.decode64(object))
        end
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.27 lib/workarea/elasticsearch/serializer.rb
workarea-core-3.5.26 lib/workarea/elasticsearch/serializer.rb
workarea-core-3.4.45 lib/workarea/elasticsearch/serializer.rb
workarea-core-3.5.25 lib/workarea/elasticsearch/serializer.rb
workarea-core-3.5.23 lib/workarea/elasticsearch/serializer.rb
workarea-core-3.4.44 lib/workarea/elasticsearch/serializer.rb
workarea-core-3.5.22 lib/workarea/elasticsearch/serializer.rb
workarea-core-3.4.43 lib/workarea/elasticsearch/serializer.rb
workarea-core-3.5.21 lib/workarea/elasticsearch/serializer.rb
workarea-core-3.4.42 lib/workarea/elasticsearch/serializer.rb
workarea-core-3.5.20 lib/workarea/elasticsearch/serializer.rb
workarea-core-3.4.41 lib/workarea/elasticsearch/serializer.rb
workarea-core-3.5.19 lib/workarea/elasticsearch/serializer.rb
workarea-core-3.4.40 lib/workarea/elasticsearch/serializer.rb
workarea-core-3.5.18 lib/workarea/elasticsearch/serializer.rb
workarea-core-3.4.39 lib/workarea/elasticsearch/serializer.rb
workarea-core-3.5.17 lib/workarea/elasticsearch/serializer.rb
workarea-core-3.4.38 lib/workarea/elasticsearch/serializer.rb
workarea-core-3.5.16 lib/workarea/elasticsearch/serializer.rb
workarea-core-3.4.37 lib/workarea/elasticsearch/serializer.rb