Skip to content

Instantly share code, notes, and snippets.

@timotta
Created April 14, 2011 11:57
Show Gist options
  • Save timotta/919334 to your computer and use it in GitHub Desktop.
Save timotta/919334 to your computer and use it in GitHub Desktop.
# -*- encoding: utf-8 -*-
class Conteudo
include Mongoid::Document
field :titulo, :type => String
field :tags, :type => Array, :default => []
end
# -*- encoding: utf-8 -*-
class Tag
include Mongoid::Document
collection_name = "tags"
def self.gerar
map = <<-JAVASCRIPT
function() {
this.tags.forEach( function(tag) {
emit(tag, { quantidade: 1 } );
})
}
JAVASCRIPT
reduce = <<-JAVASCRIPT
function(key, values) {
var soma = 0;
values.forEach( function(value) {
soma += value.quantidade;
} );
return { quantidade: soma };
}
JAVASCRIPT
Conteudo.collection.map_reduce(map, reduce, {out: collection_name})
end
def self.instantiate(attrs)
super(attrs['value'].merge('valor' => attrs['_id']))
end
def self.hereditary?
return false
end
Mongoid::Persistence.instance_methods.each do |method|
undef_method method
end
end
# -*- encoding: utf-8 -*-
namespace :tags do
desc 'gera tags'
task :tags => :environment do
Tag.gerar
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment