Class: Fuzzy::MatchResult
Overview
Enumerator wrapper that supports .limit() and .each
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Iterate over matches: yields (entry_data, highlight_positions, score).
-
#initialize(entries, query) ⇒ MatchResult
constructor
A new instance of MatchResult.
-
#limit(n) ⇒ Object
Set maximum number of results.
Constructor Details
#initialize(entries, query) ⇒ MatchResult
Returns a new instance of MatchResult.
44 45 46 47 48 49 50 |
# File 'lib/fuzzy.rb', line 44 def initialize(entries, query) @entries = entries @query = query @query_lower = query.downcase @query_chars = @query_lower.chars @limit = nil end |
Instance Method Details
#each(&block) ⇒ Object
Iterate over matches: yields (entry_data, highlight_positions, score)
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/fuzzy.rb', line 59 def each(&block) return enum_for(:each) unless block_given? results = [] @entries.each do |entry| score, positions = calculate_match(entry) next if score.nil? # No match results << [entry.data, positions, score] end # Sort by score descending results.sort_by! { |_, _, score| -score } # Apply limit results = results.first(@limit) if @limit results.each(&block) end |
#limit(n) ⇒ Object
Set maximum number of results
53 54 55 56 |
# File 'lib/fuzzy.rb', line 53 def limit(n) @limit = n self end |