Class: Cuprum::Collections::Query
- Inherits:
-
Object
- Object
- Cuprum::Collections::Query
- Includes:
- Enumerable
- Defined in:
- lib/cuprum/collections/query.rb
Overview
Abstract base class for collection Query implementations.
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize(scope: nil) ⇒ Query
constructor
A new instance of Query.
-
#limit(count = UNDEFINED) ⇒ Object
Sets or returns the maximum number of items returned by the query.
-
#offset(count = UNDEFINED) ⇒ Object
Sets or returns the number of ordered items skipped by the query.
-
#order(*attributes) ⇒ Query
(also: #order_by)
Returns a copy of the query with the specified order.
-
#reset ⇒ Cuprum::Collections::Query
Returns a copy of the query with no cached query results.
-
#scope ⇒ Cuprum::Collections::Scopes::Base
Returns the current scope for the query.
- #where ⇒ Object
Constructor Details
#initialize(scope: nil) ⇒ Query
Returns a new instance of Query.
17 18 19 20 21 22 |
# File 'lib/cuprum/collections/query.rb', line 17 def initialize(scope: nil) @limit = nil @offset = nil @order = {} @scope = scope ? default_scope.and(scope) : default_scope end |
Instance Method Details
#limit ⇒ Integer? #limit(count) ⇒ Query
Sets or returns the maximum number of items returned by the query.
53 54 55 56 57 58 59 |
# File 'lib/cuprum/collections/query.rb', line 53 def limit(count = UNDEFINED) return @limit if count == UNDEFINED validate_limit(count) dup.tap { |copy| copy.with_limit(count) } end |
#offset ⇒ Integer? #offset(count) ⇒ Query
Sets or returns the number of ordered items skipped by the query.
80 81 82 83 84 85 86 |
# File 'lib/cuprum/collections/query.rb', line 80 def offset(count = UNDEFINED) return @offset if count == UNDEFINED validate_offset(count) dup.tap { |copy| copy.with_offset(count) } end |
#order ⇒ Hash{String,Symbol=>Symbol} #order(*attributes) ⇒ Query #order(attributes) ⇒ Query Also known as: order_by
Returns a copy of the query with the specified order.
The query will find the matching items, sort them in the specified order, and then apply limit and/or offset (if applicable) to determine the final returned items.
When #order is called on a query that already defines an ordering, the old ordering is replaced with the new.
128 129 130 131 132 133 134 |
# File 'lib/cuprum/collections/query.rb', line 128 def order(*attributes) return @order if attributes.empty? normalized = Cuprum::Collections::Queries::Ordering.normalize(*attributes) dup.tap { |copy| copy.with_order(normalized) } end |
#reset ⇒ Cuprum::Collections::Query
Returns a copy of the query with no cached query results.
Once the query has been called (e.g. by calling #each or #to_a), the matching data is cached. If the underlying collection changes, those changes will not be reflected in the query.
Calling #reset clears the cached results. The next time the query is called, the results will be drawn from the current collection state.
148 149 150 |
# File 'lib/cuprum/collections/query.rb', line 148 def reset dup.reset! end |
#scope ⇒ Cuprum::Collections::Scopes::Base
Returns the current scope for the query.
Composition methods should not be called on the scope directly, as they will not change the scope object bound to the query. Call the corresponding methods on the query itself, i.e. call query.where() instead of query.scope.where().
33 34 35 |
# File 'lib/cuprum/collections/query.rb', line 33 def scope @scope ||= default_scope end |
#where(hash) ⇒ Object #where(&block) ⇒ Object
179 180 181 |
# File 'lib/cuprum/collections/query.rb', line 179 def where(...) dup.with_scope(scope.where(...)).reset! end |