lib/picky/internals/query/token.rb in picky-2.0.0 vs lib/picky/internals/query/token.rb in picky-2.1.0
- old
+ new
@@ -26,19 +26,23 @@
# Returns a qualified and normalized token.
#
# Note: Use this in the search engine if you need a qualified
# and normalized token. I.e. one prepared for a search.
#
- def self.processed text
- token = new text
- token.qualify
- token.extract_original
- token.partialize
- token.similarize
- token.remove_illegals
- token
+ def self.processed text, downcase = true
+ new(text).process downcase
end
+ def process downcases = true
+ qualify
+ extract_original
+ downcase if downcases
+ partialize
+ similarize
+ remove_illegals
+ symbolize
+ self
+ end
# This returns a predefined category name if the user has given one.
#
def user_defined_category_name
@qualifier
@@ -54,10 +58,16 @@
end
def extract_original
@original = @text.dup
end
+ # Downcases the text.
+ #
+ def downcase
+ @text.downcase!
+ end
+
# Partial is a conditional setter.
#
# It is only settable if it hasn't been set yet.
#
def partial= partial
@@ -67,19 +77,23 @@
!@similar && @partial
end
# If the text ends with *, partialize it. If with ", don't.
#
+ # The latter wins. So "hello*" will not be partially searched.
+ #
@@no_partial = /\"\Z/
@@partial = /\*\Z/
def partialize
- self.partial = false and return if @text =~ @@no_partial
- self.partial = true if @text =~ @@partial
+ self.partial = false and return unless @text !~ @@no_partial
+ self.partial = true unless @text !~ @@partial
end
# If the text ends with ~ similarize it. If with ", don't.
#
+ # The latter wins.
+ #
@@no_similar = /\"\Z/
@@similar = /\~\Z/
def similarize
self.similar = false and return if @text =~ @@no_similar
self.similar = true if @text =~ @@similar
@@ -94,26 +108,15 @@
@@illegals = /["*~]/
def remove_illegals
@text.gsub! @@illegals, '' unless @text.blank?
end
- # Visitor for tokenizer.
#
- # TODO Rewrite!!!
#
- def tokenize_with tokenizer
- @text = tokenizer.normalize @text
+ def symbolize
+ @text = @text.to_sym
end
- # TODO spec!
- #
- # TODO Rewrite!!
- #
- def tokenized tokenizer
- tokenizer.tokenize(@text.to_s).each do |text|
- yield text
- end
- end
# Returns an array of possible combinations.
#
def possible_combinations_in index
index.possible_combinations self
@@ -177,9 +180,15 @@
# Internal identifier.
#
def identifier
"#{similar?? :similarity : :index}:#{@text}"
+ end
+
+ # If the originals & the text are the same, they are the same.
+ #
+ def == other
+ self.original == other.original && self.text == other.text
end
# Displays the qualifier text and the text, joined.
#
# e.g. name:meier
\ No newline at end of file