merch_calendar 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +3 -0
- data/.rspec +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +4 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +71 -0
- data/LICENSE +19 -0
- data/README.md +93 -0
- data/Rakefile +7 -0
- data/lib/merch_calendar/configurable.rb +28 -0
- data/lib/merch_calendar/configuration.rb +13 -0
- data/lib/merch_calendar/date_calculator.rb +103 -0
- data/lib/merch_calendar/merch_week.rb +224 -0
- data/lib/merch_calendar/util.rb +160 -0
- data/lib/merch_calendar/version.rb +3 -0
- data/lib/merch_calendar.rb +12 -0
- data/merch_calendar.gemspec +26 -0
- data/spec/merch_calendar/configurable_spec.rb +19 -0
- data/spec/merch_calendar/configuration_spec.rb +17 -0
- data/spec/merch_calendar/date_calculator_spec.rb +52 -0
- data/spec/merch_calendar/merch_week_spec.rb +175 -0
- data/spec/merch_calendar/util_spec.rb +105 -0
- data/spec/merch_calendar_spec.rb +58 -0
- data/spec/spec_helper.rb +17 -0
- metadata +145 -0
@@ -0,0 +1,160 @@
|
|
1
|
+
module MerchCalendar
|
2
|
+
|
3
|
+
# Utility methods for the merch calendar
|
4
|
+
module Util
|
5
|
+
|
6
|
+
# The start date of the month
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# # The following are all equivalent, and refer to May 2015
|
10
|
+
# MerchCalendar.start_of_month(2015, 5)
|
11
|
+
# MerchCalendar.start_of_month(2015, month: 5)
|
12
|
+
# MerchCalendar.start_of_month(2015, julian_month: 5)
|
13
|
+
# MerchCalendar.start_of_month(2015, merch_month: 4)
|
14
|
+
#
|
15
|
+
# @param year [Fixnum] the merch year
|
16
|
+
# @param month_param [Fixnum,Hash] an integer specifying the julian month. This can also be a named hash
|
17
|
+
# @option month_param [Fixnum] :month the julian month
|
18
|
+
# @option month_param [Fixnum] :julian_month the julian month
|
19
|
+
# @option month_param [Fixnum] :merch_month the MERCH month
|
20
|
+
#
|
21
|
+
# @return [Date] the starting date of the specified month
|
22
|
+
def start_of_month(year, month_param)
|
23
|
+
merch_month = get_merch_month_param(month_param)
|
24
|
+
date_calc.start_of_month(year, merch_month)
|
25
|
+
end
|
26
|
+
|
27
|
+
# The end date of the month
|
28
|
+
#
|
29
|
+
# @param year [Fixnum] the merch year
|
30
|
+
# @param month_param [Hash] month hash
|
31
|
+
#
|
32
|
+
# @see #start_of_month The start_of_month method for examples using month_param
|
33
|
+
#
|
34
|
+
# @return [Date]
|
35
|
+
def end_of_month(year, month_param)
|
36
|
+
merch_month = get_merch_month_param(month_param)
|
37
|
+
date_calc.end_of_month(year, merch_month)
|
38
|
+
end
|
39
|
+
|
40
|
+
# The start date of the year
|
41
|
+
#
|
42
|
+
# @param year [Fixnum] the merch year
|
43
|
+
#
|
44
|
+
# @return [Date] the starting date of the specified year
|
45
|
+
def start_of_year(year)
|
46
|
+
date_calc.start_of_year(year)
|
47
|
+
end
|
48
|
+
|
49
|
+
# The end date of the year
|
50
|
+
#
|
51
|
+
# @param year [Fixnum] the merch year
|
52
|
+
#
|
53
|
+
# @return [Date] the ending date of the specified year
|
54
|
+
def end_of_year(year)
|
55
|
+
date_calc.end_of_year(year)
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
# The start date of the quarter
|
60
|
+
#
|
61
|
+
# @param year [Fixnum] the merch year
|
62
|
+
# @param quarter [Fixnum] the quarter
|
63
|
+
#
|
64
|
+
# @return [Date] the starting date of the specified quarter
|
65
|
+
def start_of_quarter(year, quarter)
|
66
|
+
date_calc.start_of_quarter(year, quarter)
|
67
|
+
end
|
68
|
+
|
69
|
+
# The end date of the quarter
|
70
|
+
#
|
71
|
+
# @param year [Fixnum] the merch year
|
72
|
+
# @param quarter [Fixnum] the quarter
|
73
|
+
#
|
74
|
+
# @return [Date] the ending date of the specified quarter
|
75
|
+
def end_of_quarter(year, quarter)
|
76
|
+
date_calc.end_of_quarter(year, quarter)
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
# Returns the number of weeks in a given merch year
|
81
|
+
#
|
82
|
+
# @param year [Fixnum] the merch year
|
83
|
+
#
|
84
|
+
# @return [Fixnum] number of weeks
|
85
|
+
def weeks_in_year(year)
|
86
|
+
date_calc.weeks_in_year(year)
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
# Converts a merch month to the correct julian month
|
92
|
+
#
|
93
|
+
# @param month [Fixnum] the merch month to convert
|
94
|
+
# @return [Fixnum] the julian month
|
95
|
+
def merch_to_julian(month)
|
96
|
+
date_calc.merch_to_julian(month)
|
97
|
+
end
|
98
|
+
|
99
|
+
# Converts a julian month to a merch month
|
100
|
+
#
|
101
|
+
# @param month [Fixnum] the julian month to convert
|
102
|
+
# @return [Fixnum] the merch month
|
103
|
+
def julian_to_merch(month)
|
104
|
+
date_calc.julian_to_merch(month)
|
105
|
+
end
|
106
|
+
|
107
|
+
# An array of merch weeks in a given month
|
108
|
+
#
|
109
|
+
# @param year [Fixnum] the merch year
|
110
|
+
# @param month_param [Hash] month hash
|
111
|
+
#
|
112
|
+
# @see #start_of_month The start_of_month method for examples using month_param
|
113
|
+
#
|
114
|
+
# @return [Array<MerchWeek>]
|
115
|
+
def weeks_for_month(year, month_param)
|
116
|
+
merch_month = get_merch_month_param(month_param)
|
117
|
+
|
118
|
+
start_date = date_calc.start_of_month(year, merch_month)
|
119
|
+
|
120
|
+
weeks = (date_calc.end_of_month(year, merch_month) - start_date + 1) / 7
|
121
|
+
|
122
|
+
(1..weeks).map do |week_num|
|
123
|
+
week_start = start_date + ((week_num - 1) * 7)
|
124
|
+
week_end = week_start + 6
|
125
|
+
MerchWeek.new(week_start, { start_of_week: week_start, end_of_week: week_end, week: week_num })
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
|
130
|
+
private
|
131
|
+
|
132
|
+
def date_calc
|
133
|
+
@date_calc ||= DateCalculator.new
|
134
|
+
end
|
135
|
+
|
136
|
+
# Reads the provided parameter and converts the value
|
137
|
+
# to a MERCH MONTH
|
138
|
+
def get_merch_month_param(param)
|
139
|
+
if param.is_a? Fixnum
|
140
|
+
return date_calc.julian_to_merch(param)
|
141
|
+
elsif param.is_a? Hash
|
142
|
+
julian_month = param.delete(:julian_month) || param.delete(:month)
|
143
|
+
merch_month = param.delete(:merch_month)
|
144
|
+
if merch_month
|
145
|
+
return merch_month
|
146
|
+
elsif julian_month
|
147
|
+
return date_calc.julian_to_merch(julian_month)
|
148
|
+
end
|
149
|
+
end
|
150
|
+
raise ArgumentError
|
151
|
+
end
|
152
|
+
|
153
|
+
end
|
154
|
+
|
155
|
+
# Load the utils into the MerchCalendar namespace
|
156
|
+
class << self
|
157
|
+
include Util
|
158
|
+
end
|
159
|
+
|
160
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
module MerchCalendar
|
4
|
+
|
5
|
+
end
|
6
|
+
|
7
|
+
require_relative 'merch_calendar/version'
|
8
|
+
require_relative 'merch_calendar/util'
|
9
|
+
require_relative 'merch_calendar/configurable'
|
10
|
+
require_relative 'merch_calendar/configuration'
|
11
|
+
require_relative 'merch_calendar/merch_week'
|
12
|
+
require_relative 'merch_calendar/date_calculator'
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require 'merch_calendar/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "merch_calendar"
|
7
|
+
s.version = MerchCalendar::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ['Mitch Dempsey']
|
10
|
+
s.email = ['mitch@stitchfix.com']
|
11
|
+
s.licenses = ['MIT']
|
12
|
+
s.homepage = "https://github.com/stitchfix/merch_calendar"
|
13
|
+
s.summary = "Utility for manipulating dates within a 4-5-4 retail calendar"
|
14
|
+
s.description = "Utility for manipulating dates within a 4-5-4 retail calendar"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency "rake"
|
22
|
+
s.add_development_dependency "rspec", ">= 3.0.0"
|
23
|
+
s.add_development_dependency "simplecov"
|
24
|
+
s.add_development_dependency "coveralls"
|
25
|
+
s.add_development_dependency "pry"
|
26
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
describe MerchCalendar do
|
5
|
+
describe "#reset_config!" do
|
6
|
+
it "resets the configuration to defaults" do
|
7
|
+
MerchCalendar.configure do |config|
|
8
|
+
config.quarter_start_month = 1
|
9
|
+
end
|
10
|
+
|
11
|
+
MerchCalendar.reset_config!
|
12
|
+
|
13
|
+
expect(MerchCalendar.configuration.quarter_start_month).to eq MerchCalendar::Configuration.new.quarter_start_month
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MerchCalendar::Configuration do
|
4
|
+
describe "#quarter_start_month" do
|
5
|
+
it "default value is 8" do
|
6
|
+
expect(described_class.new.quarter_start_month).to eq 8
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "#quarter_start_month=" do
|
11
|
+
it "can set value" do
|
12
|
+
config = described_class.new
|
13
|
+
config.quarter_start_month = 7
|
14
|
+
expect(config.quarter_start_month).to eq 7
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MerchCalendar::DateCalculator do
|
4
|
+
|
5
|
+
subject { described_class.new }
|
6
|
+
|
7
|
+
describe "#julian_to_merch" do
|
8
|
+
it { expect(subject.julian_to_merch(1)).to eq 12 }
|
9
|
+
it { expect(subject.julian_to_merch(2)).to eq 1 }
|
10
|
+
it { expect(subject.julian_to_merch(3)).to eq 2 }
|
11
|
+
it { expect(subject.julian_to_merch(4)).to eq 3 }
|
12
|
+
it { expect(subject.julian_to_merch(5)).to eq 4 }
|
13
|
+
it { expect(subject.julian_to_merch(6)).to eq 5 }
|
14
|
+
it { expect(subject.julian_to_merch(7)).to eq 6 }
|
15
|
+
it { expect(subject.julian_to_merch(8)).to eq 7 }
|
16
|
+
it { expect(subject.julian_to_merch(9)).to eq 8 }
|
17
|
+
it { expect(subject.julian_to_merch(10)).to eq 9 }
|
18
|
+
it { expect(subject.julian_to_merch(11)).to eq 10 }
|
19
|
+
it { expect(subject.julian_to_merch(12)).to eq 11 }
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "#merch_to_julian" do
|
23
|
+
it { expect(subject.merch_to_julian(12)).to eq 1 }
|
24
|
+
it { expect(subject.merch_to_julian(1)).to eq 2 }
|
25
|
+
it { expect(subject.merch_to_julian(2)).to eq 3 }
|
26
|
+
it { expect(subject.merch_to_julian(3)).to eq 4 }
|
27
|
+
it { expect(subject.merch_to_julian(4)).to eq 5 }
|
28
|
+
it { expect(subject.merch_to_julian(5)).to eq 6 }
|
29
|
+
it { expect(subject.merch_to_julian(6)).to eq 7 }
|
30
|
+
it { expect(subject.merch_to_julian(7)).to eq 8 }
|
31
|
+
it { expect(subject.merch_to_julian(8)).to eq 9 }
|
32
|
+
it { expect(subject.merch_to_julian(9)).to eq 10 }
|
33
|
+
it { expect(subject.merch_to_julian(10)).to eq 11 }
|
34
|
+
it { expect(subject.merch_to_julian(11)).to eq 12 }
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "#weeks_in_year" do
|
38
|
+
it "returns 53 for a leap year" do
|
39
|
+
expect(subject.weeks_in_year(2012)).to eq 53
|
40
|
+
end
|
41
|
+
|
42
|
+
it "returns 52 for a normal year" do
|
43
|
+
expect(subject.weeks_in_year(2013)).to eq 52
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it "#start_of_quarter"
|
48
|
+
|
49
|
+
it "#end_of_quarter"
|
50
|
+
|
51
|
+
end
|
52
|
+
|
@@ -0,0 +1,175 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MerchCalendar::MerchWeek do
|
4
|
+
|
5
|
+
describe ".find" do
|
6
|
+
it "returns an array of weeks" do
|
7
|
+
weeks = described_class.find(2014,1)
|
8
|
+
expect(weeks).to be_an Array
|
9
|
+
expect(weeks.size).to eq 4
|
10
|
+
end
|
11
|
+
|
12
|
+
it "with year, month, week" do
|
13
|
+
mw = described_class.find(2014,1,1)
|
14
|
+
expect(mw.year).to eq 2014
|
15
|
+
expect(mw.month).to eq 1
|
16
|
+
expect(mw.merch_month).to eq 12
|
17
|
+
expect(mw.week).to eq 1
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe ".from_date" do
|
22
|
+
|
23
|
+
context "parameters" do
|
24
|
+
context "allows valid date types" do
|
25
|
+
it "allows a date string" do
|
26
|
+
mw = described_class.from_date("1990-10-01")
|
27
|
+
expect(mw.date.to_s).to eq "1990-10-01"
|
28
|
+
expect(mw.date.month).to eq 10
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "throws exception for invalid types" do
|
33
|
+
it { expect{described_class.from_date}.to raise_error(ArgumentError) }
|
34
|
+
it { expect{described_class.from_date(1)}.to raise_error(ArgumentError) }
|
35
|
+
it { expect{described_class.from_date(true)}.to raise_error(ArgumentError) }
|
36
|
+
it { expect{described_class.from_date(1.0)}.to raise_error(ArgumentError) }
|
37
|
+
end
|
38
|
+
|
39
|
+
context "throws exception for invalid dates" do
|
40
|
+
it { expect{described_class.from_date("2015")}.to raise_error(ArgumentError) }
|
41
|
+
it { expect{described_class.from_date("2015-04")}.to raise_error(ArgumentError) }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe ".today" do
|
47
|
+
it "returns a merch week based on today's date" do
|
48
|
+
mw = described_class.today
|
49
|
+
expect(mw.date.to_s).to eq Date.today.to_s
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "#to_s" do
|
54
|
+
let(:merch_week) { described_class.from_date("2014-01-01") }
|
55
|
+
|
56
|
+
it ":short / default format" do
|
57
|
+
expect(merch_week.to_s(:short)).to eq "Dec W5"
|
58
|
+
expect(merch_week.to_s).to eq "Dec W5"
|
59
|
+
expect("#{merch_week}").to eq "Dec W5"
|
60
|
+
end
|
61
|
+
|
62
|
+
it ":long format" do
|
63
|
+
expect(merch_week.to_s(:long)).to eq "2013:48 Dec W5"
|
64
|
+
end
|
65
|
+
|
66
|
+
it ":elasticsearch format" do
|
67
|
+
expect(merch_week.to_s(:elasticsearch)).to eq "2013-12w05"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
it "#end_of_week" do
|
72
|
+
mw = described_class.find(2014,1,1)
|
73
|
+
|
74
|
+
expect(mw.end_of_week).to eq (mw.start_of_week + 6)
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
describe "#end_of_month" do
|
79
|
+
it "for a 4 week month" do
|
80
|
+
mw = described_class.find(2014, 2, 1)
|
81
|
+
expect(mw.end_of_month - mw.start_of_month + 1).to eq (4*7)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "for a 5 week month" do
|
85
|
+
mw = described_class.find(2014, 3, 1)
|
86
|
+
expect(mw.end_of_month - mw.start_of_month + 1).to eq (5*7)
|
87
|
+
end
|
88
|
+
|
89
|
+
it "for a 4 to 5 week month in a leap year" do
|
90
|
+
mw = described_class.find(2011, 1, 1)
|
91
|
+
expect(mw.end_of_month - mw.start_of_month + 1).to eq (4*7)
|
92
|
+
|
93
|
+
mw = described_class.find(2012, 1, 1)
|
94
|
+
expect(mw.end_of_month - mw.start_of_month + 1).to eq (5*7)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe "#season" do
|
99
|
+
context "Fall/Winter" do
|
100
|
+
it { expect(described_class.from_date("2011-08-06").season).to eq "Fall/Winter" }
|
101
|
+
it { expect(described_class.from_date("2011-09-06").season).to eq "Fall/Winter" }
|
102
|
+
it { expect(described_class.from_date("2011-10-06").season).to eq "Fall/Winter" }
|
103
|
+
it { expect(described_class.from_date("2011-11-06").season).to eq "Fall/Winter" }
|
104
|
+
it { expect(described_class.from_date("2011-12-06").season).to eq "Fall/Winter" }
|
105
|
+
it { expect(described_class.from_date("2012-01-06").season).to eq "Fall/Winter" }
|
106
|
+
end
|
107
|
+
|
108
|
+
context "Spring/Summer" do
|
109
|
+
it { expect(described_class.from_date("2011-02-06").season).to eq "Spring/Summer" }
|
110
|
+
it { expect(described_class.from_date("2011-03-06").season).to eq "Spring/Summer" }
|
111
|
+
it { expect(described_class.from_date("2011-04-06").season).to eq "Spring/Summer" }
|
112
|
+
it { expect(described_class.from_date("2011-05-06").season).to eq "Spring/Summer" }
|
113
|
+
it { expect(described_class.from_date("2011-06-06").season).to eq "Spring/Summer" }
|
114
|
+
it { expect(described_class.from_date("2011-07-06").season).to eq "Spring/Summer" }
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
|
119
|
+
context "logic" do
|
120
|
+
[
|
121
|
+
OpenStruct.new(date: "2011-05-01", year: 2011, month: 5, week: 1, quarter: 4, year_week: 14, start_date: "2011-01-30"),
|
122
|
+
|
123
|
+
OpenStruct.new(date: "2014-08-31", year: 2014, month: 9, week: 1, quarter: 1, year_week: 31, start_date: "2014-02-02"),
|
124
|
+
OpenStruct.new(date: "2017-12-30", year: 2017, month: 12, week: 5, quarter: 2, year_week: 48, start_date: "2017-01-29"),
|
125
|
+
OpenStruct.new(date: "2014-01-01", year: 2013, month: 12, week: 5, quarter: 2, year_week: 48, start_date: "2013-02-03"),
|
126
|
+
OpenStruct.new(date: "2014-01-04", year: 2013, month: 12, week: 5, quarter: 2, year_week: 48, start_date: "2013-02-03"),
|
127
|
+
OpenStruct.new(date: "2014-01-05", year: 2013, month: 1, week: 1, quarter: 2, year_week: 49, start_date: "2013-02-03"),
|
128
|
+
OpenStruct.new(date: "2014-01-12", year: 2013, month: 1, week: 2, quarter: 2, year_week: 50, start_date: "2013-02-03"),
|
129
|
+
OpenStruct.new(date: "2014-01-19", year: 2013, month: 1, week: 3, quarter: 2, year_week: 51, start_date: "2013-02-03"),
|
130
|
+
OpenStruct.new(date: "2014-01-26", year: 2013, month: 1, week: 4, quarter: 2, year_week: 52, start_date: "2013-02-03"),
|
131
|
+
|
132
|
+
# 2013
|
133
|
+
OpenStruct.new(date: "2013-02-03", year: 2013, month: 2, week: 1, quarter: 3, year_week: 1, start_date: "2013-02-03"),
|
134
|
+
|
135
|
+
# 2014
|
136
|
+
OpenStruct.new(date: "2014-02-02", year: 2014, month: 2, week: 1, quarter: 3, year_week: 1, start_date: "2014-02-02"),
|
137
|
+
OpenStruct.new(date: "2015-01-31", year: 2014, month: 1, week: 4, quarter: 2, year_week: 52, start_date: "2014-02-02"),
|
138
|
+
|
139
|
+
OpenStruct.new(date: "2014-02-01", year: 2013, month: 1, week: 4, quarter: 2, year_week: 52, start_date: "2013-02-03"),
|
140
|
+
|
141
|
+
OpenStruct.new(date: "2015-02-01", year: 2015, month: 2, week: 1, quarter: 3, year_week: 1, start_date: "2015-02-01"),
|
142
|
+
|
143
|
+
].each do |date_check|
|
144
|
+
|
145
|
+
context "using date '#{date_check.date}'" do
|
146
|
+
let(:merch_week) { described_class.from_date(date_check.date) }
|
147
|
+
|
148
|
+
it "#year_week" do
|
149
|
+
expect(merch_week.year_week).to eq date_check.year_week
|
150
|
+
end
|
151
|
+
|
152
|
+
it "#month" do
|
153
|
+
expect(merch_week.month).to eq date_check.month
|
154
|
+
end
|
155
|
+
|
156
|
+
it "#quarter" do
|
157
|
+
expect(merch_week.quarter).to eq date_check.quarter
|
158
|
+
end
|
159
|
+
|
160
|
+
it "#week" do
|
161
|
+
expect(merch_week.week).to eq date_check.week
|
162
|
+
end
|
163
|
+
|
164
|
+
it "#start_of_year" do
|
165
|
+
expect(merch_week.start_of_year.to_s).to eq date_check.start_date
|
166
|
+
end
|
167
|
+
|
168
|
+
it "#year" do
|
169
|
+
expect(merch_week.year).to eq date_check.year
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MerchCalendar::Util do
|
4
|
+
|
5
|
+
describe "#weeks_for_month" do
|
6
|
+
context "correct number of weeks" do
|
7
|
+
it "returns 4 weeks for a 4-week month" do
|
8
|
+
weeks = MerchCalendar.weeks_for_month(2014, 4)
|
9
|
+
expect(weeks.size).to eq 4
|
10
|
+
end
|
11
|
+
|
12
|
+
it "returns 5 weeks for a 5-week month" do
|
13
|
+
weeks = MerchCalendar.weeks_for_month(2014, 3)
|
14
|
+
expect(weeks.size).to eq 5
|
15
|
+
end
|
16
|
+
|
17
|
+
it "returns 5 weeks for a 4-week end month if leap year" do
|
18
|
+
weeks = MerchCalendar.weeks_for_month(2012, 1)
|
19
|
+
expect(weeks.size).to eq 5
|
20
|
+
|
21
|
+
weeks = MerchCalendar.weeks_for_month(2014, 1)
|
22
|
+
expect(weeks.size).to eq 4
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "gives correct weeks" do
|
27
|
+
let!(:weeks) { MerchCalendar.weeks_for_month(2014,1) }
|
28
|
+
|
29
|
+
it "misc checks" do
|
30
|
+
expect(weeks.size).to eq 4
|
31
|
+
end
|
32
|
+
|
33
|
+
it "#week" do
|
34
|
+
expect(weeks[0].week).to eq 1
|
35
|
+
expect(weeks[1].week).to eq 2
|
36
|
+
expect(weeks[2].week).to eq 3
|
37
|
+
expect(weeks[3].week).to eq 4
|
38
|
+
end
|
39
|
+
|
40
|
+
it "#date" do
|
41
|
+
expect(weeks[0].date).to eq Date.new(2015,1,4)
|
42
|
+
expect(weeks[1].date).to eq Date.new(2015,1,11)
|
43
|
+
expect(weeks[2].date).to eq Date.new(2015,1,18)
|
44
|
+
expect(weeks[3].date).to eq Date.new(2015,1,25)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "#start_of_week" do
|
48
|
+
expect(weeks[0].start_of_week).to eq Date.new(2015,1,4)
|
49
|
+
expect(weeks[1].start_of_week).to eq Date.new(2015,1,11)
|
50
|
+
expect(weeks[2].start_of_week).to eq Date.new(2015,1,18)
|
51
|
+
expect(weeks[3].start_of_week).to eq Date.new(2015,1,25)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "#start_of_year" do
|
55
|
+
expect(weeks.map(&:start_of_year)).to all( eq Date.new(2014,2,2) )
|
56
|
+
end
|
57
|
+
|
58
|
+
it "#end_of_year" do
|
59
|
+
expect(weeks.map(&:end_of_year)).to all( eq Date.new(2015,1,31) )
|
60
|
+
end
|
61
|
+
|
62
|
+
it "#month" do
|
63
|
+
expect(weeks.map(&:month)).to all( eq 1 )
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe '#get_merch_month_param' do
|
70
|
+
let(:date_calc) { MerchCalendar::DateCalculator.new }
|
71
|
+
before(:each) do
|
72
|
+
allow(MerchCalendar).to receive(:date_calc).and_return(date_calc)
|
73
|
+
end
|
74
|
+
|
75
|
+
context "valid calls" do
|
76
|
+
before(:each) do
|
77
|
+
expect(date_calc).to receive(:start_of_month).with(2014, 1)
|
78
|
+
end
|
79
|
+
it "assumes an integer is a julian month" do
|
80
|
+
MerchCalendar.start_of_month(2014, 2)
|
81
|
+
end
|
82
|
+
|
83
|
+
context "when passed a hash" do
|
84
|
+
it "accepts :month as julian month" do
|
85
|
+
MerchCalendar.start_of_month(2014, month: 2)
|
86
|
+
end
|
87
|
+
|
88
|
+
it "accepts :julian_month as julian month" do
|
89
|
+
MerchCalendar.start_of_month(2014, julian_month: 2)
|
90
|
+
end
|
91
|
+
|
92
|
+
it "accepts :merch_month as merch month" do
|
93
|
+
MerchCalendar.start_of_month(2014, merch_month: 1)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context "errors" do
|
99
|
+
before(:each) do
|
100
|
+
expect(date_calc).to_not receive(:start_of_month)
|
101
|
+
end
|
102
|
+
it { expect { MerchCalendar.start_of_month(2014, :april) }.to raise_error(ArgumentError) }
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MerchCalendar do
|
4
|
+
context "responds to util methods" do
|
5
|
+
[
|
6
|
+
:start_of_month, :end_of_month,
|
7
|
+
:start_of_year, :end_of_year,
|
8
|
+
:start_of_quarter, :end_of_quarter,
|
9
|
+
:merch_to_julian,
|
10
|
+
:julian_to_merch,
|
11
|
+
:weeks_in_year,
|
12
|
+
:weeks_for_month
|
13
|
+
].each do |method_name|
|
14
|
+
it "responds to the #{method_name} method" do
|
15
|
+
expect(described_class).to respond_to(method_name)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
it "#start_of_month" do
|
22
|
+
expect(described_class.start_of_month(2014,1)).to be_a Date
|
23
|
+
end
|
24
|
+
|
25
|
+
it "#end_of_month" do
|
26
|
+
expect(described_class.end_of_month(2014,1)).to be_a Date
|
27
|
+
end
|
28
|
+
|
29
|
+
it "#start_of_year" do
|
30
|
+
expect(described_class.start_of_year(2014)).to be_a Date
|
31
|
+
end
|
32
|
+
|
33
|
+
it "#end_of_year" do
|
34
|
+
expect(described_class.end_of_year(2014)).to be_a Date
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
it "#start_of_quarter" do
|
39
|
+
expect(described_class.start_of_quarter(2014,1)).to be_a Date
|
40
|
+
end
|
41
|
+
|
42
|
+
it "#end_of_quarter" do
|
43
|
+
expect(described_class.end_of_quarter(2014,1)).to be_a Date
|
44
|
+
end
|
45
|
+
|
46
|
+
it "#weeks_in_year" do
|
47
|
+
expect(described_class.weeks_in_year(2014)).to be_a Fixnum
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
it "#merch_to_julian" do
|
52
|
+
expect(described_class.merch_to_julian(1)).to be_a Fixnum
|
53
|
+
end
|
54
|
+
it "#julian_to_merch" do
|
55
|
+
expect(described_class.julian_to_merch(1)).to be_a Fixnum
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require "simplecov"
|
2
|
+
require "coveralls"
|
3
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
4
|
+
SimpleCov::Formatter::HTMLFormatter,
|
5
|
+
Coveralls::SimpleCov::Formatter
|
6
|
+
]
|
7
|
+
SimpleCov.start { add_filter "/spec/" }
|
8
|
+
|
9
|
+
require "pry"
|
10
|
+
require "merch_calendar"
|
11
|
+
|
12
|
+
RSpec.configure do |config|
|
13
|
+
config.mock_with :rspec do |mocks_config|
|
14
|
+
mocks_config.verify_doubled_constant_names = true
|
15
|
+
mocks_config.verify_partial_doubles = true
|
16
|
+
end
|
17
|
+
end
|