Skip to content

Commit

Permalink
Renames DateCalculator to RetailCalendar. Makes interface more consis…
Browse files Browse the repository at this point in the history
…tent.

Methods on DateCalculator were treating months differently, giving inconsistent
results.  Renamed this class to RetailCalendar to show that it really represents
the NRF 4-5-4 retail calendar.
  • Loading branch information
Jon Worek committed Jun 29, 2017
1 parent 525f082 commit e4970bf
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 45 deletions.
2 changes: 1 addition & 1 deletion lib/merch_calendar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ module MerchCalendar
require_relative 'merch_calendar/configurable'
require_relative 'merch_calendar/configuration'
require_relative 'merch_calendar/merch_week'
require_relative 'merch_calendar/date_calculator'
require_relative 'merch_calendar/retail_calendar'
18 changes: 9 additions & 9 deletions lib/merch_calendar/merch_week.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ 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)
retail_calendar.end_of_month(start_of_year.year, num) >= date && date >= retail_calendar.start_of_month(start_of_year.year, num)
end
end

Expand All @@ -103,7 +103,7 @@ def year
#
# @return [Fixnum]
def month
@month ||= date_calc.merch_to_julian(merch_month)
@month ||= retail_calendar.merch_to_julian(merch_month)
end

# The specific quarter this week falls in
Expand Down Expand Up @@ -155,21 +155,21 @@ def start_of_year
#
# @return [Date]
def end_of_year
@end_of_year ||= date_calc.end_of_year(year)
@end_of_year ||= retail_calendar.end_of_year(year)
end

# The start date of the merch month
#
# @return [Date]
def start_of_month
@start_of_month ||= date_calc.start_of_month(year, merch_month)
@start_of_month ||= retail_calendar.start_of_month(year, merch_month)
end

# The end date of the merch month
#
# @return [Date]
def end_of_month
@end_of_month ||= date_calc.end_of_month(year, merch_month)
@end_of_month ||= retail_calendar.end_of_month(year, merch_month)
end

# The merch season this date falls under.
Expand Down Expand Up @@ -209,15 +209,15 @@ def to_s(format = :short)
private

def year_start_date
start_date = date_calc.start_of_year(date.year)
start_date = retail_calendar.start_of_year(date.year)
if start_date > date
start_date = date_calc.start_of_year(date.year - 1)
start_date = retail_calendar.start_of_year(date.year - 1)
end
start_date
end

def date_calc
@date_calc ||= DateCalculator.new
def retail_calendar
@retail_calendar ||= RetailCalendar.new
end

end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
require "date"

module MerchCalendar
class DateCalculator
class RetailCalendar
def initialze(first_month_of_year = 2)
@first_month_of_year = first_month_of_year
end

def end_of_year(year)
year_end = Date.new((year + 1), 1, -1)
Expand Down Expand Up @@ -51,14 +54,12 @@ def end_of_month(year, merch_month)

# Returns the date that corresponds to the first day in the merch week
def start_of_week(year, month, merch_week)
week = MerchCalendar::MerchWeek.find(year, month, merch_week)
week.start_of_week
start_of_month(year, month) + ((merch_week - 1) * 7)
end

# Returns the date that corresponds to the last day in the merch week
def end_of_week(year, month, merch_week)
week = MerchCalendar::MerchWeek.find(year, month, merch_week)
week.end_of_week
start_of_month(year, month) + (6 + ((merch_week - 1) * 7))
end

# Return the starting date for a particular quarter
Expand Down
36 changes: 18 additions & 18 deletions lib/merch_calendar/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Util
#
# @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, month, week)
end

# The end date of the week
Expand All @@ -22,7 +22,7 @@ def start_of_week(year, month, 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, month, week)
end

