Skip to content

Commit

Permalink
Add merch_months_in with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lisa Sheridan committed Jul 2, 2015
1 parent df8e98f commit 14eaeaf
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/merch_calendar/date_calculator.rb
Expand Up @@ -98,6 +98,18 @@ def julian_to_merch(julian_month)
end
end


def merch_months_in(start_date, end_date)
merch_months = []
prev_date = start_date - 2
date = start_date
while date <= end_date do
date = MerchCalendar.start_of_month(date.year, merch_month: date.month)
next if prev_date == date
merch_months.push(date)
prev_date = date
date += 14
end
merch_months
end
end
end
end
10 changes: 10 additions & 0 deletions lib/merch_calendar/util.rb
Expand Up @@ -87,6 +87,16 @@ def 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)
end


# Converts a merch month to the correct julian month
#
Expand Down
38 changes: 38 additions & 0 deletions spec/merch_calendar/date_calculator_spec.rb
Expand Up @@ -48,5 +48,43 @@

it "#end_of_quarter"

describe "#merch_months_in" do
it "returns merch date for start_date if start_date is the same as end_date" do
start_date = Date.new(2014,8,1)
end_date = start_date
start_merch_date = MerchCalendar.start_of_month(start_date.year, merch_month: start_date.month)

merch_months = subject.merch_months_in(start_date, end_date)
expect(merch_months.count).to be(1)
expect(merch_months.first.year).to eq start_merch_date.year
expect(merch_months.first.month).to eq start_merch_date.month
expect(merch_months.first.day).to eq start_merch_date.day
end

it "returns valid merch dates for 2014" do
start_date = Date.new(2014, 1, 1)
end_date = Date.new(2014, 12, 1)

merch_months = subject.merch_months_in(start_date, end_date)
expect(merch_months.count).to be 11

merch_months.each do |merch_month|
expect(merch_month.year).to be 2014
end

expect(merch_months[0].strftime('%Y-%m-%d')).to eq '2014-02-02'
expect(merch_months[1].strftime('%Y-%m-%d')).to eq '2014-03-02'
expect(merch_months[2].strftime('%Y-%m-%d')).to eq '2014-04-06'
expect(merch_months[3].strftime('%Y-%m-%d')).to eq '2014-05-04'
expect(merch_months[4].strftime('%Y-%m-%d')).to eq '2014-06-01'
expect(merch_months[5].strftime('%Y-%m-%d')).to eq '2014-07-06'
expect(merch_months[6].strftime('%Y-%m-%d')).to eq '2014-08-03'
expect(merch_months[7].strftime('%Y-%m-%d')).to eq '2014-08-31'
expect(merch_months[8].strftime('%Y-%m-%d')).to eq '2014-10-05'
expect(merch_months[9].strftime('%Y-%m-%d')).to eq '2014-11-02'
expect(merch_months[10].strftime('%Y-%m-%d')).to eq '2014-11-30'
end
end

end

0 comments on commit 14eaeaf

Please sign in to comment.