Class: TabTab::Completions::Gem

Inherits:
Object
  • Object
show all
Defined in:
lib/tabtab/completions/gems.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gem_name, app_name, current_token, previous_token, global_config = {}) ⇒ Gem

Returns a new instance of Gem.



4
5
6
7
8
9
10
# File 'lib/tabtab/completions/gems.rb', line 4

def initialize(gem_name, app_name, current_token, previous_token, global_config = {})
  parse_gem_name_and_optional_explicit_file(gem_name)
  @app_name       = app_name
  @current_token  = current_token
  @previous_token = previous_token
  @global_config  = global_config
end

Instance Attribute Details

#app_nameObject (readonly)

Returns the value of attribute app_name.



2
3
4
# File 'lib/tabtab/completions/gems.rb', line 2

def app_name
  @app_name
end

#current_tokenObject (readonly)

Returns the value of attribute current_token.



2
3
4
# File 'lib/tabtab/completions/gems.rb', line 2

def current_token
  @current_token
end

#definitions_fileObject (readonly)

Returns the value of attribute definitions_file.



2
3
4
# File 'lib/tabtab/completions/gems.rb', line 2

def definitions_file
  @definitions_file
end

#gem_nameObject (readonly)

Returns the value of attribute gem_name.



2
3
4
# File 'lib/tabtab/completions/gems.rb', line 2

def gem_name
  @gem_name
end

#global_configObject (readonly)

Returns the value of attribute global_config.



2
3
4
# File 'lib/tabtab/completions/gems.rb', line 2

def global_config
  @global_config
end

#previous_tokenObject (readonly)

Returns the value of attribute previous_token.



2
3
4
# File 'lib/tabtab/completions/gems.rb', line 2

def previous_token
  @previous_token
end

Instance Method Details

#extractObject

Returns the sub-list of all options filtered by a common prefix e.g. if current extract list is [‘–help’, ‘–extra’, ‘-h’, ‘-x’] then starts_with(‘–’) returns [‘–help’, ‘–extra’]



15
16
17
18
19
20
21
22
23
# File 'lib/tabtab/completions/gems.rb', line 15

def extract
  require "tabtab/definitions"
  load definitions_file
  if TabTab::Definition[app_name]
    TabTab::Definition[app_name].extract_completions(previous_token, current_token, global_config)
  else
    []
  end
end

#gem_root_pathObject



39
40
41
42
43
# File 'lib/tabtab/completions/gems.rb', line 39

def gem_root_path
  require "rubygems"
  gem gem_name
  $LOAD_PATH.grep(/#{gem_name}.*\/lib$/).first.gsub(/\/lib$/, '')
end

#load_gem_and_return_definitions_fileObject



25
26
27
# File 'lib/tabtab/completions/gems.rb', line 25

def load_gem_and_return_definitions_file
  Dir[File.join(gem_root_path, '**', "tabtab_definitions.rb")].first
end

#parse_gem_name_and_optional_explicit_file(gem_name) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/tabtab/completions/gems.rb', line 29

def parse_gem_name_and_optional_explicit_file(gem_name)
  if gem_name =~ %r{^([\w\-_]*)/(.*)}
    @gem_name, file_path = $1, $2
    @definitions_file = File.join(gem_root_path, file_path)
  else
    @gem_name = gem_name
    @definitions_file = load_gem_and_return_definitions_file
  end
end