Module: EventReporter::Print

Defined in:
lib/event_reporter/print.rb

Class Method Summary collapse

Class Method Details

.find_column_lengths(queue) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/event_reporter/print.rb', line 4

def find_column_lengths(queue)
  queue.inject(new_length_hash) do |column_length, attendee|
    HEADERS.each do |header|
      header = header.to_sym

      if attendee[header].length > column_length[header]
        column_length[header] = attendee[header].length
      end
    end

    column_length
  end
end

.new_length_hashObject



18
19
20
21
22
23
# File 'lib/event_reporter/print.rb', line 18

def new_length_hash
  HEADERS.inject({}) do |column_length, header|
    column_length[header.to_sym] = header.length
    column_length
  end
end


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/event_reporter/print.rb', line 43

def print_contents(columns_length, queue)
  queue.each_with_index do |attendee, i|
    i+=1
    HEADERS.each do |header|
      column_width = columns_length[header.to_sym]
      print "#{attendee[header].ljust(column_width)} "
    end
    print "\n"
    print_status(i, queue)
  end
end


35
36
37
38
39
40
41
# File 'lib/event_reporter/print.rb', line 35

def print_headers(columns_length)
  HEADERS.each do |header|
    column_width = columns_length[header.to_sym]
    print "#{header.gsub("_", " ").upcase.ljust(column_width)} "
  end
  print "\n"
end


55
56
57
58
59
60
# File 'lib/event_reporter/print.rb', line 55

def print_status(i, queue)
  if i % 10 == 0 && i != 0
    puts "Showing Matches #{i-9}-#{i} of #{queue.length}"
    AcceptAnyInput.read_char
  end
end

.queue_print(queue) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/event_reporter/print.rb', line 25

def queue_print(queue)
  if queue.empty?
    puts "Nothing in queue!"
  else
    columns_length = find_column_lengths(queue)
    print_headers(columns_length)
    print_contents(columns_length, queue)
  end
end

.queue_print_by(attribute, queue) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/event_reporter/print.rb', line 62

def queue_print_by(attribute, queue)
  if attribute.nil? || !queue.first.respond_to?(attribute)
    throw(:top, "Attribute not found to print by...")
  end

  queue.sort! do |attendee1, attendee2|
    attendee1[attribute] <=> attendee2[attribute]
  end

  queue_print(queue)
end