class HtmlSkeleton

calendar methods ###################################################

table methods ######################################################

Attributes

day_header[R]
options[R]

Public Instance Methods

calendar(options = {}, &block) click to toggle source
# File lib/html_skeleton.rb, line 10
def calendar(options = {}, &block)
  set_calendar_options(options, &block)
  month = @options[:month]
  frame = month ? 'div' : 'table'
  body  = month ? a_month(@options[:year], month) : a_year(@options[:year])
  %(<#{frame} class="#{@options[:calendar_class]}"> #{body} </#{frame}>)
end
table(rows, cols, options = {}, &block) click to toggle source
# File lib/html_skeleton.rb, line 18
  def table(rows, cols, options = {}, &block)
    set_table_options(options, &block)
    <<~TABLE
      <table class="#{@options[:table_class]}">
        #{table_header(cols)}
        #{table_body(rows, cols)}
      </table>
    TABLE
  end

Protected Instance Methods

a_day(date, today, block) click to toggle source
# File lib/html_skeleton_calendar.rb, line 56
def a_day(date, today, block)
  attrs = 'day'
  attrs += ' weekendDay'  if weekend?(date)
  attrs += ' today'       if date == today
  "<td class=\"#{attrs}\">#{block.call(date)}</td>"
end
a_month(year, month) click to toggle source
# File lib/html_skeleton_calendar.rb, line 25
  def a_month(year, month)
    title = @options[:month_names][month]
    <<~TABLE
      <table class="month">
        <tr class="monthName"><th colspan="7">#{title}</th></tr>
        <tr class="dayName">#{@day_header}</tr>
        #{days_of_month(year, month)}
      </table>
    TABLE
  end
a_year(year) click to toggle source
# File lib/html_skeleton_calendar.rb, line 10
  def a_year(year)
    rows = @options[:rows]
    cols = 12 / rows
    raise 'html_skeleton_calendar: invalid option <rows>' unless rows * cols == 12

    body = (0..rows - 1).collect { |y|
      str = (1..cols).collect { |x| "<td>#{a_month(year, y * cols + x)}</td>" }
      "<tr>#{str.join}</tr>"
    }
    <<~THEAD
      <thead><th colspan="2">#{@options[:title]}</th></thead>
      #{body.join}
    THEAD
  end
days_between(first, second) click to toggle source
# File lib/html_skeleton_calendar.rb, line 67
def days_between(first, second)
  first > second ? second + (7 - first) : second - first
end
days_of_month(year, month) click to toggle source
# File lib/html_skeleton_calendar.rb, line 36
def days_of_month(year, month)
  first_weekday = @options[:first_day_of_week]
  last_weekday  = first_weekday.positive? ? first_weekday - 1 : 6
  cell_proc = @options[:cell_proc]
  bool = Time.respond_to?(:zone) && !(zone = Time.zone).nil?
  today = bool ? zone.now.to_date : Date.today

  first = Date.civil(year, month, 1)
  last  = Date.civil(year, month, -1)

  cal = +'<tr>'
  cal << '<td></td>' * days_between(first_weekday, first.wday)
  first.upto(last) { |cur|
    cal << a_day(cur, today, cell_proc)
    cal << '</tr> <tr>' if cur.wday == last_weekday
  }
  cal << '<td></td>' * days_between((last + 1).wday, first_weekday + 7)
  cal << '</tr>'
end
set_calendar_options(options, &block) click to toggle source
# File lib/html_skeleton.rb, line 29
def set_calendar_options(options, &block)
  year = DateTime.now.year
  @options = {
    year: year,
    title: year,
    rows: 3,
    calendar_class: 'skeleton',
    month_names: Date::MONTHNAMES,
    abbrev: (0..1),
    cell_proc: block || ->(d) { d.day.to_s },
    first_day_of_week: 1
  }.merge options

  names = options[:day_names] || Date::DAYNAMES.dup
  @options[:first_day_of_week].times { names.push(names.shift) }

  @day_header = names.collect { |day|
    abbr = day[@options[:abbrev]]
    str = abbr == day ? day : %(<abbr title="#{day}">#{abbr}</abbr>)
    %(<th scope="col">#{str}</th>)
  }.join
end
set_table_options(options, &block) click to toggle source
# File lib/html_skeleton.rb, line 52
def set_table_options(options, &block)
  @options = {
    legend: nil,
    col_legend: ->(x) { x.to_s },
    row_legend: ->(x) { x.id },
    th_attribute: ->(_col) {},
    tr_attribute: ->(_row) {},

    table_class: 'skeleton',
    cell_proc: block || ->(row, col) { "<td>#{row} #{col}</td>" }
  }.merge options
end
table_body(rows, cols) click to toggle source
# File lib/html_skeleton_table.rb, line 20
def table_body(rows, cols)
  legend       = @options[:legend]
  row_legend   = @options[:row_legend]
  tr_attribute = @options[:tr_attribute]
  rows.collect { |row|
    rlegend = ''
    rlegend = %(<td class="legend">#{row_legend.call(row)}</td>) if legend
    cells = table_row(row, cols)
    %(<tr #{tr_attribute.call(row)}>#{rlegend}#{cells}</tr>)
  }.join("\n")
end
table_header(cols) click to toggle source
# File lib/html_skeleton_table.rb, line 8
def table_header(cols)
  legend       = @options[:legend]
  th_attribute = @options[:th_attribute]
  return '' unless legend

  proc = @options[:col_legend]
  col_header = cols.collect { |col|
    "<th #{th_attribute.call(col)}>#{proc.call(col)}</th>"
  }.join
  %(<thead><th class="legend">#{legend}</th>#{col_header}</thead>)
end
table_row(row, cols) click to toggle source
# File lib/html_skeleton_table.rb, line 32
def table_row(row, cols)
  cell_proc = @options[:cell_proc]
  cols.collect { |col|  cell_proc.call(row, col) }.join
end
weekend?(date) click to toggle source
# File lib/html_skeleton_calendar.rb, line 63
def weekend?(date)
  [0, 6].include?(date.wday)
end