Class: MMTop::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/mmtop/command.rb,
lib/mmtop/commands/kill.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.commandsObject

Returns the value of attribute commands.



39
40
41
# File 'lib/mmtop/command.rb', line 39

def commands
  @commands
end

Class Method Details

.kill_prompt(queries) ⇒ Object



22
23
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
# File 'lib/mmtop/commands/kill.rb', line 22

def self.kill_prompt(queries)
  if queries.empty?
    puts "No queries matched."
    return
  end

  puts "killing: "
  queries.each_with_index do |q, i|
    puts "#{i}: #{q.host.name}\t\t#{q.sql[0..80]}"
  end

  print "What shall I kill? (ex: 1-4,5,9|*)> "

  line = $stdin.readline
  if line == "\n"
    puts "nothing, ok."
  else
    indexes = parse_kill_selection(line)
    indexes && indexes.each { |i|
      if i == "*"
        queries.each(&:kill!)
      elsif queries[i]
        queries[i].kill!
      else
        puts "No such query: #{i}"
      end
    }
  end
end

.parse_kill_selection(str) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mmtop/commands/kill.rb', line 3

def self.parse_kill_selection(str)
  strs = str.split(/\s*,\s*/).map(&:strip)
  strs.map { |s|
    if s !~ /^[0-9\-\*]+$/
      $stdout.puts("Invalid query selection, try again")
      return nil
    else
      if s.include?("-")
        a = s.split("-").map(&:strip)
        ((a[0].to_i)..(a[1].to_i)).to_a
      elsif s == "*"
        s
      else
        s.to_i
      end
    end
  }.flatten
end

.register {|c| ... } ⇒ Object

Yields:

  • (c)


32
33
34
35
36
37
# File 'lib/mmtop/command.rb', line 32

def register
  @commands ||= []
  c = self.new
  yield c
  @commands << c
end

Instance Method Details

#command(&block) ⇒ Object



27
28
29
# File 'lib/mmtop/command.rb', line 27

def command(&block)
  @command = block.to_proc
end

#explain(e = nil) ⇒ Object



22
23
24
25
# File 'lib/mmtop/command.rb', line 22

def explain(e = nil)
  @explain = e if e
  @explain
end

#matches?(cmd) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/mmtop/command.rb', line 3

def matches?(cmd)
  @regexp.match(cmd)
end

#regexp(r = nil) ⇒ Object



12
13
14
15
# File 'lib/mmtop/command.rb', line 12

def regexp(r = nil)
  @regexp = r if r
  @regexp
end

#run(cmd, config) ⇒ Object



7
8
9
10
# File 'lib/mmtop/command.rb', line 7

def run(cmd, config)
  cmd =~ @regexp
  @command.call(cmd, config)
end

#usage(u = nil) ⇒ Object



17
18
19
20
# File 'lib/mmtop/command.rb', line 17

def usage(u = nil)
  @usage = u if u
  @usage
end