Class: MerchCalendar::MerchWeek
- Inherits:
-
Object
- Object
- MerchCalendar::MerchWeek
- Defined in:
- lib/merch_calendar/merch_week.rb
Overview
Represents the Merch Week for a specified date.
Constant Summary collapse
- MONTHS =
%w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec).freeze
Instance Attribute Summary collapse
-
#date ⇒ Object
readonly
The Julian date that is being represented.
Class Method Summary collapse
-
.find(year, julian_month, week_number = nil) ⇒ Object
Returns an array of merch weeks for a month, or a specific week.
-
.from_date(julian_date) ⇒ MerchWeek
Locates the
MerchWeekfor a given Julian date. -
.today ⇒ MerchWeek
Returns the
MerchWeekfor today’s date.
Instance Method Summary collapse
-
#end_of_month ⇒ Date
The end date of the merch month.
-
#end_of_week ⇒ Date
Returns the date of the end of this week.
-
#end_of_year ⇒ Date
The end date of the corresponding merch year.
-
#initialize(date, options = {}) ⇒ MerchWeek
constructor
Pass in a date, make sure it is a valid date object.
-
#merch_month ⇒ Fixnum
This returns the “merch month” number for a date Merch months are shifted by one.
-
#month ⇒ Fixnum
The julian month that this merch week falls in.
-
#quarter ⇒ Fixnum
The specific quarter this week falls in.
-
#season ⇒ String
The merch season this date falls under.
-
#start_of_month ⇒ Date
The start date of the merch month.
-
#start_of_week ⇒ Date
Returns the date of the start of this week.
-
#start_of_year ⇒ Date
The date of the start of the corresponding merch year.
-
#to_s(format = :short) ⇒ Date
Outputs a text representation of this merch week.
-
#week ⇒ Fixnum
the number of the week within the given month will be between 1 and 5.
-
#year ⇒ Fixnum
The merch year.
-
#year_week ⇒ Fixnum
What week it is within the year from 1-53.
Constructor Details
#initialize(date, options = {}) ⇒ MerchWeek
Pass in a date, make sure it is a valid date object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/merch_calendar/merch_week.rb', line 60 def initialize(date, = {}) @date = date # Placeholders. These should be populated by functions if nil # week_start: nil, week_end: nil, week_number: nil @start_of_year = [:start_of_year] @end_of_year = [:end_of_year] @start_of_week = [:start_of_week] @end_of_week = [:end_of_week] @week = [:week] @start_of_month = [:start_of_month] @end_of_month = [:end_of_month] end |
Instance Attribute Details
#date ⇒ Object (readonly)
The Julian date that is being represented
12 13 14 |
# File 'lib/merch_calendar/merch_week.rb', line 12 def date @date end |
Class Method Details
.find(year, julian_month) ⇒ Array<MerchWeek> .find(year, julian_month, week_number) ⇒ MerchWeek
Returns an array of merch weeks for a month, or a specific week.
40 41 42 43 44 45 46 |
# File 'lib/merch_calendar/merch_week.rb', line 40 def find(year, julian_month, week_number=nil) if week_number.nil? MerchCalendar.weeks_for_month(year, julian_month) else MerchCalendar.weeks_for_month(year, julian_month)[week_number-1] end end |
.from_date(String) ⇒ MerchWeek .from_date(Date) ⇒ MerchWeek
Locates the MerchWeek for a given Julian date.
24 25 26 |
# File 'lib/merch_calendar/merch_week.rb', line 24 def from_date(julian_date) MerchWeek.new Date.parse("#{julian_date}") end |
.today ⇒ MerchWeek
Returns the MerchWeek for today’s date
51 52 53 |
# File 'lib/merch_calendar/merch_week.rb', line 51 def today MerchWeek.from_date Date.today end |
Instance Method Details
#end_of_month ⇒ Date
The end date of the merch month
171 172 173 |
# File 'lib/merch_calendar/merch_week.rb', line 171 def end_of_month @end_of_month ||= retail_calendar.end_of_month(year, merch_month) end |
#end_of_week ⇒ Date
Returns the date of the end of this week
135 136 137 |
# File 'lib/merch_calendar/merch_week.rb', line 135 def end_of_week @end_of_week ||= (start_of_week + 6) end |
#end_of_year ⇒ Date
The end date of the corresponding merch year
157 158 159 |
# File 'lib/merch_calendar/merch_week.rb', line 157 def end_of_year @end_of_year ||= retail_calendar.end_of_year(year) end |
#merch_month ⇒ Fixnum
This returns the “merch month” number for a date Merch months are shifted by one. Month 1 is Feb
87 88 89 90 91 92 93 |
# File 'lib/merch_calendar/merch_week.rb', line 87 def merch_month # TODO: This is very inefficient, but less complex than strategic guessing # maybe switch to a binary search or something @merch_month ||= (1..12).detect do |num| retail_calendar.end_of_month(start_of_year.year, num) >= date && date >= retail_calendar.start_of_month(start_of_year.year, num) end end |
#month ⇒ Fixnum
The julian month that this merch week falls in
105 106 107 |
# File 'lib/merch_calendar/merch_week.rb', line 105 def month @month ||= MerchCalendar.merch_to_julian(merch_month) end |
#quarter ⇒ Fixnum
The specific quarter this week falls in
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/merch_calendar/merch_week.rb', line 112 def quarter case merch_month when 7,8,9 return 1 when 10,11,12 return 2 when 1,2,3 return 3 else return 4 end end |
#season ⇒ String
The merch season this date falls under. Returns a string of Fall/Winter or Spring/Summer
179 180 181 182 183 184 185 186 |
# File 'lib/merch_calendar/merch_week.rb', line 179 def season case merch_month when 1,2,3,4,5,6 "Spring/Summer" when 7,8,9,10,11,12 "Fall/Winter" end end |
#start_of_month ⇒ Date
The start date of the merch month
164 165 166 |
# File 'lib/merch_calendar/merch_week.rb', line 164 def start_of_month @start_of_month ||= retail_calendar.start_of_month(year, merch_month) end |
#start_of_week ⇒ Date
Returns the date of the start of this week
128 129 130 |
# File 'lib/merch_calendar/merch_week.rb', line 128 def start_of_week @start_of_week ||= (start_of_month + (7 * (week - 1))) end |
#start_of_year ⇒ Date
The date of the start of the corresponding merch year
150 151 152 |
# File 'lib/merch_calendar/merch_week.rb', line 150 def start_of_year @start_of_year ||= year_start_date end |
#to_s(format = :short) ⇒ Date
Outputs a text representation of this merch week
Format keys:
-
:short(default) “Dec W5” -
:long“2012:48 Dec W5” -
:elasticsearch(default) “2012-12w05”
198 199 200 201 202 203 204 205 206 207 |
# File 'lib/merch_calendar/merch_week.rb', line 198 def to_s(format = :short) case format when :elasticsearch sprintf("%04d-%02dw%02d", year, month, week) when :long "#{year}:#{year_week} #{self.to_s(:short)}" else "#{MONTHS[month - 1]} W#{week}" end end |
#week ⇒ Fixnum
the number of the week within the given month will be between 1 and 5
143 144 145 |
# File 'lib/merch_calendar/merch_week.rb', line 143 def week @week ||= (((date-start_of_month)+1)/7.0).ceil end |
#year ⇒ Fixnum
The merch year
98 99 100 |
# File 'lib/merch_calendar/merch_week.rb', line 98 def year start_of_year.year end |
#year_week ⇒ Fixnum
What week it is within the year from 1-53
79 80 81 |
# File 'lib/merch_calendar/merch_week.rb', line 79 def year_week @year_week ||= (((date-start_of_year)+1)/7.0).ceil end |