class Solargraph::Pin::Search

Public Class Methods

new(pins, query) click to toggle source

@param pins [Array<Pin::Base>] @param query [String]

# File lib/solargraph/pin/search.rb, line 22
def initialize pins, query
  @pins = pins
  @query = query
end

Public Instance Methods

results() click to toggle source

@return [Array<Pin::Base>]

# File lib/solargraph/pin/search.rb, line 28
def results
  @results ||= do_query
end

Private Instance Methods

do_query() click to toggle source

@return [Array<Pin::Base>]

# File lib/solargraph/pin/search.rb, line 35
def do_query
  return @pins if @query.nil? || @query.empty?
  @pins.map do |pin|
    match = [fuzzy_string_match(pin.path, @query), fuzzy_string_match(pin.name, @query)].max
    Result.new(match, pin) if match > 0.7
  end
    .compact
    .sort { |a, b| b.match <=> a.match }
    .map(&:pin)
end
fuzzy_string_match(str1, str2) click to toggle source

@param str1 [String] @param str2 [String] @return [Float]

# File lib/solargraph/pin/search.rb, line 49
def fuzzy_string_match str1, str2
  return (1.0 + (str2.length.to_f / str1.length.to_f)) if str1.downcase.include?(str2.downcase)
  JaroWinkler.distance(str1, str2, ignore_case: true)
end