Sha256: d8b7f2501270dc6fb94eb1ad5fb403a28f6c2ea7c2d5968b7e892ebc664bfd35

Contents?: true

Size: 1.35 KB

Versions: 27

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true
# encoding: utf-8

module Mongoid
  class Criteria
    module Marshalable

      # Provides the data needed to Marshal.dump a criteria.
      #
      # @example Dump the criteria.
      #   Marshal.dump(criteria)
      #
      # @return [ Array<Object> ] The dumped data.
      #
      # @since 3.0.15
      def marshal_dump
        data = [ klass, driver, inclusions, documents, strategy, negating ]
        data.push(scoping_options).push(dump_hash(:selector)).push(dump_hash(:options))
      end

      # Resets the criteria object after a Marshal.load
      #
      # @example Load the criteria.
      #   Marshal.load(criteria)
      #
      # @param [ Array ] data The raw data.
      #
      # @since 3.0.15
      def marshal_load(data)
        @scoping_options, raw_selector, raw_options = data.pop(3)
        @klass, @driver, @inclusions, @documents, @strategy, @negating = data
        @selector = load_hash(Queryable::Selector, raw_selector)
        @options = load_hash(Queryable::Options, raw_options)
      end

      private

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

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

Version data entries

27 entries across 27 versions & 2 rubygems

Version Path
mongoid-7.3.5 lib/mongoid/criteria/marshalable.rb
mongoid-7.3.4 lib/mongoid/criteria/marshalable.rb
mongoid-7.1.11 lib/mongoid/criteria/marshalable.rb
mongoid-7.2.6 lib/mongoid/criteria/marshalable.rb
mongoid-7.3.3 lib/mongoid/criteria/marshalable.rb
mongoid-7.3.2 lib/mongoid/criteria/marshalable.rb
mongoid-7.2.5 lib/mongoid/criteria/marshalable.rb
mongoid-7.1.10 lib/mongoid/criteria/marshalable.rb
mongoid-7.1.9 lib/mongoid/criteria/marshalable.rb
mongoid-7.2.4 lib/mongoid/criteria/marshalable.rb
mongoid-7.3.1 lib/mongoid/criteria/marshalable.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/mongoid-7.1.7/lib/mongoid/criteria/marshalable.rb
mongoid-7.3.0 lib/mongoid/criteria/marshalable.rb
mongoid-7.2.3 lib/mongoid/criteria/marshalable.rb
mongoid-7.1.8 lib/mongoid/criteria/marshalable.rb
mongoid-7.2.2 lib/mongoid/criteria/marshalable.rb
mongoid-7.2.1 lib/mongoid/criteria/marshalable.rb
mongoid-7.1.7 lib/mongoid/criteria/marshalable.rb
mongoid-7.2.0 lib/mongoid/criteria/marshalable.rb
mongoid-7.1.6 lib/mongoid/criteria/marshalable.rb