Skip to content

Instantly share code, notes, and snippets.

@clemens
Created November 24, 2014 15:36
Show Gist options
  • Save clemens/2b95802e38c692b4c3e7 to your computer and use it in GitHub Desktop.
Save clemens/2b95802e38c692b4c3e7 to your computer and use it in GitHub Desktop.
A simple class wrapping a calendar week
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