lib/picky/indexes/index_indexing.rb in picky-3.0.0.pre1 vs lib/picky/indexes/index_indexing.rb in picky-3.0.0.pre2
- old
+ new
@@ -4,12 +4,11 @@
#
#
class Index
- attr_reader :after_indexing,
- :bundle_class
+ attr_reader :bundle_class
# Delegators for indexing.
#
delegate :cache,
:check,
@@ -33,10 +32,23 @@
categories.each &:index
end
end
end
+ # Define an index tokenizer on the index.
+ #
+ # Parameters are the exact same as for indexing.
+ #
+ def indexing options = {}
+ @tokenizer = if options.respond_to?(:tokenize)
+ options
+ else
+ options && Tokenizers::Index.new(options)
+ end
+ end
+ alias define_indexing indexing
+
# Check if the given enumerable source is empty.
#
# Note: Checking as early as possible to tell the
# user as early as possible.
#
@@ -66,19 +78,10 @@
indexer = Indexers::Parallel.new self
indexer.index categories
categories.each &:cache
end
- # Define an index tokenizer on the index.
- #
- # Parameters are the exact same as for indexing.
- #
- def indexing options = {}
- @tokenizer = Tokenizers::Index.new options
- end
- alias define_indexing indexing
-
# Returns the installed tokenizer or the default.
#
def tokenizer
@tokenizer || Indexes.tokenizer
end
@@ -100,10 +103,11 @@
#
def extract_source
@source = @source.respond_to?(:call) ? @source.call : @source
end
def define_source source
+ check_source source
@source = source
end
def raise_no_source
raise NoSourceSpecifiedException.new(<<-NO_SOURCE
@@ -117,19 +121,42 @@
end
NO_SOURCE
)
end
+ def check_source source # :nodoc:
+ raise ArgumentError.new(<<-SOURCE
+
+ The index "#{name}" should use a data source that responds to either the method #each, or the method #harvest, which yields(id, text), OR it can be a lambda/block, returning such a source.
+ Or it could use one of the built-in sources:
+ Sources::#{(Sources.constants - [:Base, :Wrappers, :NoCSVFileGiven, :NoCouchDBGiven]).join(',
+ Sources::')}
+
+
+ SOURCE
+ ) unless source.respond_to?(:each) || source.respond_to?(:harvest) || source.respond_to?(:call)
+ end
+
# Define a key_format on the index.
#
# Parameter is a method name to use on the key (e.g. :to_i, :to_s, :strip).
#
def key_format format = nil
format ? define_key_format(format) : @key_format
end
def define_key_format key_format
@key_format = key_format
+ end
+
+ # Define what to do after indexing.
+ # (Only used in the Sources::DB)
+ #
+ def after_indexing after_indexing = nil
+ after_indexing ? define_after_indexing(after_indexing) : @after_indexing
+ end
+ def define_after_indexing after_indexing
+ @after_indexing = after_indexing
end
end
end
\ No newline at end of file