Class: Locomotive::Steam::Adapters::MongoDB::Query
- Inherits:
-
Object
- Object
- Locomotive::Steam::Adapters::MongoDB::Query
- Defined in:
- lib/locomotive/steam/adapters/mongodb/query.rb
Constant Summary collapse
- SYMBOL_OPERATORS =
%w(all elem_match exists gt gte in lt lte mod ne near near_sphere nin with_size with_type within_box within_circle within_polygon within_spherical_circle)
Instance Attribute Summary collapse
-
#criteria ⇒ Object
readonly
Returns the value of attribute criteria.
-
#sort ⇒ Object
readonly
Returns the value of attribute sort.
Instance Method Summary collapse
- #against(collection) ⇒ Object
-
#initialize(scope, localized_attributes, &block) ⇒ Query
constructor
A new instance of Query.
- #key(name, operator) ⇒ Object (also: #k)
- #limit(limit) ⇒ Object
- #offset(offset) ⇒ Object
- #only(*args) ⇒ Object
- #order_by(*args) ⇒ Object
- #to_origin ⇒ Object
- #where(criterion = nil) ⇒ Object
Constructor Details
#initialize(scope, localized_attributes, &block) ⇒ Query
Returns a new instance of Query.
11 12 13 14 15 16 17 18 |
# File 'lib/locomotive/steam/adapters/mongodb/query.rb', line 11 def initialize(scope, localized_attributes, &block) @criteria, @sort, @fields, @skip, @limit = {}, nil, nil, nil, nil @scope, @localized_attributes = scope, localized_attributes apply_default_scope instance_eval(&block) if block_given? end |
Instance Attribute Details
#criteria ⇒ Object (readonly)
Returns the value of attribute criteria.
9 10 11 |
# File 'lib/locomotive/steam/adapters/mongodb/query.rb', line 9 def criteria @criteria end |
#sort ⇒ Object (readonly)
Returns the value of attribute sort.
9 10 11 |
# File 'lib/locomotive/steam/adapters/mongodb/query.rb', line 9 def sort @sort end |
Instance Method Details
#against(collection) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/locomotive/steam/adapters/mongodb/query.rb', line 46 def against(collection) _query = to_origin selector, fields, sort = _query.selector, _query.[:fields], _query.[:sort] results = collection.find(selector) results = results.sort(sort) if sort results = results.projection(fields) if fields results = results.skip(@skip) if @skip results = results.limit(@limit) if @limit results end |
#key(name, operator) ⇒ Object Also known as: k
63 64 65 |
# File 'lib/locomotive/steam/adapters/mongodb/query.rb', line 63 def key(name, operator) :"#{name}".send(operator.to_sym) end |
#limit(limit) ⇒ Object
42 43 44 |
# File 'lib/locomotive/steam/adapters/mongodb/query.rb', line 42 def limit(limit) self.tap { @limit = limit } end |
#offset(offset) ⇒ Object
38 39 40 |
# File 'lib/locomotive/steam/adapters/mongodb/query.rb', line 38 def offset(offset) self.tap { @skip = offset } end |
#only(*args) ⇒ Object
32 33 34 35 36 |
# File 'lib/locomotive/steam/adapters/mongodb/query.rb', line 32 def only(*args) self.tap do @fields = [*args] end end |
#order_by(*args) ⇒ Object
26 27 28 29 30 |
# File 'lib/locomotive/steam/adapters/mongodb/query.rb', line 26 def order_by(*args) self.tap do @sort = decode_order_by(*args) end end |
#to_origin ⇒ Object
59 60 61 |
# File 'lib/locomotive/steam/adapters/mongodb/query.rb', line 59 def to_origin build_origin_query.only(@fields).where(@criteria).order_by(*@sort) end |
#where(criterion = nil) ⇒ Object
20 21 22 23 24 |
# File 'lib/locomotive/steam/adapters/mongodb/query.rb', line 20 def where(criterion = nil) self.tap do @criteria.merge!(decode_symbol_operators(criterion)) unless criterion.nil? end end |