Sha256: 6b4aa31c2f7e3e7f1fd63f73662fe49c76566f10ddce7f3e33b7186c782a03f5

Contents?: true

Size: 1.27 KB

Versions: 8

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

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.
      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.
      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

8 entries across 8 versions & 1 rubygems

Version Path
mongoid-7.5.4 lib/mongoid/criteria/marshalable.rb
mongoid-7.5.3 lib/mongoid/criteria/marshalable.rb
mongoid-7.5.2 lib/mongoid/criteria/marshalable.rb
mongoid-7.5.1 lib/mongoid/criteria/marshalable.rb
mongoid-7.4.3 lib/mongoid/criteria/marshalable.rb
mongoid-7.5.0 lib/mongoid/criteria/marshalable.rb
mongoid-7.4.1 lib/mongoid/criteria/marshalable.rb
mongoid-7.4.0 lib/mongoid/criteria/marshalable.rb