class Reditor::LibrarySearchQuery

Attributes

half_pattern[R]
options[R]
partial_pattern[R]
query[R]

Public Class Methods

new(query, options = {}) click to toggle source
# File lib/reditor/library_search_query.rb, line 15
def initialize(query, options = {})
  @query   = query.to_s
  @options = {limit: 20, global: false}.merge(options)

  quoted = Regexp.quote(query)
  @half_pattern    = /^#{quoted}|#{quoted}$/i
  @partial_pattern = /#{quoted}/i
end

Public Instance Methods

candidates() click to toggle source
# File lib/reditor/library_search_query.rb, line 30
def candidates
  @candidates ||= (
    candidates_from_loadpath +
    candidates_from_gem      +
    candidates_from_bundler
  ).uniq
end

Private Instance Methods

candidates_from_bundler() click to toggle source
# File lib/reditor/library_search_query.rb, line 52
def candidates_from_bundler
  return [] if options[:global]

  bundler_specs.map(&:name)
end
candidates_from_gem() click to toggle source
# File lib/reditor/library_search_query.rb, line 58
def candidates_from_gem
  Gem::Specification.map(&:name)
rescue Gem::LoadError
  []
end
candidates_from_loadpath() click to toggle source
# File lib/reditor/library_search_query.rb, line 64
def candidates_from_loadpath
  $LOAD_PATH.each_with_object([]) {|path, memo|
    begin
      Pathname(File.expand_path(path)).entries.each do |entry|
          memo << entry.basename('.rb').to_s if entry.extname == '.rb'
      end
    rescue Errno::ENOENT
      # maybe load path is invalid
    end
  }
end
distance_scores(name) click to toggle source
# File lib/reditor/library_search_query.rb, line 48
def distance_scores(name)
  [Hotwater.damerau_levenshtein_distance(query, name)]
end
match_scores(name) click to toggle source
# File lib/reditor/library_search_query.rb, line 40
def match_scores(name)
  words         = name.split(/-_/)
  half_count    = words.grep(half_pattern).count
  partial_count = words.grep(partial_pattern).count

  [-half_count, -partial_count]
end