Class: Mongoid::MapReduce::Results
- Inherits:
-
Array
- Object
- Array
- Mongoid::MapReduce::Results
- Defined in:
- lib/mongoid/mapreduce/results.rb
Instance Method Summary collapse
-
#[](value) ⇒ Object
Allow [] to be used to find records by key.
-
#counts ⇒ Object
Simplifies the Results to a Hash containing only a key and a single value (the count).
-
#find(value) ⇒ Object
Search for contained Documents by key.
-
#has_key?(value) ⇒ Boolean
Determines whether or not a key exists.
-
#keys ⇒ Object
Returns a list of keys from contained documents.
-
#to_hash ⇒ Object
Converts the Results to a Hash.
Instance Method Details
#[](value) ⇒ Object
Allow [] to be used to find records by key
value - Value of the index or value to search by
Return record at index if Fixnum, or result of find(value)
11 12 13 14 15 16 |
# File 'lib/mongoid/mapreduce/results.rb', line 11 def [](value) unless value.is_a? Fixnum return find(value) end super end |
#counts ⇒ Object
Simplifies the Results to a Hash containing only a key and a single value (the count)
Returns Hash
54 55 56 |
# File 'lib/mongoid/mapreduce/results.rb', line 54 def counts self.each.inject({}) {|h, doc| h[doc._key_value.to_s] = doc._count; h } end |
#find(value) ⇒ Object
Search for contained Documents by key
value - Value of the map key
Return Document or nil
23 24 25 26 |
# File 'lib/mongoid/mapreduce/results.rb', line 23 def find(value) self.each {|doc| return doc if doc._key_value == value } nil end |
#has_key?(value) ⇒ Boolean
Determines whether or not a key exists
value - Value of the map key
Returns true or false
40 41 42 |
# File 'lib/mongoid/mapreduce/results.rb', line 40 def has_key?(value) find(value) ? true : false end |
#keys ⇒ Object
Returns a list of keys from contained documents
Returns Array
31 32 33 |
# File 'lib/mongoid/mapreduce/results.rb', line 31 def keys self.collect {|d| d._key_value } end |
#to_hash ⇒ Object
Converts the Results to a Hash
Returns Hash
47 48 49 |
# File 'lib/mongoid/mapreduce/results.rb', line 47 def to_hash self.each.inject({}){|h, doc| h[doc._key_value.to_s] = doc.to_hash; h } end |