Skip to content

Commit

Permalink
Merge 285a427 into b790f19
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Worek committed Jun 30, 2017
2 parents b790f19 + 285a427 commit 67cf1ba
Show file tree
Hide file tree
Showing 14 changed files with 426 additions and 167 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Expand Up @@ -2,3 +2,9 @@ coverage/
doc
.yardoc/
pkg/

# ctags
tags

# vim
*.swp
9 changes: 7 additions & 2 deletions Gemfile.lock
@@ -1,11 +1,12 @@
PATH
remote: .
specs:
merch_calendar (0.0.5)
merch_calendar (0.1.0.rc1)

GEM
remote: https://www.rubygems.org/
specs:
byebug (9.0.6)
coderay (1.1.1)
coveralls (0.8.13)
json (~> 1.8)
Expand All @@ -21,6 +22,9 @@ GEM
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
pry-byebug (3.4.2)
byebug (~> 9.0)
pry (~> 0.10)
rake (10.5.0)
rspec (3.4.0)
rspec-core (~> 3.4.0)
Expand Down Expand Up @@ -53,9 +57,10 @@ DEPENDENCIES
coveralls
merch_calendar!
pry
pry-byebug
rake
rspec (>= 3.0.0)
simplecov

BUNDLED WITH
1.15.0
1.15.1
3 changes: 2 additions & 1 deletion lib/merch_calendar.rb
Expand Up @@ -9,4 +9,5 @@ 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'
require_relative 'merch_calendar/fiscal_year_calendar'
62 changes: 62 additions & 0 deletions lib/merch_calendar/fiscal_year_calendar.rb
@@ -0,0 +1,62 @@
require "merch_calendar/retail_calendar"

module MerchCalendar
class FiscalYearCalendar
def initialize
@retail_calendar = RetailCalendar.new
end

# The day after last years' end date
def start_of_year(year)
start_of_quarter(year, 1)
end

def end_of_year(year)
end_of_quarter(year, 4)
end

# Return the starting date for a particular quarter
def start_of_quarter(year, quarter)
@retail_calendar.start_of_quarter(*offset_quarter(year, quarter))
end

# Return the ending date for a particular quarter
def end_of_quarter(year, quarter)
@retail_calendar.end_of_quarter(*offset_quarter(year, quarter))
end

def start_of_month(year, merch_month)
@retail_calendar.start_of_month(*offset_month(year, merch_month))
end

def end_of_month(year, merch_month)
@retail_calendar.end_of_month(*offset_month(year, merch_month))
end

# Returns the date that corresponds to the first day in the merch week
def start_of_week(year, merch_month, merch_week)
@retail_calendar.start_of_week(*offset_month(year, merch_month), merch_week)
end

# Returns the date that corresponds to the last day in the merch week
def end_of_week(year, merch_month, merch_week)
@retail_calendar.end_of_week(*offset_month(year, merch_month), merch_week)
end

def weeks_in_year(year)
@retail_calendar.weeks_in_year(year - 1)
end

private

def offset_quarter(year, quarter)
# first quarter in fiscal calendar is Q3 of retail calendar of previous year
yr, qt = quarter >= 3 ? [year, quarter - 2] : [year - 1, quarter + 2]
end

def offset_month(year, month)
# first month in fiscal calendar is the sixth month in the retail calendar
yr, mn = month >= 7 ? [year, month - 6] : [year - 1, month + 6]
end
end
end
18 changes: 9 additions & 9 deletions lib/merch_calendar/merch_week.rb
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 ||= MerchCalendar.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
@@ -1,12 +1,9 @@
require "date"

module MerchCalendar

# @api private
class DateCalculator

class RetailCalendar
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 @@ -53,41 +50,39 @@ 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
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 All @@ -96,22 +91,6 @@ def weeks_in_year(year)
((start_of_year(year + 1) - start_of_year(year)) / 7).to_i
end

def merch_to_julian(merch_month)
if merch_month == 12
1
else
merch_month + 1
end
end

def julian_to_merch(julian_month)
if julian_month == 1
12
else
julian_month - 1
end
end

def merch_months_in(start_date, end_date)
merch_months = []
prev_date = start_date - 2
Expand Down

0 comments on commit 67cf1ba

Please sign in to comment.