Module: Bond::Search

Included in:
Mission
Defined in:
lib/bond/search.rb

Overview

Contains search methods used to filter possible completions given what the user has typed for that completion.

Instance Method Summary collapse

Instance Method Details

#anywhere_search(input, list) ⇒ Object

Searches completions anywhere in the string.



10
11
12
# File 'lib/bond/search.rb', line 10

def anywhere_search(input, list)
  list.grep(/#{Regexp.escape(input)}/)
end

#default_search(input, list) ⇒ Object

Searches completions from the beginning of the string.



5
6
7
# File 'lib/bond/search.rb', line 5

def default_search(input, list)
  list.grep(/^#{Regexp.escape(input)}/)
end

#ignore_case_search(input, list) ⇒ Object

Searches completions from the beginning and ignores case.



15
16
17
# File 'lib/bond/search.rb', line 15

def ignore_case_search(input, list)
  list.grep(/^#{Regexp.escape(input)}/i)
end

#underscore_search(input, list) ⇒ Object

Searches completions from the beginning but also provides aliasing of underscored words. For example ‘some_dang_long_word’ can be specified as ‘s-d-l-w’. Aliases can be any unique string at the beginning of an underscored word. For example, to choose the first completion between ‘so_long’ and ‘so_larger’, type ‘s-lo’.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bond/search.rb', line 23

def underscore_search(input, list)
  if input.include?("-")
    index = 0
    input.split('-').inject(list) {|new_list,e|
      new_list = new_list.select {|f| f.split(/_+/)[index] =~ /^#{Regexp.escape(e)}/ };
      index +=1; new_list
    }
  else
    default_search(input, list)
  end
end