Skip to content

Commit

Permalink
Fixes quarter calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Worek committed Jun 29, 2017
1 parent 51fb0a9 commit 525f082
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
20 changes: 9 additions & 11 deletions lib/merch_calendar/date_calculator.rb
@@ -1,12 +1,10 @@
require "date"

module MerchCalendar

# @api private
class DateCalculator

def end_of_year(year)
year_end = Date.new (year + 1), 1, -1
year_end = Date.new((year + 1), 1, -1)
wday = (year_end.wday + 1) % 7

if wday > 3
Expand Down Expand Up @@ -67,27 +65,27 @@ def end_of_week(year, month, merch_week)
def start_of_quarter(year, quarter)
case quarter
when 1
start_of_month(year, 7)
start_of_month(year, 1)
when 2
start_of_month(year, 10)
start_of_month(year, 4)
when 3
start_of_month(year, 1)
start_of_month(year, 7)
when 4
start_of_month(year, 4)
start_of_month(year, 10)
end
end

# Return the ending date for a particular quarter
def end_of_quarter(year, quarter)
case quarter
when 1
end_of_month(year, 9)
end_of_month(year, 3)
when 2
end_of_month(year, 12)
end_of_month(year, 6)
when 3
end_of_month(year, 3)
end_of_month(year, 9)
when 4
end_of_month(year, 6)
end_of_month(year, 12)
end
end

Expand Down
15 changes: 9 additions & 6 deletions spec/merch_calendar/date_calculator_spec.rb
@@ -1,9 +1,6 @@
require 'spec_helper'

describe MerchCalendar::DateCalculator do

subject { described_class.new }

RSpec.describe MerchCalendar::DateCalculator 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 @@ -44,9 +41,15 @@
end
end

it "#start_of_quarter"
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)
end

it "#end_of_quarter"
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)
end

describe "#merch_months_in" do
it "returns merch date for start_date if start_date is the same as end_date" do
Expand Down

0 comments on commit 525f082

Please sign in to comment.