123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Criteria::Marshalable

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/mongoid/criteria/marshalable.rb

Overview

Mixin module for ::Mongoid::Criteria which adds custom Marshal.dump functionality.

Instance Method Summary

Instance Method Details

#dump_hash(name) (private)

[ GitHub ]

  
# File 'lib/mongoid/criteria/marshalable.rb', line 45

def dump_hash(name)
  send(name).inject({}) do |raw, (key, value)|
    raw[key] = value
    raw
  end
end

#load_hash(hash_class, raw) (private)

[ GitHub ]

  
# File 'lib/mongoid/criteria/marshalable.rb', line 52

def load_hash(hash_class, raw)
  hash = hash_class.new(klass.aliased_fields, klass.fields)
  hash.merge!(raw)
  hash
end

#marshal_dumpArray<Object>

Provides the data needed to Marshal.dump a criteria.

Note :mongo is written here for backwards compatibility with ::Mongoid 7 and earlier.

Examples:

Dump the criteria.

Marshal.dump(criteria)

Returns:

  • (Array<Object>)

    The dumped data.

[ GitHub ]

  
# File 'lib/mongoid/criteria/marshalable.rb', line 20

def marshal_dump
  data = [ klass, :mongo, inclusions, documents, strategy, negating ]
  data.push(scoping_options).push(dump_hash(:selector)).push(dump_hash(:options))
end

#marshal_load(data)

Resets the criteria object after a Marshal.load

Examples:

Load the criteria.

Marshal.load(criteria)

Parameters:

  • data (Array)

    The raw data.

[ GitHub ]

  
# File 'lib/mongoid/criteria/marshalable.rb', line 31

def marshal_load(data)
  @scoping_options, raw_selector, raw_options = data.pop(3)
  @klass, driver, @inclusions, @documents, @strategy, @negating = data

  if driver == :mongo1x
    raise NotImplementedError, "Mongoid no longer supports marshalling with driver version 1.x."
  end

  @selector = load_hash(Queryable::Selector, raw_selector)
  @options = load_hash(Queryable::Options, raw_options)
end