Class: XapianDb::Adapters::GenericAdapter

Inherits:
BaseAdapter
  • Object
show all
Defined in:
lib/xapian_db/adapters/generic_adapter.rb

Overview

The generic adapter is a universal adapater that can be used for any ruby class. To use the generic adapter (which is the default), configure the expression that generates a unique key from your objects using the method 'unique_key'. This adapter does the following:

  • adds the instance method xapian_id to an indexed class

Author:

  • Gernot Kogler

Class Method Summary collapse

Class Method Details

.add_class_helper_methods_to(klass) ⇒ Object

Implement the class helper methods

Parameters:

  • klass (Class)

    The class to add the helper methods to



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/xapian_db/adapters/generic_adapter.rb', line 28

def add_class_helper_methods_to(klass)
  raise "Unique key is not configured for generic adapter!" if @unique_key_block.nil?

  # Add the helpers from the base class
  super klass

  expression = @unique_key_block
  klass.instance_eval do
    define_method(:xapian_id) do
      instance_eval &expression
    end
  end
end

.add_doc_helper_methods_to(obj) ⇒ Object

Implement the document helper methods on a module. So far there are none

Parameters:

  • a_module (Module)

    The module to add the helper methods to



44
45
46
# File 'lib/xapian_db/adapters/generic_adapter.rb', line 44

def add_doc_helper_methods_to(obj)
  # We have none so far
end

.unique_key(&block) ⇒ Object

Define the unique key expression

Examples:

Use the same unique expression like the active record adapter (assuming your objects have an id)

XapianDb::Adapters::GenericAdapter.unique_key do
  "#{self.class}-#{self.id}"
end


22
23
24
# File 'lib/xapian_db/adapters/generic_adapter.rb', line 22

def unique_key(&block)
  @unique_key_block = block
end