Class: Grep

Inherits:
IRB::Command::Base
  • Object
show all
Defined in:
lib/alet/irb/command/grep.rb

Instance Method Summary collapse

Instance Method Details

#execute(arg) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/alet/irb/command/grep.rb', line 8

def execute(arg)
  argv = arg.split(' ')
  opt = OptionParser.new
  opt.on '-l', '--label'
  opt.on '-n', '--api-name'

  params = {}
  opt.parse(argv, into: params)

  regxp = %r{#{argv.first}}

  sobjects = Alet.describe_global['sobjects'].select do |so|
    if params.has_key?(:label)
      regxp.match(so['label'])
    elsif params.has_key?(:"api-name")
      regxp.match?(so['name'])
    else
      regxp.match?(so['name']) || regxp.match(so['label'])
    end
  end

  table = TTY::Table.new(
            ["name", "label"],
            sobjects.map{ |so| [so['name'], so['label']] }
            )
  puts table.render :unicode
rescue SObjectModel::Rest::RequestError => e
  pastel = Pastel.new
  puts pastel.red(e.message)
end