Class: Gem::Commands::GrepCommand

Inherits:
QueryCommand
  • Object
show all
Defined in:
lib/rubygems/commands/grep_command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGrepCommand

Returns a new instance of GrepCommand.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rubygems/commands/grep_command.rb', line 8

def initialize
  super 'grep', "Enhances search command by providing extra search options and displaying results as a table"
  defaults.merge!(:columns=>[:name,:summary,:authors])

  add_option('-c', '--columns STRING', 'Gemspec columns/attributes to display per gem') do |value, options|
    options[:columns] = GemGrep.parse_input(value).map {|e| e.to_sym}
  end

  add_option('-f', '--fields STRING', 'Gemspec fields/attributes to search (only for local gems)') do |value, options|
    GemGrep.grep_fields = options[:fields] = GemGrep.parse_input(value)
  end
  remove_option '--name-matches'
  remove_option '-d'
  remove_option '--versions'
end

Instance Attribute Details

#resultsObject

Returns the value of attribute results.



7
8
9
# File 'lib/rubygems/commands/grep_command.rb', line 7

def results
  @results
end

#results_onlyObject

Returns the value of attribute results_only.



7
8
9
# File 'lib/rubygems/commands/grep_command.rb', line 7

def results_only
  @results_only
end

Instance Method Details

#argumentsObject

:nodoc:



24
25
26
# File 'lib/rubygems/commands/grep_command.rb', line 24

def arguments # :nodoc:
  "REGEXP          regular expression string to search specified gemspec attributes"
end

#cleanup_tuples(spec_tuples) ⇒ Object

borrowed from query command



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rubygems/commands/grep_command.rb', line 57

def cleanup_tuples(spec_tuples)
  versions = Hash.new { |h,name| h[name] = [] }

  spec_tuples.each do |spec_tuple, source_uri|
    versions[spec_tuple.first] << [spec_tuple, source_uri]
  end

  versions = versions.sort_by do |(name,_),_|
    name.downcase
  end
  tuples = options[:all] ? versions.inject([]) {|t,e| t += e[1]; t } : versions.map {|e| e[1].first }
  tuples.each {|e|
    if e.first.length != 4
     uri = URI.parse e.last
     spec = Gem::SpecFetcher.fetcher.fetch_spec e.first, uri
     e.first.push(spec)
    end
  }
  tuples
end

#defaults_strObject

:nodoc:



32
33
34
# File 'lib/rubygems/commands/grep_command.rb', line 32

def defaults_str # :nodoc:
  "--local --columns name,summary,authors --fields name --no-installed"
end

#descriptionObject

:nodoc:



36
37
38
39
40
41
# File 'lib/rubygems/commands/grep_command.rb', line 36

def description # :nodoc:
  'Enhances search command by providing options to search (--fields) and display (--columns) ' +
 'gemspec attributes. Results are displayed in an ascii table. Gemspec attributes can be specified '+
 'by the first unique string that it starts with i.e. "su" for "summary". To specify multiple gemspec attributes, delimit ' +
 "them with commas. Gemspec attributes available to options are: #{GemGrep.valid_gemspec_columns.join(', ')}."
end

#executeObject



43
44
45
46
47
48
# File 'lib/rubygems/commands/grep_command.rb', line 43

def execute
  string = get_one_optional_argument
  options[:name] = /#{string}/i
  Gem.source_index.extend Gem::SuperSearch
  super
end

#output_query_results(tuples) ⇒ Object



50
51
52
53
54
# File 'lib/rubygems/commands/grep_command.rb', line 50

def output_query_results(tuples)
  @results = cleanup_tuples(tuples).map {|e| e[0][-1]}
  @results_only ? @results :
    say(Hirb::Helpers::Table.render(@results, :fields=>options[:columns]))
end

#usageObject

:nodoc:



28
29
30
# File 'lib/rubygems/commands/grep_command.rb', line 28

def usage # :nodoc:
  "#{program_name} [REGEXP]"
end