Sha256: a3f6681dd41ef5afdd7bc9c4917d2b0f94567892de4ac256ea3aacb28b17a40d

Contents?: true

Size: 1.85 KB

Versions: 3

Compression:

Stored size: 1.85 KB

Contents

require 'date'

# calendar methods ###################################################
class HtmlSkeleton
  attr_reader :day_header

 protected
  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>"
    }
    <<-EOS
<thead><th colspan="2">#{@options[:title]}</th></thead>
#{body.join('')}
    EOS
  end

  def a_month(year, month)
    title = @options[:month_names][month]
    <<-EOS
<table class="month">
  <tr class="monthName"><th colspan="7">#{title}</th></tr>
  <tr class="dayName">#{@day_header}</tr>
  #{days_of_month(year, month)}
</table>
    EOS
  end

  def days_of_month(year, month)
    first_weekday = @options[:first_day_of_week]
    last_weekday  = first_weekday > 0 ? first_weekday - 1 : 6
    cell_proc = @options[:cell_proc]
    today = (Time.respond_to?(:zone) &&
	    !(zone = Time.zone).nil? ? 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

  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>"
#    "<td class=\"#{attrs}\">##</td>"
  end
  
  def weekend?(date)
    [0, 6].include?(date.wday)
  end

  def days_between(first, second)
    first > second ? second + (7 - first) : second - first
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
html_skeleton-0.4.4 lib/html_skeleton_calendar.rb
html_skeleton-0.4.3 lib/html_skeleton_calendar.rb
html_skeleton-0.4.1 lib/html_skeleton_calendar.rb