Class: Mongoid::MapReduce::Results

Inherits:
Array
  • Object
show all
Defined in:
lib/mongoid/mapreduce/results.rb

Instance Method Summary collapse

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

#countsObject

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

Returns:

  • (Boolean)


40
41
42
# File 'lib/mongoid/mapreduce/results.rb', line 40

def has_key?(value)
  find(value) ? true : false
end

#keysObject

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_hashObject

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