Class: Reditor::LibrarySearchQuery

Inherits:
Object
  • Object
show all
Includes:
BundlerSupport
Defined in:
lib/reditor/library_search_query.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BundlerSupport

#bundler_specs

Constructor Details

#initialize(query, options = {}) ⇒ LibrarySearchQuery

Returns a new instance of LibrarySearchQuery.



15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#half_patternObject (readonly)

Returns the value of attribute half_pattern.



13
14
15
# File 'lib/reditor/library_search_query.rb', line 13

def half_pattern
  @half_pattern
end

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/reditor/library_search_query.rb', line 12

def options
  @options
end

#partial_patternObject (readonly)

Returns the value of attribute partial_pattern.



13
14
15
# File 'lib/reditor/library_search_query.rb', line 13

def partial_pattern
  @partial_pattern
end

#queryObject (readonly)

Returns the value of attribute query.



12
13
14
# File 'lib/reditor/library_search_query.rb', line 12

def query
  @query
end

Class Method Details

.search(query, options = {}) ⇒ Object



8
9
10
# File 'lib/reditor/library_search_query.rb', line 8

def self.search(query, options = {})
  new(query, options).search
end

Instance Method Details

#candidatesObject



30
31
32
33
34
35
36
# File 'lib/reditor/library_search_query.rb', line 30

def candidates
  @candidates ||= (
    candidates_from_loadpath +
    candidates_from_gem      +
    candidates_from_bundler
  ).uniq
end

#search(limit = options[:limit]) ⇒ Object



24
25
26
27
28
# File 'lib/reditor/library_search_query.rb', line 24

def search(limit = options[:limit])
  candidates.sort_by {|name|
    [*match_scores(name), *distance_scores(name), name]
  }.take(limit)
end