Sha256: 085519dcc15a7433d4306bd17554df01b5ee929819aa38fa1f60308982bddbef

Contents?: true

Size: 1.55 KB

Versions: 2

Compression:

Stored size: 1.55 KB

Contents

# encoding: utf-8

module XapianDb
  module Adapters

    # 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 <code>xapian_id</code> to an indexed class
    # @author Gernot Kogler
    class GenericAdapter

      class << self

        # Define the unique key expression
        # @example 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
        def unique_key(&block)
          @unique_key_block = block
        end

        # Implement the class helper methods
        # @param [Class] klass The class to add the helper methods to
        def add_class_helper_methods_to(klass)
          raise "Unique key is not configured for generic adapter!" if @unique_key_block.nil?
          expression = @unique_key_block
          klass.instance_eval do
            define_method(:xapian_id) do
              instance_eval &expression
            end
          end
        end

        # Implement the document helper methods on a module. So far there are none
        # @param [Module] a_module The module to add the helper methods to
        def add_doc_helper_methods_to(obj)
          # We have none so far
        end

      end

    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
xapian_db-0.3.3 lib/xapian_db/adapters/generic_adapter.rb
xapian_db-0.3.2 lib/xapian_db/adapters/generic_adapter.rb