class TreerfulScanner::Printer

Constants

BEGIN_HOUR
END_HOUR
TITLE_WIDTH

Public Class Methods

new(time_tables = [], date = nil) click to toggle source
# File lib/treerful_scanner/printer.rb, line 10
def initialize(time_tables = [], date = nil)
  @time_tables = time_tables || []
  @date = date
end

Public Instance Methods

binary_form(time_table) click to toggle source
# File lib/treerful_scanner/printer.rb, line 25
def binary_form(time_table)
  result = String.new
  result << ' ' * ((END_HOUR - BEGIN_HOUR) * 2)
  offset = BEGIN_HOUR * 60
  time_table.durations.each do |duration|
    start = (duration.from - offset) / 30
    finish = (duration.to - offset) / 30
    result[start...finish] = 'O' * (finish - start)
  end
  result
end
header() click to toggle source
# File lib/treerful_scanner/printer.rb, line 15
def header
  format("%-#{TITLE_WIDTH}s", @date) << (BEGIN_HOUR..END_HOUR).map { |i| format('%-4s', i) }.join << "\n"
end
print() click to toggle source
row(time_table) click to toggle source
# File lib/treerful_scanner/printer.rb, line 19
def row(time_table)
  fixed_string(time_table.place.name, TITLE_WIDTH) +
    binary_form(time_table).each_char.map { |i| format('%-2s', i) }.join +
    "\n"
end

Private Instance Methods

fixed_string(string, width) click to toggle source
# File lib/treerful_scanner/printer.rb, line 48
def fixed_string(string, width)
  length = string.length + string.scan(/\p{Han}|\p{Katakana}|\p{Hiragana}\p{Hangul}/).length
  diff = width - length
  return string + (' ' * diff) if diff > 0
  string
end