Module: MongoMapper::Mixins::Indexer

Defined in:
lib/mixins/indexer.rb

Overview

Mixin for MongoMapper adding indexing stuff. See class ClassMethods for details.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Bootstrap method to include patches with.

Parameters:

base

Class to include class methods of module into


21
22
23
# File 'lib/mixins/indexer.rb', line 21

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#save_callbackObject

current class.

Samples:

class Post < MongoMapper::Document
  use_database SERVER.default_database

  property :title
  property :body

  fulltext_index :title, :body
end

Post.by_fulltext_index('first')
=> [...]
post = Post.by_fulltext_index('this is @title post').first
post.title
=> "First Post"
post.class
=> Post

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mixins/indexer.rb', line 58

def save_callback()
  object = self
  if object.id.nil?
    idsize = fulltext_opts[:idsize] || 32
    limit = (1 << idsize) - 1
    
    while true
      id = rand(limit)
      candidate = "#{self.class.to_s}-#{id}"
      
      begin
        object.class.find(candidate) # Resource not found exception if available
      rescue MongoMapper::DocumentNotFound
        object.id = candidate
        break
      end
    end
  end
end