Created
November 24, 2014 15:36
-
-
Save clemens/2b95802e38c692b4c3e7 to your computer and use it in GitHub Desktop.
A simple class wrapping a calendar week
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 CalendarWeek | |
attr_reader :week, :year, :date | |
private :date | |
def self.current | |
for_date(Date.today) | |
end | |
def self.for_date(date) | |
new(date.year, date.cweek) | |
end | |
def initialize(year, week) | |
@year, @week = year, week | |
@date = Date.commercial(year, week) | |
end | |
def start | |
@date.beginning_of_week | |
end | |
def end | |
@date.end_of_week | |
end | |
def next | |
date_next_week = (date + 1.week).end_of_week | |
self.class.new(date_next_week.year, date_next_week.cweek) | |
end | |
def previous | |
date_previous_week = (date - 1.week).end_of_week | |
self.class.new(date_previous_week.year, date_previous_week.cweek) | |
end | |
def to_param | |
"#{year}-#{week}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment