Module: Noodall::Tagging::ClassMethods

Defined in:
lib/noodall/tagging.rb

Instance Method Summary collapse

Instance Method Details

#tag_cloud(options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/noodall/tagging.rb', line 10

def tag_cloud(options = {})
  return [] if self.count == 0 # Don't bother if there is nothing in this collection
  query = query(options.reverse_merge(
    :order => 'value DESC'
  ))
  tags_map = collection.map_reduce(tag_cloud_map, tag_cloud_reduce, {:query => query.criteria.to_hash, :out => "#{self.collection_name}_tags" })
  if tags_map.count > 0
    # Strange scoping issue means tags needs to be defined outside of the find block
    tags = nil
    tags_map.find({}, query.options.merge(:timeout => false).to_hash) do |cursor|
      tags = cursor.map { |hash| Tag.new(hash['_id'], hash['value']) }
    end
    tags
  else
    []
  end
end

#tag_cloud_mapObject



28
29
30
31
32
33
34
35
# File 'lib/noodall/tagging.rb', line 28

def tag_cloud_map
 "function(){" +
  "this.tags.forEach(" +
  "function(z){" +
  "emit( z , 1 );" +
  "}" +
  ")}"
end

#tag_cloud_reduceObject



37
38
39
40
41
42
43
44
45
# File 'lib/noodall/tagging.rb', line 37

def tag_cloud_reduce
  "function( key , values ){" +
  "var total = 0;" +
  "for ( var i=0; i<values.length; i++ ){" +
  "total += values[i];" +
  "}" +
  "return total;" +
  "}"
end