class MonthlyCalendar
Constants
- VERSION
Attributes
pdf[R]
Public Class Methods
new(options = {})
click to toggle source
# File lib/monthly_calendar.rb, line 12 def initialize(options = {}) @start_date = (options[:start_date] && Date.parse(options[:start_date])) || Date.today @pages_count = options[:pages] || 1 @pdf = Prawn::Document.new(page_layout: :landscape, top_margin: 1.in, skip_page_creation: true) create end
Public Instance Methods
save(file_name = "calendar.pdf")
click to toggle source
# File lib/monthly_calendar.rb, line 21 def save(file_name = "calendar.pdf") pdf.render_file File.expand_path(file_name) end
stream() { |to_s| ... }
click to toggle source
# File lib/monthly_calendar.rb, line 25 def stream yield(to_s) end
to_s()
click to toggle source
# File lib/monthly_calendar.rb, line 29 def to_s pdf.render end
Private Instance Methods
calendar_contents()
click to toggle source
# File lib/monthly_calendar.rb, line 64 def calendar_contents months = [] current_month = @start_date @pages_count.times do |page_index| month_text = current_month.strftime("%B %Y") days_in_month = Date.new(current_month.year, current_month.month, -1).mday first_day_of_week = Date.new(current_month.year, current_month.month, 1).wday weeks_count = ((days_in_month + first_day_of_week) / 7.0).ceil days = [] days << [] until days.count == weeks_count first_day_of_week.times do days[0] << nil end days_in_month.times do |day| week = (day + first_day_of_week) / 7 days[week] << day + 1 end days[4] << nil until days[4].count == 7 unless days[5].nil? days[5] << nil until days[5].count == 7 end months << [month_text, days] current_month = current_month.next_month end months end
create()
click to toggle source
# File lib/monthly_calendar.rb, line 35 def create calendar_contents.each do |(month, weeks)| pdf.start_new_page days_in_month = weeks.flatten.compact.max first_day_of_week = weeks[0].index(1) weeks_count = ((days_in_month + first_day_of_week) / 7.0).ceil pdf.define_grid(rows: weeks_count, columns: 7) draw_header(month) weeks.each_with_index do |week, week_index| draw_notes(week_index, find_nil_cells(week), weeks_count == 6 ? :close : :far) week.each_with_index do |day, day_index| draw_day_square(week_index, day_index, day) end end # The last thing drawn on each page isn't rendered for some reason so this # just "flushes the queue" (causes the last thing previous to be drawn) pdf.stroke end end
find_nil_cells(week)
click to toggle source
# File lib/monthly_calendar.rb, line 102 def find_nil_cells(week) { start: week.index(nil), count: week.count(nil) } end