Class: Cuprum::Collections::Basic::Query

Inherits:
Query
  • Object
show all
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

Methods inherited from Query

#criteria, #limit, #offset, #order, #reset, #where

Constructor Details

#initialize(data) ⇒ Query

Returns a new instance of Query.

Parameters:

  • data (Array<Hash>)

    The current data in the collection. Should be an Array of Hashes, each of which represents one item in the collection.



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

#eachEnumerator #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.

Overloads:

  • #eachEnumerator

    Returns an enumerator that iterates over the sorted, matching items within the given offset and limit.

    Returns:

    • (Enumerator)

      an enumerator that iterates over the sorted, matching items within the given offset and limit.

  • #each {|Object| ... } ⇒ Object

    Yields:

    • (Object)

      Each sorted, matching item within the given offset and limit is yielded to the block.

See Also:



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.

Returns:

  • (Boolean)

    true if any items match the query; otherwise 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_aArray

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.

Returns:

  • (Array)

    The sorted, matching items within the given offset and limit.

See Also:



92
93
94
# File 'lib/cuprum/collections/basic/query.rb', line 92

def to_a
  filtered_data
end