Class: FindGem::GemAutocomplete

Inherits:
Object
  • Object
show all
Defined in:
lib/find_gem/gem_autocomplete.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#comp_lineObject

Returns the value of attribute comp_line.



3
4
5
# File 'lib/find_gem/gem_autocomplete.rb', line 3

def comp_line
  @comp_line
end

Instance Method Details

#cmdline_gem_commandsObject



53
54
55
# File 'lib/find_gem/gem_autocomplete.rb', line 53

def cmdline_gem_commands
  `gem help commands | grep "^    \\w"`
end

#cmds_re(cmds) ⇒ Object



17
18
19
# File 'lib/find_gem/gem_autocomplete.rb', line 17

def cmds_re(cmds)
  /^(#{cmds.join('|')})\b/
end

#filter_by_after_match(list, after_match) ⇒ Object



28
29
30
# File 'lib/find_gem/gem_autocomplete.rb', line 28

def filter_by_after_match(list, after_match)
  list.grep(/^#{Regexp.escape after_match.strip}/)
end

#gem_cmdObject



13
14
15
# File 'lib/find_gem/gem_autocomplete.rb', line 13

def gem_cmd
  ['gem help', 'gem']
end

#gem_commands_list(after_match) ⇒ Object



46
47
48
49
50
51
# File 'lib/find_gem/gem_autocomplete.rb', line 46

def gem_commands_list(after_match)
  @commands ||= begin
    list = cmdline_gem_commands.split("\n").map { |line| line.split.first }
    filter_by_after_match(list, after_match)
  end
end

#gem_local_list(after_match) ⇒ Object



39
40
41
42
43
44
# File 'lib/find_gem/gem_autocomplete.rb', line 39

def gem_local_list(after_match)
  @local_list ||= begin
    list = `gem list | grep "^\\w" | sed -e "s/ .*//"`
    filter_by_after_match(list, after_match)
  end
end

#gem_remote_list(after_match) ⇒ Object



32
33
34
35
36
37
# File 'lib/find_gem/gem_autocomplete.rb', line 32

def gem_remote_list(after_match)
  @remote_list ||= begin
    list = `gem list --remote | grep "^\\w" | sed -e "s/ .*//"`
    filter_by_after_match(list, after_match)
  end
end

#list_clean(comp_line) ⇒ Object



21
22
23
24
25
26
# File 'lib/find_gem/gem_autocomplete.rb', line 21

def list_clean(comp_line)
  return gem_remote_list($')  if cmds_re(remote_cmds) =~ comp_line
  return gem_local_list($')   if cmds_re(local_cmds) =~ comp_line
  return gem_commands_list($')  if cmds_re(gem_cmd) =~ comp_line
  ""
end

#local_cmdsObject



9
10
11
# File 'lib/find_gem/gem_autocomplete.rb', line 9

def local_cmds
  @local_cmds ||= %w[find_gem edit_gem] + %w[contents dependency rdoc uninstall unpack update].map {|cmd| "gem #{cmd}"}
end

#match_gem_commands?Boolean

Returns:

  • (Boolean)


57
58
# File 'lib/find_gem/gem_autocomplete.rb', line 57

def match_gem_commands?
end

#remote_cmdsObject



5
6
7
# File 'lib/find_gem/gem_autocomplete.rb', line 5

def remote_cmds
  ['gem install']
end

#run(comp_line, _exit = true) ⇒ Object



60
61
62
63
# File 'lib/find_gem/gem_autocomplete.rb', line 60

def run(comp_line, _exit = true)
  puts self.list_clean(comp_line)
  exit 0 if _exit
end