Sha256: 2885e8cbd374a2578d0901fedf4da48b4b32987addc9d6df5318785d06ed82a0

Contents?: true

Size: 1.88 KB

Versions: 14

Compression:

Stored size: 1.88 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

      # Returns the name of the key as a string.
      #
      # @example Get the name of the key.
      #   criterion.to_s
      #
      # @return [ String ] The field name.
      #
      # @since 2.1.0
      def to_s
        key.to_s
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
mongoid-2.2.6 lib/mongoid/criterion/complex.rb
mongoid-2.2.5 lib/mongoid/criterion/complex.rb
mongoid-2.2.4 lib/mongoid/criterion/complex.rb
mongoid-2.2.3 lib/mongoid/criterion/complex.rb
mongoid-2.2.2 lib/mongoid/criterion/complex.rb
mongoid-2.2.1 lib/mongoid/criterion/complex.rb
mongoid-2.2.0 lib/mongoid/criterion/complex.rb
mongoid-2.1.9 lib/mongoid/criterion/complex.rb
mongoid-2.1.8 lib/mongoid/criterion/complex.rb
mongoid-2.1.7 lib/mongoid/criterion/complex.rb
mongoid-2.1.6 lib/mongoid/criterion/complex.rb
mongoid-2.1.5 lib/mongoid/criterion/complex.rb
mongoid-2.1.4 lib/mongoid/criterion/complex.rb
mongoid-2.1.3 lib/mongoid/criterion/complex.rb