Sha256: a4ee213dde7c615c44e78392085b645fb5030a2978bb6193d4f9d8aad0fda564

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

Contents

module OpenGem
  module CommonOptions
    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

    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
    
    def get_spec(name)
      dep = Gem::Dependency.new name, options[:version]
      specs = Gem.source_index.search(dep)
      if block_given?
        specs = specs.select{|spec| yield spec}
      end

      if specs.length == 0
        say "'#{name}' is not available"
        return nil

      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
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
adamsanderson-open_gem-1.3.1 lib/open_gem/common_options.rb
adamsanderson-open_gem-1.3.2 lib/open_gem/common_options.rb
open_gem-1.3.1 lib/open_gem/common_options.rb