Class: Mongrep::Query
- Inherits:
-
Object
- Object
- Mongrep::Query
- Defined in:
- lib/mongrep/query.rb
Overview
A mongodb query object
Instance Method Summary collapse
-
#&(other) ⇒ Query
Combines two queries by merging their underlying query hashes.
-
#and(query_hash) ⇒ Query
Combines self with the given query hash by using the MongoDB $and operator.
-
#initialize(query_hash = {}) ⇒ Query
constructor
A new instance of Query.
-
#or(query_hash) ⇒ Query
Combines self with the given query hash by using the MongoDB $or operator.
-
#to_h ⇒ Hash
The underlying query hash.
-
#where(query_hash) ⇒ Query
Combines self with the given query hash by merging it to the existing one.
-
#|(other) ⇒ Query
Combines two queries by using the MongoDB $or operator.
Constructor Details
#initialize(query_hash = {}) ⇒ Query
Returns a new instance of Query.
8 9 10 |
# File 'lib/mongrep/query.rb', line 8 def initialize(query_hash = {}) @query_hash = query_hash.to_h end |
Instance Method Details
#&(other) ⇒ Query
Combines two queries by merging their underlying query hashes
20 21 22 |
# File 'lib/mongrep/query.rb', line 20 def &(other) self.class.new(@query_hash.merge(other.to_h)) end |
#and(query_hash) ⇒ Query
Combines self with the given query hash by using the MongoDB $and operator
70 71 72 |
# File 'lib/mongrep/query.rb', line 70 def and(query_hash) self.class.new(:$and => [@query_hash, query_hash]) end |
#or(query_hash) ⇒ Query
Combines self with the given query hash by using the MongoDB $or operator
59 60 61 |
# File 'lib/mongrep/query.rb', line 59 def or(query_hash) self | self.class.new(query_hash) end |
#to_h ⇒ Hash
Returns The underlying query hash.
75 76 77 |
# File 'lib/mongrep/query.rb', line 75 def to_h @query_hash end |
#where(query_hash) ⇒ Query
This is mainly for usage in Repository#find using a block
Combines self with the given query hash by merging it to the existing one
47 48 49 |
# File 'lib/mongrep/query.rb', line 47 def where(query_hash) self & self.class.new(query_hash) end |
#|(other) ⇒ Query
Combines two queries by using the MongoDB $or operator
33 34 35 |
# File 'lib/mongrep/query.rb', line 33 def |(other) self.class.new(:$or => [@query_hash, other.to_h]) end |