Module: ShiftyWeek
Overview
Normally a calendar displayed by ruby for October 2009 would be appear:
(Using cwday)
Mo Tu We Th Fr Sa Su
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
(Using wday)
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
With this gem you can set a week_day_start.
In this example to Wed.
- And the calendar will be displayed
-
We Th Fr Sa Su Mo Tu
1 2 3 4 5 6 7 8 9 10 11 12 13
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Defined Under Namespace
Classes: Constants
Constant Summary collapse
- WORKER_FORMAT =
"%Y-%m-%d %H:%M:%S"
Class Method Summary collapse
-
.included(in_class) ⇒ Object
Let’s generate some helpers.
Instance Method Summary collapse
-
#days_in_month ⇒ Object
Instance method for retreiving the number of days in this month.
-
#last_week_day ⇒ Object
Calculate the last week day for the month.
-
#month_name ⇒ Object
Convenience method.
-
#month_names ⇒ Object
Convenience method.
-
#step_to_month_end(step = 1) ⇒ Object
Hopefully the name explains what this method does.
-
#wday_offset ⇒ Object
Get the offset of this object’s date, to the first day of the week.
-
#week ⇒ Object
Get the week number for the current object.
-
#week_day_start ⇒ Object
Accessor for first week day of the calendar.
-
#week_days(options = {}, &block) ⇒ Object
Get the days of the week based on the current week_day_start.
-
#weeks_in_year ⇒ Object
Calculate the number of weeks in a year taking into account “shifted” weeks.
Class Method Details
.included(in_class) ⇒ Object
Let’s generate some helpers
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/shifty_week.rb', line 149 def included in_class in_class.class.send :define_method, :day_names, proc { ( (0..6).to_a + Date::DAYNAMES + Date::ABBR_DAYNAMES + Date::ABBR_DAYNAMES.collect {|wday| wday[0,2] } ) } in_class.class.send :define_method, :month_names, proc { ( (0..12).to_a + DateTime::MONTHNAMES + DateTime::ABBR_MONTHNAMES ) } end |
Instance Method Details
#days_in_month ⇒ Object
Instance method for retreiving the number of days in this month
58 59 60 |
# File 'lib/shifty_week.rb', line 58 def days_in_month Time.send("month_days", self.year, self.month) end |
#last_week_day ⇒ Object
Calculate the last week day for the month
53 54 55 |
# File 'lib/shifty_week.rb', line 53 def last_week_day DateTime.new(self.year, self.month, self.days_in_month).wday end |
#month_name ⇒ Object
Convenience method
73 74 75 |
# File 'lib/shifty_week.rb', line 73 def month_name DateTime::MONTHNAMES[self.month] end |
#month_names ⇒ Object
Convenience method
78 79 80 |
# File 'lib/shifty_week.rb', line 78 def month_names self.class.month_names end |
#step_to_month_end(step = 1) ⇒ Object
Hopefully the name explains what this method does
137 138 139 140 141 142 143 144 |
# File 'lib/shifty_week.rb', line 137 def step_to_month_end step = 1 days_in_month = self.days_in_month days_in_month += self.wday_offset if days_in_month%(7*4) >= 0 self.step(days_in_month, step) do |date| date.week_day_start = self.week_day_start yield date end end |
#wday_offset ⇒ Object
Get the offset of this object’s date, to the first day of the week.
68 69 70 |
# File 'lib/shifty_week.rb', line 68 def wday_offset() _week_day_numbers.index(self.wday) end |
#week ⇒ Object
Get the week number for the current object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/shifty_week.rb', line 83 def week working_date = DateTime.new(self.year, 1, 1) working_date.week_day_start = self.week_day_start working_date = (working_date-working_date.send("wday_offset")) week_num = 0 working_date.step(self) { |a_day| if a_day.wday == _week_day_numbers.first week_num += 1 end } week_num end |
#week_day_start ⇒ Object
Accessor for first week day of the calendar
104 105 106 |
# File 'lib/shifty_week.rb', line 104 def week_day_start @week_day_start || 0 end |
#week_days(options = {}, &block) ⇒ Object
Get the days of the week based on the current week_day_start
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/shifty_week.rb', line 109 def week_days(={}, &block) start_date = self result = [] (start_date-wday_offset).step 7 do |a_day| a_day.week_day_start = self.week_day_start if block_given? yield a_day else result.push(a_day) end end result end |
#weeks_in_year ⇒ Object
Calculate the number of weeks in a year taking into account “shifted” weeks
63 64 65 |
# File 'lib/shifty_week.rb', line 63 def weeks_in_year return (self-(self.yday-1)).wday == 6 && self.leap? ? 54 : 53 end |