class XapianDb::Adapters::DatamapperAdapter

Adapter for Datamapper. To use it, configure it like this:

XapianDb::Config.setup do |config|
  config.adapter :datamapper
end

This adapter does the following:

@author Gernot Kogler

Public Class Methods

add_class_helper_methods_to(klass) click to toggle source

Implement the class helper methods @param [Class] klass The class to add the helper methods to

   # File lib/xapian_db/adapters/datamapper_adapter.rb
33 def add_class_helper_methods_to(klass)
34 
35   # Add the helpers from the base class
36   super klass
37 
38   klass.instance_eval do
39     # define the method to retrieve a unique key
40     define_method(:xapian_id) do
41       "#{self.class}-#{self.id}"
42     end
43 
44     def order_condition(primary_key)
45       primary_key.to_sym
46     end
47   end
48 
49   klass.class_eval do
50     # add the after save/destroy logic, unless the blueprint has autoindexing turned off
51     if XapianDb::DocumentBlueprint.blueprint_for(klass.name).autoindex?
52       after :save do
53         blueprint = XapianDb::DocumentBlueprint.blueprint_for klass.to_s
54         if blueprint.should_index?(self)
55           XapianDb.index(self)
56         else
57           XapianDb.delete_doc_with(self.xapian_id)
58         end
59       end
60 
61       after :destroy do
62         XapianDb.delete_doc_with(self.xapian_id)
63       end
64     end
65 
66     # Add a method to reindex all models of this class
67     define_singleton_method(:rebuild_xapian_index) do |options={}|
68       options[:primary_key] = klass.serial.name
69       XapianDb.reindex_class(self, options)
70     end
71   end
72 
73 end
add_doc_helper_methods_to(a_module) click to toggle source

Implement the document helper methods on a module @param [Module] a_module The module to add the helper methods to

    # File lib/xapian_db/adapters/datamapper_adapter.rb
 77 def add_doc_helper_methods_to(a_module)
 78   a_module.instance_eval do
 79 
 80     include XapianDb::Utilities
 81 
 82     # Implement access to the model id
 83     define_method :id do
 84       return @id unless @d.nil?
 85       # retrieve the class and id from data
 86       klass_name, id = data.split("-")
 87       @id = id.to_i
 88     end
 89 
 90     # Implement access to the indexed object
 91     define_method :indexed_object do
 92       return @indexed_object unless @indexed_object.nil?
 93       # retrieve the class and id from data
 94       klass_name, id = data.split("-")
 95       klass = constantize klass_name
 96       @indexed_object = klass.get(id.to_i)
 97     end
 98   end
 99 
100 end
order_condition(primary_key) click to toggle source
   # File lib/xapian_db/adapters/datamapper_adapter.rb
44 def order_condition(primary_key)
45   primary_key.to_sym
46 end
primary_key_for(klass) click to toggle source

return the name of the primary key column of a class @param [Class] klass the class @return [Symbol] the name of the primary key column

   # File lib/xapian_db/adapters/datamapper_adapter.rb
27 def primary_key_for(klass)
28   klass.serial.name
29 end