Class: MerchCalendar::RetailCalendar
- Inherits:
-
Object
- Object
- MerchCalendar::RetailCalendar
- Defined in:
- lib/merch_calendar/retail_calendar.rb
Constant Summary collapse
- QUARTER_1 =
1- QUARTER_2 =
2- QUARTER_3 =
3- QUARTER_4 =
4- FOUR_WEEK_MONTHS =
[2, 5, 8, 11]
- FIVE_WEEK_MONTHS =
[3, 6, 9, 12]
Instance Method Summary collapse
-
#end_of_month(year, merch_month) ⇒ Date
The ending date of the given merch month.
-
#end_of_quarter(year, quarter) ⇒ Date
Return the ending date for a particular quarter.
-
#end_of_week(year, month, merch_week) ⇒ Date
Returns the date that corresponds to the last day in the merch week.
-
#end_of_year(year) ⇒ Date
The the first date of the retail year.
-
#julian_to_merch(julian_month) ⇒ Fixnum
Converts a julian month to a merch month.
-
#merch_months_in(start_date, end_date) ⇒ Array
Given beginning and end dates it will return an array of Retail Month’s Start date.
-
#merch_to_julian(merch_month) ⇒ Fixnum
Converts a merch month to the correct julian month.
-
#merch_year_from_date(date) ⇒ Fixnum
Given any julian date it will return what retail year it belongs to.
-
#quarter(merch_month) ⇒ Date
Returns the quarter that the merch month falls in.
-
#season(merch_month) ⇒ String
Returns the season given for the merch_month.
-
#start_of_month(year, merch_month) ⇒ Date
The starting date of the given merch month.
-
#start_of_quarter(year, quarter) ⇒ Date
Return the starting date for a particular quarter.
-
#start_of_week(year, month, merch_week) ⇒ Date
Returns the date that corresponds to the first day in the merch week.
-
#start_of_year(year) ⇒ Date
The last date of the retail year.
-
#weeks_for_month(year, month_param) ⇒ Array
Returns an array of Merch Weeks that pertains to the Julian Month of a Retail Year.
-
#weeks_in_year(year) ⇒ Fixnum
Returns the number of weeks in the retail year.
Instance Method Details
#end_of_month(year, merch_month) ⇒ Date
The ending date of the given merch month
65 66 67 68 69 70 71 |
# File 'lib/merch_calendar/retail_calendar.rb', line 65 def end_of_month(year, merch_month) if merch_month == 12 end_of_year(year) else start_of_month(year, merch_month + 1) - 1 end end |
#end_of_quarter(year, quarter) ⇒ Date
Return the ending date for a particular quarter
118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/merch_calendar/retail_calendar.rb', line 118 def end_of_quarter(year, quarter) case quarter when QUARTER_1 end_of_month(year, 3) when QUARTER_2 end_of_month(year, 6) when QUARTER_3 end_of_month(year, 9) when QUARTER_4 end_of_month(year, 12) else raise "invalid quarter" end end |
#end_of_week(year, month, merch_week) ⇒ Date
Returns the date that corresponds to the last day in the merch week
89 90 91 |
# File 'lib/merch_calendar/retail_calendar.rb', line 89 def end_of_week(year, month, merch_week) start_of_month(year, month) + (6 + ((merch_week - 1) * 7)) end |
#end_of_year(year) ⇒ Date
The the first date of the retail year
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/merch_calendar/retail_calendar.rb', line 18 def end_of_year(year) year_end = Date.new((year + 1), 1, -1) # Jan 31st wday = (year_end.wday + 1) % 7 if wday > 3 year_end += 7 - wday else year_end -= wday end year_end end |
#julian_to_merch(julian_month) ⇒ Fixnum
Converts a julian month to a merch month
210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/merch_calendar/retail_calendar.rb', line 210 def julian_to_merch(julian_month) if julian_month > 12 || julian_month <= 0 raise ArgumentError end if julian_month == 1 12 else julian_month - 1 end end |
#merch_months_in(start_date, end_date) ⇒ Array
Given beginning and end dates it will return an array of Retail Month’s Start date
227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/merch_calendar/retail_calendar.rb', line 227 def merch_months_in(start_date, end_date) merch_months = [] prev_date = start_date - 2 date = start_date while date <= end_date do date = MerchCalendar.start_of_month(date.year, merch_month: date.month) next if prev_date == date merch_months.push(date) prev_date = date date += 14 end merch_months end |
#merch_to_julian(merch_month) ⇒ Fixnum
Converts a merch month to the correct julian month
194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/merch_calendar/retail_calendar.rb', line 194 def merch_to_julian(merch_month) if merch_month > 12 || merch_month <= 0 raise ArgumentError end if merch_month == 12 1 else merch_month + 1 end end |
#merch_year_from_date(date) ⇒ Fixnum
Given any julian date it will return what retail year it belongs to
179 180 181 182 183 184 185 186 187 |
# File 'lib/merch_calendar/retail_calendar.rb', line 179 def merch_year_from_date(date) date_end_of_year = end_of_year(date.year) date_start_of_year = start_of_year(date.year) if date < date_start_of_year date.year - 1 else date.year end end |
#quarter(merch_month) ⇒ Date
Returns the quarter that the merch month falls in
137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/merch_calendar/retail_calendar.rb', line 137 def quarter(merch_month) case merch_month when 1,2,3 return QUARTER_1 when 4,5,6 return QUARTER_2 when 7,8,9 return QUARTER_3 when 10,11,12 return QUARTER_4 else raise "invalid merch month" end end |
#season(merch_month) ⇒ String
Returns the season given for the merch_month
156 157 158 159 160 161 162 163 164 165 |
# File 'lib/merch_calendar/retail_calendar.rb', line 156 def season(merch_month) case merch_month when 1,2,3,4,5,6 "Spring/Summer" when 7,8,9,10,11,12 "Fall/Winter" else raise "invalid merch month" end end |
#start_of_month(year, merch_month) ⇒ Date
The starting date of the given merch month
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/merch_calendar/retail_calendar.rb', line 43 def start_of_month(year, merch_month) # 91 = number of days in a single 4-5-4 set start = start_of_year(year) + ((merch_month - 1) / 3).to_i * 91 case merch_month when *FOUR_WEEK_MONTHS # 28 = 4 weeks start = start + 28 when *FIVE_WEEK_MONTHS # The 5 week months # 63 = 4 weeks + 5 weeks start = start + 63 end start end |
#start_of_quarter(year, quarter) ⇒ Date
Return the starting date for a particular quarter
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/merch_calendar/retail_calendar.rb', line 98 def start_of_quarter(year, quarter) case quarter when QUARTER_1 start_of_month(year, 1) when QUARTER_2 start_of_month(year, 4) when QUARTER_3 start_of_month(year, 7) when QUARTER_4 start_of_month(year, 10) else raise "invalid quarter" end end |
#start_of_week(year, month, merch_week) ⇒ Date
Returns the date that corresponds to the first day in the merch week
79 80 81 |
# File 'lib/merch_calendar/retail_calendar.rb', line 79 def start_of_week(year, month, merch_week) start_of_month(year, month) + ((merch_week - 1) * 7) end |
#start_of_year(year) ⇒ Date
The last date of the retail year
34 35 36 |
# File 'lib/merch_calendar/retail_calendar.rb', line 34 def start_of_year(year) end_of_year(year - 1) + 1 end |
#weeks_for_month(year, month_param) ⇒ Array
Returns an array of Merch Weeks that pertains to the Julian Month of a Retail Year
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/merch_calendar/retail_calendar.rb', line 246 def weeks_for_month(year, month_param) merch_month = get_merch_month_param(month_param) start_date = start_of_month(year, merch_month) weeks = (end_of_month(year, merch_month) - start_date + 1) / 7 (1..weeks).map do |week_num| week_start = start_date + ((week_num - 1) * 7) week_end = week_start + 6 MerchWeek.new(week_start, { start_of_week: week_start, end_of_week: week_end, week: week_num, calendar: RetailCalendar.new }) end end |
#weeks_in_year(year) ⇒ Fixnum
Returns the number of weeks in the retail year
171 172 173 |
# File 'lib/merch_calendar/retail_calendar.rb', line 171 def weeks_in_year(year) ((start_of_year(year + 1) - start_of_year(year)) / 7).to_i end |