lib/fuzzy_match/wrapper.rb in fuzzy_match-1.0.5 vs lib/fuzzy_match/wrapper.rb in fuzzy_match-1.1.0
- old
+ new
@@ -1,24 +1,29 @@
class FuzzyMatch
# Wrappers are the tokens that are passed around when doing scoring and optimizing.
class Wrapper #:nodoc: all
attr_reader :fuzzy_match
attr_reader :record
- attr_reader :read
+ attr_reader :literal
+ attr_reader :rendered
- def initialize(fuzzy_match, record, read = nil)
+ def initialize(fuzzy_match, record, literal = false)
@fuzzy_match = fuzzy_match
@record = record
- @read = read
+ @literal = literal
end
def inspect
"#<Wrapper render=#{render} variants=#{variants.length}>"
end
+
+ def read
+ fuzzy_match.read unless literal
+ end
def render
- return @render if rendered?
+ return @render if rendered
str = case read
when ::Proc
read.call record
when ::Symbol
if record.respond_to?(read)
@@ -40,11 +45,12 @@
@render
end
alias :to_str :render
- WORD_BOUNDARY = %r{\s*\b\s*}
+ # "Foo's Bar" should be treated as [ "Foo's", "Bar" ], so we don't use traditional regexp word boundaries (\b)
+ WORD_BOUNDARY = %r{\s+}
def words
@words ||= render.split(WORD_BOUNDARY)
end
def similarity(other)
@@ -56,12 +62,8 @@
if tightener.apply? render
memo.push tightener.apply(render)
end
memo
end.uniq
- end
-
- def rendered?
- @rendered == true
end
end
end