Class: MerchCalendar::MerchWeek

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  @date = date

  # Placeholders. These should be populated by functions if nil
  # week_start: nil, week_end: nil, week_number: nil
  @start_of_year = options[:start_of_year]
  @end_of_year = options[:end_of_year]

  @start_of_week = options[:start_of_week]
  @end_of_week = options[:end_of_week]
  @week = options[:week]

  @start_of_month = options[:start_of_month]
  @end_of_month = options[:end_of_month]
end

Instance Attribute Details

#dateObject (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.

Overloads:

  • .find(year, julian_month) ⇒ Array<MerchWeek>

    Returns an array of MerchWeeks for a given month

    Parameters:

    • year (Fixnum)

      the merch year to locate

    • julian_month (Fixnum)

      the month to find merch months for

    Returns:

  • .find(year, julian_month, week_number) ⇒ MerchWeek

    Returns the specific merch week

    Parameters:

    • year (Fixnum)

      the merch year to locate

    • julian_month (Fixnum)

      the month to find merch months for

    • week_number (Fixnum)

      the specific week number.

    Returns:


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.

Overloads:

  • .from_date(String) ⇒ MerchWeek

    Parameters:

    • julian_date (String)

      a julian date in the format of YYYY-MM-DD

  • .from_date(Date) ⇒ MerchWeek

    Parameters:

    • julian_date (Date)

      a Date object

Returns:


24
25
26
# File 'lib/merch_calendar/merch_week.rb', line 24

def from_date(julian_date)
  MerchWeek.new Date.parse("#{julian_date}")
end

.todayMerchWeek

Returns the MerchWeek for today's date

Returns:


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_monthDate

The end date of the merch month

Returns:

  • (Date)

171
172
173
# File 'lib/merch_calendar/merch_week.rb', line 171

def end_of_month
  @end_of_month ||= date_calc.end_of_month(year, merch_month)
end

#end_of_weekDate

Returns the date of the end of this week

Returns:

  • (Date)

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_yearDate

The end date of the corresponding merch year

Returns:

  • (Date)

157
158
159
# File 'lib/merch_calendar/merch_week.rb', line 157

def end_of_year
  @end_of_year ||= date_calc.end_of_year(year)
end

#merch_monthFixnum

This returns the “merch month” number for a date Merch months are shifted by one. Month 1 is Feb

Returns:

  • (Fixnum)

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|
    date_calc.end_of_month(start_of_year.year, num) >= date && date >= date_calc.start_of_month(start_of_year.year, num)
  end
end

#monthFixnum

The julian month that this merch week falls in

Returns:

  • (Fixnum)

105
106
107
# File 'lib/merch_calendar/merch_week.rb', line 105

def month
  @month ||= date_calc.merch_to_julian(merch_month)
end

#quarterFixnum

The specific quarter this week falls in

Returns:

  • (Fixnum)

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

#seasonString

The merch season this date falls under. Returns a string of Fall/Winter or Spring/Summer

Returns:

  • (String)

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_monthDate

The start date of the merch month

Returns:

  • (Date)

164
165
166
# File 'lib/merch_calendar/merch_week.rb', line 164

def start_of_month
  @start_of_month ||= date_calc.start_of_month(year, merch_month)
end

#start_of_weekDate

Returns the date of the start of this week

Returns:

  • (Date)

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_yearDate

The date of the start of the corresponding merch year

Returns:

  • (Date)

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”

Parameters:

  • format (Symbol) (defaults to: :short)

    the format identifier to return. Default is :short

Returns:

  • (Date)

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

#weekFixnum

the number of the week within the given month will be between 1 and 5

Returns:

  • (Fixnum)

143
144
145
# File 'lib/merch_calendar/merch_week.rb', line 143

def week
  @week ||= (((date-start_of_month)+1)/7.0).ceil
end

#yearFixnum

The merch year

Returns:

  • (Fixnum)

98
99
100
# File 'lib/merch_calendar/merch_week.rb', line 98

def year
  start_of_year.year
end

#year_weekFixnum

What week it is within the year from 1-53

Returns:

  • (Fixnum)

    The week number of 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