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 and calendar
Constant Summary collapse
- MONTHS =
%w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec).freeze
Instance Attribute Summary collapse
-
#calendar ⇒ Object
readonly
The Merch Calendar that is being represented, either Fiscal or Retail.
-
#date ⇒ Object
readonly
The Julian date that is being represented.
Class Method Summary collapse
-
.find(year, julian_month, week_number = nil, options = {}) ⇒ Object
Returns an array of merch weeks for a month, or a specific week.
-
.from_date(julian_date, options = {}) ⇒ MerchWeek
Locates the
MerchWeekfor a given Julian date. -
.today(options = {}) ⇒ 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 end date 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 Month 1 is Feb for the retail calendar Month 1 is August for the fiscal calendar.
-
#month ⇒ Fixnum
Returns julian month where the merch week falls.
-
#quarter ⇒ Fixnum
Returns the quarter that this merch week falls.
-
#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 start date of this week.
-
#start_of_year ⇒ Date
The start date 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 merch month will be between 1 and 5.
-
#year ⇒ Fixnum
Returns the Merch year depending whether it is from the Retail or Fiscal calendar.
-
#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
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/merch_calendar/merch_week.rb', line 70 def initialize(date, = {}) @date = date #defaults to Retail Calendar if no other calendar is defined @calendar = .fetch(:calendar, RetailCalendar.new) # 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
#calendar ⇒ Object (readonly)
The Merch Calendar that is being represented, either Fiscal or Retail
18 19 20 |
# File 'lib/merch_calendar/merch_week.rb', line 18 def calendar @calendar end |
#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, week_number = nil, options) ⇒ Array<MerchWeek> .find(year, julian_month, week_number) ⇒ MerchWeek
Returns an array of merch weeks for a month, or a specific week.
50 51 52 53 54 55 56 57 |
# File 'lib/merch_calendar/merch_week.rb', line 50 def self.find(year, julian_month, week_number=nil, ={}) calendar = .fetch(:calendar, RetailCalendar.new) if week_number.nil? calendar.weeks_for_month(year, julian_month) else calendar.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.
31 32 33 |
# File 'lib/merch_calendar/merch_week.rb', line 31 def self.from_date(julian_date, = {}) MerchWeek.new(Date.parse("#{julian_date}"), ) end |
.today(options = {}) ⇒ MerchWeek
Returns the MerchWeek for today’s date
@param [Hash] opts the to set your calendar, if none it will default to RetailCalendar
# @option opts [Class] :calendar The Calendar Class
64 65 66 |
# File 'lib/merch_calendar/merch_week.rb', line 64 def self.today(={}) MerchWeek.from_date(Date.today, ) end |
Instance Method Details
#end_of_month ⇒ Date
The end date of the merch month
176 177 178 |
# File 'lib/merch_calendar/merch_week.rb', line 176 def end_of_month @end_of_month ||= calendar.end_of_month(year, merch_month) end |
#end_of_week ⇒ Date
Returns the end date of this week
140 141 142 |
# File 'lib/merch_calendar/merch_week.rb', line 140 def end_of_week @end_of_week ||= (start_of_week + 6) end |
#end_of_year ⇒ Date
The end date of the corresponding merch year
162 163 164 |
# File 'lib/merch_calendar/merch_week.rb', line 162 def end_of_year @end_of_year ||= calendar.end_of_year(year) end |
#merch_month ⇒ Fixnum
This returns the “merch month” number for a date Month 1 is Feb for the retail calendar Month 1 is August for the fiscal calendar
100 101 102 103 104 105 106 107 |
# File 'lib/merch_calendar/merch_week.rb', line 100 def merch_month # TODO: This is very inefficient, but less complex than strategic guessing # maybe switch to a binary search or something merch_year = calendar.merch_year_from_date(date) @merch_month ||= (1..12).detect do |num| calendar.end_of_month(merch_year, num) >= date && date >= calendar.start_of_month(merch_year, num) end end |
#month ⇒ Fixnum
Returns julian month where the merch week falls
119 120 121 |
# File 'lib/merch_calendar/merch_week.rb', line 119 def month @month ||= calendar.merch_to_julian(merch_month) end |
#quarter ⇒ Fixnum
Returns the quarter that this merch week falls
126 127 128 |
# File 'lib/merch_calendar/merch_week.rb', line 126 def quarter @quarter ||= calendar.quarter(merch_month) end |
#season ⇒ String
The merch season this date falls under. Returns a string of Fall/Winter or Spring/Summer
184 185 186 |
# File 'lib/merch_calendar/merch_week.rb', line 184 def season @season ||= calendar.season(merch_month) end |
#start_of_month ⇒ Date
The start date of the merch month
169 170 171 |
# File 'lib/merch_calendar/merch_week.rb', line 169 def start_of_month @start_of_month ||= calendar.start_of_month(year, merch_month) end |
#start_of_week ⇒ Date
Returns the start date of this week
133 134 135 |
# File 'lib/merch_calendar/merch_week.rb', line 133 def start_of_week @start_of_week ||= (start_of_month + (7 * (week - 1))) end |
#start_of_year ⇒ Date
The start date of the corresponding merch year
155 156 157 |
# File 'lib/merch_calendar/merch_week.rb', line 155 def start_of_year @start_of_year ||= calendar.start_of_year(year) 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”
197 198 199 200 201 202 203 204 205 206 |
# File 'lib/merch_calendar/merch_week.rb', line 197 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 merch month will be between 1 and 5
148 149 150 |
# File 'lib/merch_calendar/merch_week.rb', line 148 def week @week ||= (((date-start_of_month)+1)/7.0).ceil end |
#year ⇒ Fixnum
Returns the Merch year depending whether it is from the Retail or Fiscal calendar
112 113 114 |
# File 'lib/merch_calendar/merch_week.rb', line 112 def year @year ||= calendar.merch_year_from_date(date) end |
#year_week ⇒ Fixnum
What week it is within the year from 1-53
91 92 93 |
# File 'lib/merch_calendar/merch_week.rb', line 91 def year_week @year_week ||= (((date-start_of_year)+1)/7.0).ceil end |