Class: RetailCalendar::Calendar
- Inherits:
-
Object
- Object
- RetailCalendar::Calendar
- Defined in:
- lib/retail_calendar/calendar.rb
Class Method Summary collapse
-
.julian_to_merch(julian_month) ⇒ Integer
Converts a julian month to a merch month.
-
.merch_to_julian(merch_month) ⇒ Integer
Converts a retail month to the correct julian month.
Instance Method Summary collapse
-
#end_of_month(year, month) ⇒ Object
Return the ending date for a particular month.
-
#end_of_year(year) ⇒ Object
Return the ending date for a particular year.
-
#initialize(type: 445, year_end_month: 1) ⇒ Calendar
constructor
A new instance of Calendar.
-
#retail_week_to_date(retail_week, year) ⇒ Object
accetps retail week (1..53) and returns the week’s date.
-
#start_of_month(year, month) ⇒ Object
Return starting date for a particular month param: month is the merch month of the year.
-
#start_of_year(year) ⇒ Object
Return starting of retail date for a particular year.
-
#start_of_year_by_date(date) ⇒ Object
Returns the start of the retail_year for the given date.
-
#weeks_in_year(year) ⇒ Object
Return the number of weeks in a particular year.
-
#year_month(date) ⇒ Object
what month it is within the retail year for a given date.
-
#year_week(date) ⇒ Integer
What week it is within the retail year from 1-53.
Constructor Details
#initialize(type: 445, year_end_month: 1) ⇒ Calendar
Returns a new instance of Calendar.
3 4 5 6 7 8 9 |
# File 'lib/retail_calendar/calendar.rb', line 3 def initialize(type: 445, year_end_month: 1) raise ArgumentError, "Bad calendar type #{type}" unless [544, 454, 445].include?(type) raise ArgumentError, "Bad year-end month #{year_end_month}" unless year_end_month.between?(1, 12) @type = type @yr_end_mo = year_end_month end |
Class Method Details
.julian_to_merch(julian_month) ⇒ Integer
Converts a julian month to a merch month
94 95 96 97 98 99 100 101 102 |
# File 'lib/retail_calendar/calendar.rb', line 94 def self.julian_to_merch(julian_month) raise ArgumentError if julian_month > 12 || julian_month <= 0 if julian_month == 1 12 else julian_month - 1 end end |
.merch_to_julian(merch_month) ⇒ Integer
Converts a retail month to the correct julian month
80 81 82 83 84 85 86 87 88 |
# File 'lib/retail_calendar/calendar.rb', line 80 def self.merch_to_julian(merch_month) raise ArgumentError if merch_month > 12 || merch_month <= 0 if merch_month == 12 1 else merch_month + 1 end end |
Instance Method Details
#end_of_month(year, month) ⇒ Object
Return the ending date for a particular month
48 49 50 51 52 |
# File 'lib/retail_calendar/calendar.rb', line 48 def end_of_month(year, month) if month == 12 then end_of_year year else start_of_month(year, month + 1) - 1 end end |
#end_of_year(year) ⇒ Object
Return the ending date for a particular year
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/retail_calendar/calendar.rb', line 12 def end_of_year(year) year += 1 unless @yr_end_mo == 12 year_end = Date.new year, @yr_end_mo, -1 wday = (year_end.wday + 1) % 7 # Saturday-origin day-of-week # Advance or retreat to closest Saturday if wday > 3 then year_end += 7 - wday elsif wday > 0 then year_end -= wday end year_end end |
#retail_week_to_date(retail_week, year) ⇒ Object
accetps retail week (1..53) and returns the week’s date
72 73 74 75 |
# File 'lib/retail_calendar/calendar.rb', line 72 def retail_week_to_date(retail_week, year) start_week = start_of_year(year) + (retail_week - 1).weeks { start_week: start_week, end_week: start_week + 6.days } end |
#start_of_month(year, month) ⇒ Object
Return starting date for a particular month param: month is the merch month of the year
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/retail_calendar/calendar.rb', line 36 def start_of_month(year, month) start = start_of_year(year) + ((month - 1) / 3).to_i * 91 case @type * 10 + (month - 1) % 3 when 4451, 4541 then start += 28 when 5441 then start += 35 when 4452 then start += 56 when 4542, 5442 then start += 63 end start end |
#start_of_year(year) ⇒ Object
Return starting of retail date for a particular year
24 25 26 |
# File 'lib/retail_calendar/calendar.rb', line 24 def start_of_year(year) end_of_year(year - 1) + 1 end |
#start_of_year_by_date(date) ⇒ Object
Returns the start of the retail_year for the given date
29 30 31 32 |
# File 'lib/retail_calendar/calendar.rb', line 29 def start_of_year_by_date(date) date -= 1.year if date < start_of_year(date.year) start_of_year(date.year) end |
#weeks_in_year(year) ⇒ Object
Return the number of weeks in a particular year
55 56 57 |
# File 'lib/retail_calendar/calendar.rb', line 55 def weeks_in_year(year) ((start_of_year(year + 1) - start_of_year(year)) / 7).to_i end |
#year_month(date) ⇒ Object
what month it is within the retail year for a given date
66 67 68 69 |
# File 'lib/retail_calendar/calendar.rb', line 66 def year_month(date) month = date.month RetailCalendar.julian_to_merch(month) end |
#year_week(date) ⇒ Integer
What week it is within the retail year from 1-53
61 62 63 |
# File 'lib/retail_calendar/calendar.rb', line 61 def year_week(date) (((date - start_of_year_by_date(date)) + 1) / 7.0).ceil end |