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.
46 47 48 49 50 51 52 |
# File 'lib/fuzzy.rb', line 46 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)
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/fuzzy.rb', line 61 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 if @limit && @limit < results.length # Partial sort: O(n log k) via heap selection instead of full O(n log n) sort results = results.max_by(@limit) { |_, _, score| score } else results.sort_by! { |_, _, score| -score } end results.each(&block) end |
#limit(n) ⇒ Object
Set maximum number of results
55 56 57 58 |
# File 'lib/fuzzy.rb', line 55 def limit(n) @limit = n self end |