Module: OpenGem::CommonOptions

Included in:
Gem::Commands::OpenCommand, Gem::Commands::ReadCommand
Defined in:
lib/open_gem/common_options.rb

Instance Method Summary collapse

Instance Method Details

#add_command_option(description = nil) ⇒ Object



3
4
5
6
7
8
# File 'lib/open_gem/common_options.rb', line 3

def add_command_option(description=nil)
  add_option('-c', '--command COMMAND',
             description || 'Execute command at path of the rubygem') do |value, options|
    options[:command] = value
  end
end

#add_exact_match_optionObject



17
18
19
20
21
22
# File 'lib/open_gem/common_options.rb', line 17

def add_exact_match_option
  add_option('-x', '--exact',
             'Only list exact matches') do |value, options|
    options[:exact] = true
  end
end

#add_latest_version_optionObject



10
11
12
13
14
15
# File 'lib/open_gem/common_options.rb', line 10

def add_latest_version_option
  add_option('-l', '--latest',
             'If there are multiple versions, open the latest') do |value, options|
    options[:latest] = true
  end
end

#get_spec(name) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/open_gem/common_options.rb', line 24

def get_spec(name)
  version = options[:version] || Gem::Version.new(">= 0")
  exact   = options[:exact]
  
  specs = Gem::Specification.find_all do |s|
    if name.is_a? String
      name_match = s.name == name
    else
      name_match = s.name[name]
    end
    
    name_match && version.satisfied_by?(s.version)
  end

  if block_given?
    specs = specs.select{|spec| yield spec}
  end
  
  specs = specs.sort
  
  if specs.length == 0
    # If we have not tried to do a pattern match yet, fall back on it.
    if(!exact && !name.is_a?(Regexp))
      pattern = /#{Regexp.escape name}/
      get_spec(pattern)
    else
      say "#{name.inspect} is not available"
      return nil
    end

  elsif specs.length == 1 || options[:latest]
    return specs.last

  else
    choices = specs.map{|s|"#{s.name} #{s.version}"}
    c,i = choose_from_list "Open which gem?", choices
    return specs[i] if i

  end
end