Class: Mongoid::Criterion::Complex

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/criterion/complex.rb

Overview

Complex criterion are used when performing operations on symbols to get get a shorthand syntax for where clauses.

Examples:

Conversion of a simple to complex criterion.

{ :field => { "$lt" => "value" } }
becomes:
{ :field.lt => "value }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Complex

Create the new complex criterion.

Examples:

Instantiate a new complex criterion.

Complex.new(:key => :field, :operator => "$gt")

Parameters:

  • opts (Hash) (defaults to: {})

    The options to convert.



21
22
23
# File 'lib/mongoid/criterion/complex.rb', line 21

def initialize(opts = {})
  @key, @operator = opts[:key], opts[:operator]
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



13
14
15
# File 'lib/mongoid/criterion/complex.rb', line 13

def key
  @key
end

#operatorObject

Returns the value of attribute operator.



13
14
15
# File 'lib/mongoid/criterion/complex.rb', line 13

def operator
  @operator
end

Instance Method Details

#==(other) ⇒ true, false

Is the criterion equal to the other?

Examples:

Check equality.

criterion == other

Parameters:

  • other (Complex)

    The other complex criterion.

Returns:

  • (true, false)

    If they are equal.



66
67
68
69
# File 'lib/mongoid/criterion/complex.rb', line 66

def ==(other)
  return false unless other.is_a?(self.class)
  self.key == other.key && self.operator == other.operator
end

#eql?(other) ⇒ true, false

Is the criterion equal to the other?

Examples:

Check equality.

criterion.eql?(other)

Parameters:

  • other (Complex)

    The other complex criterion.

Returns:

  • (true, false)

    If they are equal.



54
55
56
# File 'lib/mongoid/criterion/complex.rb', line 54

def eql?(other)
  self == (other)
end

#hashHash

Get the criterion as a hash.

Examples:

Get the criterion as a hash.

criterion.hash

Returns:

  • (Hash)

    The keys and operators.



31
32
33
# File 'lib/mongoid/criterion/complex.rb', line 31

def hash
  [@key, @operator].hash
end

#to_mongo_query(v) ⇒ Hash

Create a mongo query with given value

Examples:

Create query

criterion.to_mongo_hash(value)

Returns:

  • (Hash)

    The query



42
43
44
# File 'lib/mongoid/criterion/complex.rb', line 42

def to_mongo_query(v)
  {"$#{self.operator}" => v}
end

#to_sString

Returns the name of the key as a string.

Examples:

Get the name of the key.

criterion.to_s

Returns:

  • (String)

    The field name.

Since:

  • 2.1.0



79
80
81
# File 'lib/mongoid/criterion/complex.rb', line 79

def to_s
  key.to_s
end