Sha256: 4733e786c044733342be6783d354cde74530d5700a1c8ca6ffa3e452100697c1

Contents?: true

Size: 1.63 KB

Versions: 3

Compression:

Stored size: 1.63 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  module Criterion #:nodoc:

    # Complex criterion are used when performing operations on symbols to get
    # get a shorthand syntax for where clauses.
    #
    # @example Conversion of a simple to complex criterion.
    #   { :field => { "$lt" => "value" } }
    #   becomes:
    #   { :field.lt => "value }
    class Complex
      attr_accessor :key, :operator

      # Create the new complex criterion.
      #
      # @example Instantiate a new complex criterion.
      #   Complex.new(:key => :field, :operator => "$gt")
      #
      # @param [ Hash ] opts The options to convert.
      def initialize(opts = {})
        @key, @operator = opts[:key], opts[:operator]
      end

      # Get the criterion as a hash.
      #
      # @example Get the criterion as a hash.
      #   criterion.hash
      #
      # @return [ Hash ] The keys and operators.
      def hash
        [@key, @operator].hash
      end

      # Is the criterion equal to the other?
      #
      # @example Check equality.
      #   criterion.eql?(other)
      #
      # @param [ Complex ] other The other complex criterion.
      #
      # @return [ true, false ] If they are equal.
      def eql?(other)
        self == (other)
      end

      # Is the criterion equal to the other?
      #
      # @example Check equality.
      #   criterion == other
      #
      # @param [ Complex ] other The other complex criterion.
      #
      # @return [ true, false ] If they are equal.
      def ==(other)
        return false unless other.is_a?(self.class)
        self.key == other.key && self.operator == other.operator
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mongoid-2.1.2 lib/mongoid/criterion/complex.rb
mongoid-2.1.1 lib/mongoid/criterion/complex.rb
mongoid-2.1.0 lib/mongoid/criterion/complex.rb