lib/merch_calendar/util.rb in merch_calendar-0.0.5 vs lib/merch_calendar/util.rb in merch_calendar-0.1.0.rc1
- old
+ new
@@ -5,26 +5,26 @@
# The start date of the week
#
# @param year [Fixnum] the merch year
# @param month [Fixnum] an integer specifying the julian month.
- # @param month [Fixnum] an integer specifying the merch week.
+ # @param week [Fixnum] an integer specifying the merch week.
#
# @return [Date] the starting date of the specified week
def start_of_week(year, month, week)
- date_calc.start_of_week(year, month, week)
+ retail_calendar.start_of_week(year, julian_to_merch(month), week)
end
# The end date of the week
#
# @param year [Fixnum] the merch year
# @param month [Fixnum] an integer specifying the julian month.
- # @param month [Fixnum] an integer specifying the merch week.
+ # @param week [Fixnum] an integer specifying the merch week.
#
# @return [Date] the ending date of the specified week
def end_of_week(year, month, week)
- date_calc.end_of_week(year, month, week)
+ retail_calendar.end_of_week(year, julian_to_merch(month), week)
end
# The start date of the month
#
# @example
@@ -41,11 +41,11 @@
# @option month_param [Fixnum] :merch_month the MERCH month
#
# @return [Date] the starting date of the specified month
def start_of_month(year, month_param)
merch_month = get_merch_month_param(month_param)
- date_calc.start_of_month(year, merch_month)
+ retail_calendar.start_of_month(year, merch_month)
end
# The end date of the month
#
# @param year [Fixnum] the merch year
@@ -54,90 +54,100 @@
# @see #start_of_month The start_of_month method for examples using month_param
#
# @return [Date]
def end_of_month(year, month_param)
merch_month = get_merch_month_param(month_param)
- date_calc.end_of_month(year, merch_month)
+ retail_calendar.end_of_month(year, merch_month)
end
# The start date of the year
#
# @param year [Fixnum] the merch year
#
# @return [Date] the starting date of the specified year
def start_of_year(year)
- date_calc.start_of_year(year)
+ retail_calendar.start_of_year(year)
end
# The end date of the year
#
# @param year [Fixnum] the merch year
#
# @return [Date] the ending date of the specified year
def end_of_year(year)
- date_calc.end_of_year(year)
+ retail_calendar.end_of_year(year)
end
# The start date of the quarter
#
# @param year [Fixnum] the merch year
# @param quarter [Fixnum] the quarter
#
# @return [Date] the starting date of the specified quarter
def start_of_quarter(year, quarter)
- date_calc.start_of_quarter(year, quarter)
+ retail_calendar.start_of_quarter(year, quarter)
end
# The end date of the quarter
#
# @param year [Fixnum] the merch year
# @param quarter [Fixnum] the quarter
#
# @return [Date] the ending date of the specified quarter
def end_of_quarter(year, quarter)
- date_calc.end_of_quarter(year, quarter)
+ retail_calendar.end_of_quarter(year, quarter)
end
# Returns the number of weeks in a given merch year
#
# @param year [Fixnum] the merch year
#
# @return [Fixnum] number of weeks
def weeks_in_year(year)
- date_calc.weeks_in_year(year)
+ retail_calendar.weeks_in_year(year)
end
# 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
def merch_months_in(start_date, end_date)
- date_calc.merch_months_in(start_date, end_date)
+ retail_calendar.merch_months_in(start_date, end_date)
end
# Converts a merch month to the correct julian month
#
- # @param month [Fixnum] the merch month to convert
+ # @param merch_month [Fixnum] the merch month to convert
# @return [Fixnum] the julian month
- def merch_to_julian(month)
- date_calc.merch_to_julian(month)
+ def merch_to_julian(merch_month)
+ if merch_month == 12
+ 1
+ else
+ merch_month + 1
+ end
end
+
# Converts a julian month to a merch month
#
- # @param month [Fixnum] the julian month to convert
+ # @param julian_month [Fixnum] the julian month to convert
# @return [Fixnum] the merch month
- def julian_to_merch(month)
- date_calc.julian_to_merch(month)
+ def julian_to_merch(julian_month)
+ if julian_month == 1
+ 12
+ else
+ julian_month - 1
+ end
end
+
# An array of merch weeks in a given month
#
# @param year [Fixnum] the merch year
# @param month_param [Hash] month hash
#
@@ -145,13 +155,13 @@
#
# @return [Array<MerchWeek>]
def weeks_for_month(year, month_param)
merch_month = get_merch_month_param(month_param)
- start_date = date_calc.start_of_month(year, merch_month)
+ start_date = retail_calendar.start_of_month(year, merch_month)
- weeks = (date_calc.end_of_month(year, merch_month) - start_date + 1) / 7
+ 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 })
@@ -159,27 +169,29 @@
end
private
- def date_calc
- @date_calc ||= DateCalculator.new
+ def retail_calendar
+ @retail_calendar ||= RetailCalendar.new
end
# Reads the provided parameter and converts the value
# to a MERCH MONTH
def get_merch_month_param(param)
if param.is_a? Fixnum
- return date_calc.julian_to_merch(param)
+ 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 date_calc.julian_to_merch(julian_month)
+ return julian_to_merch(julian_month)
end
end
+
raise ArgumentError
end
end