# The start date of the month
Expand All @@ -43,7 +43,7 @@ def end_of_week(year, month, week)
# @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
Expand All @@ -56,7 +56,7 @@ def start_of_month(year, 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
Expand All @@ -65,7 +65,7 @@ def end_of_month(year, month_param)
#
# @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
Expand All @@ -74,7 +74,7 @@ def start_of_year(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


Expand All @@ -85,7 +85,7 @@ def end_of_year(year)
#
# @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
Expand All @@ -95,7 +95,7 @@ def start_of_quarter(year, 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


Expand All @@ -105,7 +105,7 @@ def end_of_quarter(year, quarter)
#
# @return [Fixnum] number of weeks
def weeks_in_year(year)
date_calc.weeks_in_year(year)
retail_calendar.weeks_in_year(year)
end


Expand All @@ -116,7 +116,7 @@ def weeks_in_year(year)
#
# @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


Expand All @@ -125,15 +125,15 @@ def merch_months_in(start_date, end_date)
# @param month [Fixnum] the merch month to convert
# @return [Fixnum] the julian month
def merch_to_julian(month)
date_calc.merch_to_julian(month)
retail_calendar.merch_to_julian(month)
end

# Converts a julian month to a merch month
#
# @param month [Fixnum] the julian month to convert
# @return [Fixnum] the merch month
def julian_to_merch(month)
date_calc.julian_to_merch(month)
retail_calendar.julian_to_merch(month)
end

# An array of merch weeks in a given month
Expand All @@ -147,9 +147,9 @@ def julian_to_merch(month)
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)
Expand All @@ -161,22 +161,22 @@ def weeks_for_month(year, month_param)

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 retail_calendar.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 retail_calendar.julian_to_merch(julian_month)
end
end
raise ArgumentError
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

RSpec.describe MerchCalendar::DateCalculator do
RSpec.describe MerchCalendar::RetailCalendar do
describe "#julian_to_merch" do
it { expect(subject.julian_to_merch(1)).to eq 12 }
it { expect(subject.julian_to_merch(2)).to eq 1 }
Expand Down Expand Up @@ -41,14 +41,32 @@
end
end

it "#start_of_quarter" do
expect(subject.start_of_quarter(2017, 1)).to eq Date.new(2017, 1, 29)
expect(subject.start_of_quarter(2018, 1)).to eq Date.new(2018, 2, 4)
describe "#start_of_week" do
it "returns the correct date" do
expect(subject.start_of_week(2017, 1, 1)).to eq Date.new(2017, 1, 29)
expect(subject.start_of_week(2018, 1, 1)).to eq Date.new(2018, 2, 4)
end
end

describe "#end_of_week" do
it "returns the correct date" do
expect(subject.end_of_week(2017, 1, 1)).to eq Date.new(2017, 2, 4)
expect(subject.end_of_week(2018, 1, 1)).to eq Date.new(2018, 2, 10)
end
end

it "#end_of_quarter" do
expect(subject.end_of_quarter(2017, 1)).to eq Date.new(2017, 4, 29)
expect(subject.end_of_quarter(2018, 1)).to eq Date.new(2018, 5, 5)
describe "#start_of_quarter" do
it "returns the correct date" do
expect(subject.start_of_quarter(2017, 1)).to eq Date.new(2017, 1, 29)
expect(subject.start_of_quarter(2018, 1)).to eq Date.new(2018, 2, 4)
end
end

describe "#end_of_quarter" do
it "returns the correct date" do
expect(subject.end_of_quarter(2017, 1)).to eq Date.new(2017, 4, 29)
expect(subject.end_of_quarter(2018, 1)).to eq Date.new(2018, 5, 5)
end
end

describe "#merch_months_in" do
Expand Down
10 changes: 5 additions & 5 deletions spec/merch_calendar/util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@
end

describe '#get_merch_month_param' do
let(:date_calc) { MerchCalendar::DateCalculator.new }
let(:retail_calendar) { MerchCalendar::RetailCalendar.new }
before(:each) do
allow(MerchCalendar).to receive(:date_calc).and_return(date_calc)
allow(MerchCalendar).to receive(:retail_calendar).and_return(retail_calendar)
end

context "valid calls" do
before(:each) do
expect(date_calc).to receive(:start_of_month).with(2014, 1)
expect(retail_calendar).to receive(:start_of_month).with(2014, 1)
end
it "assumes an integer is a julian month" do
MerchCalendar.start_of_month(2014, 2)
Expand All @@ -97,9 +97,9 @@

context "errors" do
before(:each) do
expect(date_calc).to_not receive(:start_of_month)
expect(retail_calendar).to_not receive(:start_of_month)
end
it { expect { MerchCalendar.start_of_month(2014, :april) }.to raise_error(ArgumentError) }
end
end
end
end

0 comments on commit e4970bf

Please sign in to comment.