lib/riak/search/result_collection.rb in riak-client-2.2.0.pre1 vs lib/riak/search/result_collection.rb in riak-client-2.2.0
- old
+ new
@@ -6,11 +6,11 @@
# to the {Riak::RObject} instances found, and access through the #docs method
# to the results as returned by Solr.
class ResultCollection
# @return [Riak::Client]
attr_reader :client
-
+
# @return [Hash] the de-serialzed hash returned from Solr
attr_reader :raw
# @return [Numeric] the maximum score found by Solr
attr_reader :max_score
@@ -50,22 +50,75 @@
def empty?
length == 0
end
# @param [Integer] index the index of the [Riak::RObject] to load and return
- # @return [Riak::RObject,NilClass] the found object, or nil if the index
+ # @return [Riak::RObject,NilClass] the found object, or nil if the index
# is out of range
def [](index)
doc = docs[index]
return nil if doc.nil?
- doc.robject
+ doc.object
end
# @return [Riak::RObject,NilClass] the first found object, or nil if the
# index is out of range
def first
self[0]
+ end
+
+ # Materializes and returns an array of objects from search results.
+ # You'll probably need to type inspect its members.
+ #
+ # @return [Array] materialized objects
+ def objects
+ @objects ||= docs.map do |doc|
+ next doc.crdt if doc.crdt?
+ doc.robject
+ end
+ end
+
+ # Materializes [Riak::RObject]s from any key-value results. Refuses to
+ # return RObjects for any CRDT results.
+ #
+ # @return [Array<Riak::RObject>] key-value objects
+ def robjects
+ @robjects ||= docs.reject(&:crdt?).map(&:robject)
+ end
+
+ # Materializes [Riak::Crdt::Base] subclasses from any CRDT results.
+ #
+ # @return [Array<Riak::Crdt::Base>] CRDT objects
+ def crdts
+ @crdts ||= docs.select(&:crdt?).map(&:crdt)
+ end
+
+ # Materializes [Riak::Crdt::Counter] results.
+ #
+ # @return [Array<Riak::Crdt::Counter] counter objects
+ def counters
+ @counters ||= docs.
+ select{ |d| d.type_class == Riak::Crdt::Counter }.
+ map(&:counter)
+ end
+
+ # Materializes [Riak::Crdt::Map] results.
+ #
+ # @return [Array<Riak::Crdt::Map] map objects
+ def maps
+ @maps ||= docs.
+ select{ |d| d.type_class == Riak::Crdt::Map }.
+ map(&:map)
+ end
+
+ # Materializes [Riak::Crdt::Set] results.
+ #
+ # @return [Array<Riak::Crdt::Set>]
+ def sets
+ @sets ||= docs.
+ select{ |d| d.type_class == Riak::Crdt::Set }.
+ map(&:set)
end
# {Enumerable}-compatible iterator method. If a block is given, yields with
# each {Riak::RObject} in the collection. If no block is given, returns an
# {Enumerator} over each {Riak::RObject in the collection.