module MerchCalendar::Util

Utility methods for the merch calendar

Public Instance Methods

end_of_month(year, month_param) click to toggle source

The end date of the month

@param year [Integer] the merch year @param month_param [Hash] month hash

@see start_of_month The start_of_month method for examples using month_param

@return [Date]

# File lib/merch_calendar/util.rb, line 60
def end_of_month(year, month_param)
  merch_month = get_merch_month_param(month_param)
  retail_calendar.end_of_month(year, merch_month)
end
end_of_quarter(year, quarter) click to toggle source

The end date of the quarter

@param year [Integer] the merch year @param quarter [Integer] the quarter

@return [Date] the ending date of the specified quarter

# File lib/merch_calendar/util.rb, line 100
def end_of_quarter(year, quarter)
  fiscal_year_calendar.end_of_quarter(year + 1, quarter)
end
end_of_week(year, month, week) click to toggle source

The end date of the week

@param year [Integer] the merch year @param month [Integer] an integer specifying the julian month. @param week [Integer] an integer specifying the merch week.

@return [Date] the ending date of the specified week

# File lib/merch_calendar/util.rb, line 27
def end_of_week(year, month, week)
  retail_calendar.end_of_week(year, julian_to_merch(month), week)
end
end_of_year(year) click to toggle source

The end date of the year

@param year [Integer] the merch year

@return [Date] the ending date of the specified year

# File lib/merch_calendar/util.rb, line 79
def end_of_year(year)
  retail_calendar.end_of_year(year)
end
julian_to_merch(julian_month) click to toggle source

Converts a julian month to a merch month

@param julian_month [Integer] the julian month to convert @return [Integer] the merch month

# File lib/merch_calendar/util.rb, line 143
def julian_to_merch(julian_month)
  if julian_month == 1
    12
  else
    julian_month - 1
  end
end
merch_months_in(start_date, end_date) click to toggle source

An array of merch dates in start_date to end_date

@param start_date [Date] the start date @param end_date [Date] the end date

@return [Array<Date>] array of merch months

# File lib/merch_calendar/util.rb, line 121
def merch_months_in(start_date, end_date)
  retail_calendar.merch_months_in(start_date, end_date)
end
merch_to_julian(merch_month) click to toggle source

Converts a merch month to the correct julian month

@param merch_month [Integer] the merch month to convert @return [Integer] the julian month

# File lib/merch_calendar/util.rb, line 130
def merch_to_julian(merch_month)
  if merch_month == 12
    1
  else
    merch_month + 1
  end
end
start_of_month(year, month_param) click to toggle source

The start date of the month

@example

# The following are all equivalent, and refer to May 2015
MerchCalendar.start_of_month(2015, 5)
MerchCalendar.start_of_month(2015, month: 5)
MerchCalendar.start_of_month(2015, julian_month: 5)
MerchCalendar.start_of_month(2015, merch_month: 4)

@param year [Integer] the merch year @param month_param [Integer,Hash] an integer specifying the julian month. This can also be a named hash @option month_param [Integer] :month the julian month @option month_param [Integer] :julian_month the julian month @option month_param [Integer] :merch_month the MERCH month

@return [Date] the starting date of the specified month

# File lib/merch_calendar/util.rb, line 47
def start_of_month(year, month_param)
  merch_month = get_merch_month_param(month_param)
  retail_calendar.start_of_month(year, merch_month)
end
start_of_quarter(year, quarter) click to toggle source

The start date of the quarter

@param year [Integer] the merch year @param quarter [Integer] the quarter

@return [Date] the starting date of the specified quarter

# File lib/merch_calendar/util.rb, line 90
def start_of_quarter(year, quarter)
  fiscal_year_calendar.start_of_quarter(year + 1, quarter)
end
start_of_week(year, month, week) click to toggle source

The start date of the week

@param year [Integer] the merch year @param month [Integer] an integer specifying the julian month. @param week [Integer] an integer specifying the merch week.

@return [Date] the starting date of the specified week

# File lib/merch_calendar/util.rb, line 16
def start_of_week(year, month, week)
  retail_calendar.start_of_week(year, julian_to_merch(month), week)
end
start_of_year(year) click to toggle source

The start date of the year

@param year [Integer] the merch year

@return [Date] the starting date of the specified year

# File lib/merch_calendar/util.rb, line 70
def start_of_year(year)
  retail_calendar.start_of_year(year)
end
weeks_for_month(year, month_param) click to toggle source

An array of merch weeks in a given month

@param year [Integer] the merch year @param month_param [Hash] month hash

@see start_of_month The start_of_month method for examples using month_param

@return [Array<MerchWeek>]

# File lib/merch_calendar/util.rb, line 160
def weeks_for_month(year, month_param)
  merch_month = get_merch_month_param(month_param)

  start_date = retail_calendar.start_of_month(year, merch_month)

  weeks = (retail_calendar.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 })
  end
end
weeks_in_year(year) click to toggle source

Returns the number of weeks in a given merch year

@param year [Integer] the merch year

@return [Integer] number of weeks

# File lib/merch_calendar/util.rb, line 110
def weeks_in_year(year)
  retail_calendar.weeks_in_year(year)
end

Private Instance Methods

fiscal_year_calendar() click to toggle source
# File lib/merch_calendar/util.rb, line 181
def fiscal_year_calendar
  @fiscal_year_calendar ||= StitchFixFiscalYearCalendar.new
end
get_merch_month_param(param) click to toggle source

Reads the provided parameter and converts the value to a MERCH MONTH

# File lib/merch_calendar/util.rb, line 187
def get_merch_month_param(param)
  if param.is_a?(Integer)
    return julian_to_merch(param)
  elsif param.is_a? Hash
    julian_month = param.delete(:julian_month) || param.delete(:month)
    merch_month = param.delete(:merch_month)

    if merch_month
      return merch_month
    elsif julian_month
      return julian_to_merch(julian_month)
    end
  end

  raise ArgumentError
end
retail_calendar() click to toggle source
# File lib/merch_calendar/util.rb, line 177
def retail_calendar
  @retail_calendar ||= RetailCalendar.new
end