Class: Tableau::TableBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/tableau/tablebuilder.rb

Instance Method Summary collapse

Constructor Details

#initialize(timetable, css_options = {}) ⇒ TableBuilder

Returns a new instance of TableBuilder.



15
16
17
18
# File 'lib/tableau/tablebuilder.rb', line 15

def initialize(timetable, css_options = {})
  @timetable = timetable
  @css = css_defaults.merge(css_options)
end

Instance Method Details

#build_cssObject



20
21
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
51
52
53
54
55
56
# File 'lib/tableau/tablebuilder.rb', line 20

def build_css
%Q{
body {
  background-color: #{@css[:body_color]};
}

table {
  border-collapse: collapse;
  border: 1px solid #{@css[:border_color]};
}

#time {
  background-color: #{@css[:header_bg_color]};
  color: #{@css[:header_fg_color]};
}

#time > th {
  font-weight: lighter;
  border-left: 1px solid #{@css[:border_color]};
}

.dh {
  background-color: #{@css[:header_bg_color]};
  color: #{@css[:header_fg_color]};
}

td {
  background-color: #{@css[:empty_color]};
  border: 1px solid #{@css[:border_color]};
  padding: 5px;
}

.class_item {
  background-color: #{@css[:class_color]};
}
}
end

#css_defaultsObject



4
5
6
7
8
9
10
11
12
13
# File 'lib/tableau/tablebuilder.rb', line 4

def css_defaults
  css_defaults = {
    body_color:      '#F4F5F6',
    border_color:    'rgba(0, 0, 0, 0.25)',
    class_color:     '#a9b1b9',
    header_bg_color: '#CFD3D7',
    header_fg_color: '#25292D',
    empty_color:     '#EBEDEE'
  }
end

#day_row(day, classes, end_time = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/tableau/tablebuilder.rb', line 58

def day_row(day, classes, end_time = nil)
  days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
  time = Time.new(2013, 1, 1, 9, 0 , 0)
  end_time ||= Time.new(2013, 1, 1, 21, 0, 0)

  day_row = %Q{<td class="dh">#{days[day]}</td>}

  if !classes || classes.count == 0
    while time < end_time
      day_row += "<td></td>"
      time += 900 # 15 mins in seconds
    end
  else
    while time < end_time
      class_at_time = @timetable.class_for_time(day, time)
      if class_at_time
        day_row += make_class(class_at_time)
        time += (900 * class_at_time.intervals)
      else
        day_row += "<td></td>"
        time += 900 # 15 mins in seconds
      end
    end
  end
  day_row
end

#make_class(klass) ⇒ Object

Create the HTML for a class item on the table



86
87
88
89
90
91
92
# File 'lib/tableau/tablebuilder.rb', line 86

def make_class(klass)
  %Q{
    <td class="class_item" colspan="#{klass.intervals}">
      <p>#{klass.name} - #{klass.location}</p>
      <p>#{klass.type}</p>
    </td>}
end

#to_htmlObject

HTML5 representation of the timetable



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/tableau/tablebuilder.rb', line 95

def to_html
  time_header, rows = '<th></th>', Array.new
  end_time = Time.new(2013, 1, 1, 21, 0, 0)

  # make the time row
  @time = Time.new(2013, 1, 1, 9, 0, 0)
  while @time < end_time
    time_header += "<th>#{@time.strftime("%-k:%M")}</th>"
    @time += 900
  end

  #make each day row
  (0..4).each do |day|
    classes = @timetable.classes_for_day(day)
    rows << day_row(day, classes)
  end

  rows_str, id_str = '', "id=\"#{@timetable.name}\""
  rows.each{ |r| rows_str += "<tr class=\"day\">\n#{r}\n</tr>\n" }

  %Q{
    <!DOCTYPE html>
    <html>
    <head>
      <title>#{@timetable.name || 'Timetable' } - Timetablr.co</title>
      <style>#{build_css}</style>
    </head>
    <body>
      <h3>#{@timetable.name}</h3>
      <table #{id_str if @timetable.name}>
        <tr id="time">#{time_header}</tr>
        #{rows_str}
      </table>
    </body>
    </html>
  }
end