Module: BugsBunny::Helper

Included in:
Queue, Rabbit
Defined in:
lib/bugs_bunny/helper.rb

Instance Method Summary collapse

Instance Method Details



4
5
6
7
8
9
10
# File 'lib/bugs_bunny/helper.rb', line 4

def print_queue(h, body)
  print ["-------\n", "QUEUE #{h.delivery_tag} (#{h.content_type}): ",
        ("Redelivered " if h.redelivered), "Mode #{h.delivery_mode}",
        ("\nConsumer: #{h.consumer_tag}" rescue nil),
         "\nExchange: #{h.exchange}",
        "\n\nBody:", read_dump(body), "\n"].reject(&:nil?).join
end


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
51
52
53
54
# File 'lib/bugs_bunny/helper.rb', line 23

def print_table(title, items, *fields)
  return if items.empty?
  puts title
  #find max length for each field; start with the field names themselves
  fields = items.first.class.column_names unless fields.any?
  max_len = Hash[*fields.map {|f| [f, f.to_s.length]}.flatten]
  items.each do |item|
    fields.each do |field|
      len = item.send(field).to_s.length
      max_len[field] = len if len > max_len[field]
    end
  end

  border = '+-' + fields.map {|f| '-' * max_len[f] }.join('-+-') + '-+'
  title_row = '| ' + fields.map do |f|
    sprintf("%-#{max_len[f]}s", f.to_s.split("_")[0].capitalize)
  end.join(' | ') + ' |'

  puts border
  puts title_row
  puts border

  items.each do |item|
    row = '| ' + fields.map do |f|
      sprintf("%-#{max_len[f]}s", item.send(f))
    end.join(' | ') + ' |'
    puts row
  end

  puts border
  puts "#{items.length} items\n"
end

#read_dump(dump) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/bugs_bunny/helper.rb', line 12

def read_dump(dump)
  case BugsBunny::Opt[:mode].to_sym
  when :marshal then Marshal.load(dump)
  when :json    then JSON.load(dump)
  else dump
  end
rescue
  dump
end