Class: Cuprum::Collections::Basic::Query
- Includes:
- Enumerable
- Defined in:
- lib/cuprum/collections/basic/query.rb
Overview
Concrete implementation of a Query for an in-memory collection.
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Iterates through the collection, yielding each item matching the query.
-
#exists? ⇒ Boolean
Checks for the presence of collection items matching the query.
-
#initialize(data) ⇒ Query
constructor
A new instance of Query.
-
#to_a ⇒ Array
Returns an array containing each collection item matching the query.
Methods inherited from Query
#criteria, #limit, #offset, #order, #reset, #where
Constructor Details
#initialize(data) ⇒ Query
Returns a new instance of Query.
14 15 16 17 18 19 20 21 22 |
# File 'lib/cuprum/collections/basic/query.rb', line 14 def initialize(data) super() @data = data @filters = [] @limit = nil @offset = nil @order = {} end |
Instance Method Details
#each ⇒ Enumerator #each {|Object| ... } ⇒ Object
Iterates through the collection, yielding each item matching the query.
If the query has criteria, only items matching each criterion will be processed; these are the matching items. If the query does not have any criteria, all items in the collection will be processed.
If the query has an ordering, the matching items are then sorted in the specified order. If the query does not have an order, the matching items will be processed in the order they appear in the collection.
Finally, the limit and/or offset will be applied to the sorted matching items. Each sorted, matching item starting at the offset and up to the given limit of items will be yielded to the block.
51 52 53 54 55 |
# File 'lib/cuprum/collections/basic/query.rb', line 51 def each(&block) return enum_for(:each) unless block_given? filtered_data.each(&block) end |
#exists? ⇒ Boolean
Checks for the presence of collection items matching the query.
If the query has criteria, then only items matching each criterion will be processed; these are the matching items. If there is at least one matching item, #exists will return true; otherwise, it will return false.
64 65 66 67 68 |
# File 'lib/cuprum/collections/basic/query.rb', line 64 def exists? data.any? do |item| @filters.all? { |filter| filter.call(item) } end end |
#to_a ⇒ Array
Returns an array containing each collection item matching the query.
If the query has criteria, only items matching each criterion will be processed; these are the matching items. If the query does not have any criteria, all items in the collection will be processed.
If the query has an ordering, the matching items are then sorted in the specified order. If the query does not have an order, the matching items will be processed in the order they appear in the collection.
Finally, the limit and/or offset will be applied to the sorted matching items. Each sorted, matching item starting at the offset and up to the given limit of items will be returned in the array.
92 93 94 |
# File 'lib/cuprum/collections/basic/query.rb', line 92 def to_a filtered_data end |