Created
October 6, 2016 13:39
-
-
Save jwo/a25e6f1d8f91d048adf8e994561a9105 to your computer and use it in GitHub Desktop.
Create a calendar (array of weeks, each an array of days).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Calendar | |
attr_reader :date | |
def initialize(date) | |
@date = date | |
end | |
HEADER = %w(Sunday Monday Tuesday Wednesday Thursday Friday Saturday).freeze | |
START_DAY = :sunday | |
def weeks | |
first = date.beginning_of_month.beginning_of_week(START_DAY) | |
last = date.end_of_month.end_of_week(START_DAY) | |
(first..last).to_a.map do |date| | |
{ | |
classes: day_classes(date), | |
date_utc: date.to_time.to_i, | |
date_formatted: date.strftime("%b %d, %Y"), | |
day_of_week: HEADER[Date.today.wday] | |
} | |
end.in_groups_of(7) | |
end | |
private | |
def day_classes(day) | |
classes = [] | |
classes << 'today' if day == Date.today | |
classes << 'not-month' if day.month != date.month | |
classes.empty? ? nil : classes.join(' ') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment