class Carender::Calendar

Attributes

month[R]
year[R]

Public Class Methods

new(year, month) click to toggle source
# File lib/carender/calendar.rb, line 7
def initialize(year, month)
  @year = year
  @month = month
end

Public Instance Methods

render(view_context, collection, &block) click to toggle source
# File lib/carender/calendar.rb, line 12
    def render(view_context, collection, &block)
      html = <<-HTML
        <table>
          <tr>
            <th>Sun</th>
            <th>Mon</th>
            <th>Tue</th>
            <th>Wed</th>
            <th>Thu</th>
            <th>Fri</th>
            <th>Sat</th>
          </tr>
      HTML

      (date..date.end_of_month).each do |d|
        if d.wday == 0 || (d == date && d.wday != 0)
          html += '<tr>'
        end

        if d == date
          d.wday.times { html += '<td></td>' }
        end

        html += '<td>'

        if block
          html += view_context.capture { block.call(d, collection[d]) }.to_s
        end

        html += '</td>'

        if d == date.end_of_month
          (6 - d.wday).times { html += '<td></td>' }
        end

        if d.wday == 6 || (d == date.end_of_month && d.wday != 6)
          html += '</tr>'
        end
      end

      html += '</table>'
      html
    end

Private Instance Methods

date() click to toggle source
# File lib/carender/calendar.rb, line 60
def date
  @date ||= Date.new(year, month)
end