merch_calendar 0.0.5 → 0.1.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f96df8a358dab3d900fcdcd63295f26b87b76781
4
- data.tar.gz: eac675dcee19465f4f52b1a6917547aa915121fd
3
+ metadata.gz: eb41f5342b94855990d76a316fd9fe32a059e2ea
4
+ data.tar.gz: d16f77b34c7c286fae1a71795d00a2e87d034f8e
5
5
  SHA512:
6
- metadata.gz: 14925ac6a035b7122c552f13aa7c795222ecc4e63866f4a4381f16e3bd7fca1b80c5450465a07553cff7d4be9af2c8e628a5393379a556935bfa271d16462e5f
7
- data.tar.gz: 41fe2d24d57d8c1ab1640b386bfb89a08bf9ba28bc0960aed1108256e0aa421d7ff7dbed0dffc33036acefc81aaed6da2828a234197fb7d93f2e68dbe894bd16
6
+ metadata.gz: 0523a60e48200560bfe2218f7bdfb05e41fe99d83460654ccc192b2229cc0ca6fded1fd2504c10c5c053879e6c4187e59bee73bef503f67f2730e286a76e0d8b
7
+ data.tar.gz: 49e67aa34e6f46e62077d735fab174c6684b394c008b50377e9a451711c0b7da7fe18cace1f58790e181b518dc05759e013a58ce1d39f39abd809354e4faa7ea
data/.gitignore CHANGED
@@ -2,3 +2,9 @@ coverage/
2
2
  doc
3
3
  .yardoc/
4
4
  pkg/
5
+
6
+ # ctags
7
+ tags
8
+
9
+ # vim
10
+ *.swp
data/Gemfile.lock CHANGED
@@ -1,11 +1,12 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- merch_calendar (0.0.5)
4
+ merch_calendar (0.1.0.rc1)
5
5
 
6
6
  GEM
7
7
  remote: https://www.rubygems.org/
8
8
  specs:
9
+ byebug (9.0.6)
9
10
  coderay (1.1.1)
10
11
  coveralls (0.8.13)
11
12
  json (~> 1.8)
@@ -21,6 +22,9 @@ GEM
21
22
  coderay (~> 1.1.0)
22
23
  method_source (~> 0.8.1)
23
24
  slop (~> 3.4)
25
+ pry-byebug (3.4.2)
26
+ byebug (~> 9.0)
27
+ pry (~> 0.10)
24
28
  rake (10.5.0)
25
29
  rspec (3.4.0)
26
30
  rspec-core (~> 3.4.0)
@@ -53,9 +57,10 @@ DEPENDENCIES
53
57
  coveralls
54
58
  merch_calendar!
55
59
  pry
60
+ pry-byebug
56
61
  rake
57
62
  rspec (>= 3.0.0)
58
63
  simplecov
59
64
 
60
65
  BUNDLED WITH
61
- 1.15.0
66
+ 1.15.1
@@ -9,4 +9,5 @@ require_relative 'merch_calendar/util'
9
9
  require_relative 'merch_calendar/configurable'
10
10
  require_relative 'merch_calendar/configuration'
11
11
  require_relative 'merch_calendar/merch_week'
12
- require_relative 'merch_calendar/date_calculator'
12
+ require_relative 'merch_calendar/retail_calendar'
13
+ require_relative 'merch_calendar/fiscal_year_calendar'
@@ -0,0 +1,62 @@
1
+ require "merch_calendar/retail_calendar"
2
+
3
+ module MerchCalendar
4
+ class FiscalYearCalendar
5
+ def initialize
6
+ @retail_calendar = RetailCalendar.new
7
+ end
8
+
9
+ # The day after last years' end date
10
+ def start_of_year(year)
11
+ start_of_quarter(year, 1)
12
+ end
13
+
14
+ def end_of_year(year)
15
+ end_of_quarter(year, 4)
16
+ end
17
+
18
+ # Return the starting date for a particular quarter
19
+ def start_of_quarter(year, quarter)
20
+ @retail_calendar.start_of_quarter(*offset_quarter(year, quarter))
21
+ end
22
+
23
+ # Return the ending date for a particular quarter
24
+ def end_of_quarter(year, quarter)
25
+ @retail_calendar.end_of_quarter(*offset_quarter(year, quarter))
26
+ end
27
+
28
+ def start_of_month(year, merch_month)
29
+ @retail_calendar.start_of_month(*offset_month(year, merch_month))
30
+ end
31
+
32
+ def end_of_month(year, merch_month)
33
+ @retail_calendar.end_of_month(*offset_month(year, merch_month))
34
+ end
35
+
36
+ # Returns the date that corresponds to the first day in the merch week
37
+ def start_of_week(year, merch_month, merch_week)
38
+ @retail_calendar.start_of_week(*offset_month(year, merch_month), merch_week)
39
+ end
40
+
41
+ # Returns the date that corresponds to the last day in the merch week
42
+ def end_of_week(year, merch_month, merch_week)
43
+ @retail_calendar.end_of_week(*offset_month(year, merch_month), merch_week)
44
+ end
45
+
46
+ def weeks_in_year(year)
47
+ @retail_calendar.weeks_in_year(year - 1)
48
+ end
49
+
50
+ private
51
+
52
+ def offset_quarter(year, quarter)
53
+ # first quarter in fiscal calendar is Q3 of retail calendar of previous year
54
+ yr, qt = quarter >= 3 ? [year, quarter - 2] : [year - 1, quarter + 2]
55
+ end
56
+
57
+ def offset_month(year, month)
58
+ # first month in fiscal calendar is the sixth month in the retail calendar
59
+ yr, mn = month >= 7 ? [year, month - 6] : [year - 1, month + 6]
60
+ end
61
+ end
62
+ end
@@ -88,7 +88,7 @@ module MerchCalendar
88
88
  # TODO: This is very inefficient, but less complex than strategic guessing
89
89
  # maybe switch to a binary search or something
90
90
  @merch_month ||= (1..12).detect do |num|
91
- date_calc.end_of_month(start_of_year.year, num) >= date && date >= date_calc.start_of_month(start_of_year.year, num)
91
+ retail_calendar.end_of_month(start_of_year.year, num) >= date && date >= retail_calendar.start_of_month(start_of_year.year, num)
92
92
  end
93
93
  end
94
94
 
@@ -103,7 +103,7 @@ module MerchCalendar
103
103
  #
104
104
  # @return [Fixnum]
105
105
  def month
106
- @month ||= date_calc.merch_to_julian(merch_month)
106
+ @month ||= MerchCalendar.merch_to_julian(merch_month)
107
107
  end
108
108
 
109
109
  # The specific quarter this week falls in
@@ -155,21 +155,21 @@ module MerchCalendar
155
155
  #
156
156
  # @return [Date]
157
157
  def end_of_year
158
- @end_of_year ||= date_calc.end_of_year(year)
158
+ @end_of_year ||= retail_calendar.end_of_year(year)
159
159
  end
160
160
 
161
161
  # The start date of the merch month
162
162
  #
163
163
  # @return [Date]
164
164
  def start_of_month
165
- @start_of_month ||= date_calc.start_of_month(year, merch_month)
165
+ @start_of_month ||= retail_calendar.start_of_month(year, merch_month)
166
166
  end
167
167
 
168
168
  # The end date of the merch month
169
169
  #
170
170
  # @return [Date]
171
171
  def end_of_month
172
- @end_of_month ||= date_calc.end_of_month(year, merch_month)
172
+ @end_of_month ||= retail_calendar.end_of_month(year, merch_month)
173
173
  end
174
174
 
175
175
  # The merch season this date falls under.
@@ -209,15 +209,15 @@ module MerchCalendar
209
209
  private
210
210
 
211
211
  def year_start_date
212
- start_date = date_calc.start_of_year(date.year)
212
+ start_date = retail_calendar.start_of_year(date.year)
213
213
  if start_date > date
214
- start_date = date_calc.start_of_year(date.year - 1)
214
+ start_date = retail_calendar.start_of_year(date.year - 1)
215
215
  end
216
216
  start_date
217
217
  end
218
218
 
219
- def date_calc
220
- @date_calc ||= DateCalculator.new
219
+ def retail_calendar
220
+ @retail_calendar ||= RetailCalendar.new
221
221
  end
222
222
 
223
223
  end
@@ -1,12 +1,9 @@
1
1
  require "date"
2
2
 
3
3
  module MerchCalendar
4
-
5
- # @api private
6
- class DateCalculator
7
-
4
+ class RetailCalendar
8
5
  def end_of_year(year)
9
- year_end = Date.new (year + 1), 1, -1
6
+ year_end = Date.new((year + 1), 1, -1)
10
7
  wday = (year_end.wday + 1) % 7
11
8
 
12
9
  if wday > 3
@@ -53,27 +50,25 @@ module MerchCalendar
53
50
 
54
51
  # Returns the date that corresponds to the first day in the merch week
55
52
  def start_of_week(year, month, merch_week)
56
- week = MerchCalendar::MerchWeek.find(year, month, merch_week)
57
- week.start_of_week
53
+ start_of_month(year, month) + ((merch_week - 1) * 7)
58
54
  end
59
55
 
60
56
  # Returns the date that corresponds to the last day in the merch week
61
57
  def end_of_week(year, month, merch_week)
62
- week = MerchCalendar::MerchWeek.find(year, month, merch_week)
63
- week.end_of_week
58
+ start_of_month(year, month) + (6 + ((merch_week - 1) * 7))
64
59
  end
65
60
 
66
61
  # Return the starting date for a particular quarter
67
62
  def start_of_quarter(year, quarter)
68
63
  case quarter
69
64
  when 1
70
- start_of_month(year, 7)
65
+ start_of_month(year, 1)
71
66
  when 2
72
- start_of_month(year, 10)
67
+ start_of_month(year, 4)
73
68
  when 3
74
- start_of_month(year, 1)
69
+ start_of_month(year, 7)
75
70
  when 4
76
- start_of_month(year, 4)
71
+ start_of_month(year, 10)
77
72
  end
78
73
  end
79
74
 
@@ -81,13 +76,13 @@ module MerchCalendar
81
76
  def end_of_quarter(year, quarter)
82
77
  case quarter
83
78
  when 1
84
- end_of_month(year, 9)
79
+ end_of_month(year, 3)
85
80
  when 2
86
- end_of_month(year, 12)
81
+ end_of_month(year, 6)
87
82
  when 3
88
- end_of_month(year, 3)
83
+ end_of_month(year, 9)
89
84
  when 4
90
- end_of_month(year, 6)
85
+ end_of_month(year, 12)
91
86
  end
92
87
  end
93
88
 
@@ -96,22 +91,6 @@ module MerchCalendar
96
91
  ((start_of_year(year + 1) - start_of_year(year)) / 7).to_i
97
92
  end
98
93
 
99
- def merch_to_julian(merch_month)
100
- if merch_month == 12
101
- 1
102
- else
103
- merch_month + 1
104
- end
105
- end
106
-
107
- def julian_to_merch(julian_month)
108
- if julian_month == 1
109
- 12
110
- else
111
- julian_month - 1
112
- end
113
- end
114
-
115
94
  def merch_months_in(start_date, end_date)
116
95
  merch_months = []
117
96
  prev_date = start_date - 2
@@ -7,22 +7,22 @@ module MerchCalendar
7
7
  #
8
8
  # @param year [Fixnum] the merch year
9
9
  # @param month [Fixnum] an integer specifying the julian month.
10
- # @param month [Fixnum] an integer specifying the merch week.
10
+ # @param week [Fixnum] an integer specifying the merch week.
11
11
  #
12
12
  # @return [Date] the starting date of the specified week
13
13
  def start_of_week(year, month, week)
14
- date_calc.start_of_week(year, month, week)
14
+ retail_calendar.start_of_week(year, julian_to_merch(month), week)
15
15
  end
16
16
 
17
17
  # The end date of the week
18
18
  #
19
19
  # @param year [Fixnum] the merch year
20
20
  # @param month [Fixnum] an integer specifying the julian month.
21
- # @param month [Fixnum] an integer specifying the merch week.
21
+ # @param week [Fixnum] an integer specifying the merch week.
22
22
  #
23
23
  # @return [Date] the ending date of the specified week
24
24
  def end_of_week(year, month, week)
25
- date_calc.end_of_week(year, month, week)
25
+ retail_calendar.end_of_week(year, julian_to_merch(month), week)
26
26
  end
27
27
 
28
28
  # The start date of the month
@@ -43,7 +43,7 @@ module MerchCalendar
43
43
  # @return [Date] the starting date of the specified month
44
44
  def start_of_month(year, month_param)
45
45
  merch_month = get_merch_month_param(month_param)
46
- date_calc.start_of_month(year, merch_month)
46
+ retail_calendar.start_of_month(year, merch_month)
47
47
  end
48
48
 
49
49
  # The end date of the month
@@ -56,7 +56,7 @@ module MerchCalendar
56
56
  # @return [Date]
57
57
  def end_of_month(year, month_param)
58
58
  merch_month = get_merch_month_param(month_param)
59
- date_calc.end_of_month(year, merch_month)
59
+ retail_calendar.end_of_month(year, merch_month)
60
60
  end
61
61
 
62
62
  # The start date of the year
@@ -65,7 +65,7 @@ module MerchCalendar
65
65
  #
66
66
  # @return [Date] the starting date of the specified year
67
67
  def start_of_year(year)
68
- date_calc.start_of_year(year)
68
+ retail_calendar.start_of_year(year)
69
69
  end
70
70
 
71
71
  # The end date of the year
@@ -74,7 +74,7 @@ module MerchCalendar
74
74
  #
75
75
  # @return [Date] the ending date of the specified year
76
76
  def end_of_year(year)
77
- date_calc.end_of_year(year)
77
+ retail_calendar.end_of_year(year)
78
78
  end
79
79
 
80
80
 
@@ -85,7 +85,7 @@ module MerchCalendar
85
85
  #
86
86
  # @return [Date] the starting date of the specified quarter
87
87
  def start_of_quarter(year, quarter)
88
- date_calc.start_of_quarter(year, quarter)
88
+ retail_calendar.start_of_quarter(year, quarter)
89
89
  end
90
90
 
91
91
  # The end date of the quarter
@@ -95,7 +95,7 @@ module MerchCalendar
95
95
  #
96
96
  # @return [Date] the ending date of the specified quarter
97
97
  def end_of_quarter(year, quarter)
98
- date_calc.end_of_quarter(year, quarter)
98
+ retail_calendar.end_of_quarter(year, quarter)
99
99
  end
100
100
 
101
101
 
@@ -105,7 +105,7 @@ module MerchCalendar
105
105
  #
106
106
  # @return [Fixnum] number of weeks
107
107
  def weeks_in_year(year)
108
- date_calc.weeks_in_year(year)
108
+ retail_calendar.weeks_in_year(year)
109
109
  end
110
110
 
111
111
 
@@ -116,26 +116,36 @@ module MerchCalendar
116
116
  #
117
117
  # @return [Array<Date>] array of merch months
118
118
  def merch_months_in(start_date, end_date)
119
- date_calc.merch_months_in(start_date, end_date)
119
+ retail_calendar.merch_months_in(start_date, end_date)
120
120
  end
121
121
 
122
122
 
123
123
  # Converts a merch month to the correct julian month
124
124
  #
125
- # @param month [Fixnum] the merch month to convert
125
+ # @param merch_month [Fixnum] the merch month to convert
126
126
  # @return [Fixnum] the julian month
127
- def merch_to_julian(month)
128
- date_calc.merch_to_julian(month)
127
+ def merch_to_julian(merch_month)
128
+ if merch_month == 12
129
+ 1
130
+ else
131
+ merch_month + 1
132
+ end
129
133
  end
130
134
 
135
+
131
136
  # Converts a julian month to a merch month
132
137
  #
133
- # @param month [Fixnum] the julian month to convert
138
+ # @param julian_month [Fixnum] the julian month to convert
134
139
  # @return [Fixnum] the merch month
135
- def julian_to_merch(month)
136
- date_calc.julian_to_merch(month)
140
+ def julian_to_merch(julian_month)
141
+ if julian_month == 1
142
+ 12
143
+ else
144
+ julian_month - 1
145
+ end
137
146
  end
138
147
 
148
+
139
149
  # An array of merch weeks in a given month
140
150
  #
141
151
  # @param year [Fixnum] the merch year
@@ -147,9 +157,9 @@ module MerchCalendar
147
157
  def weeks_for_month(year, month_param)
148
158
  merch_month = get_merch_month_param(month_param)
149
159
 
150
- start_date = date_calc.start_of_month(year, merch_month)
160
+ start_date = retail_calendar.start_of_month(year, merch_month)
151
161
 
152
- weeks = (date_calc.end_of_month(year, merch_month) - start_date + 1) / 7
162
+ weeks = (retail_calendar.end_of_month(year, merch_month) - start_date + 1) / 7
153
163
 
154
164
  (1..weeks).map do |week_num|
155
165
  week_start = start_date + ((week_num - 1) * 7)
@@ -161,24 +171,26 @@ module MerchCalendar
161
171
 
162
172
  private
163
173
 
164
- def date_calc
165
- @date_calc ||= DateCalculator.new
174
+ def retail_calendar
175
+ @retail_calendar ||= RetailCalendar.new
166
176
  end
167
177
 
168
178
  # Reads the provided parameter and converts the value
169
179
  # to a MERCH MONTH
170
180
  def get_merch_month_param(param)
171
181
  if param.is_a? Fixnum
172
- return date_calc.julian_to_merch(param)
182
+ return julian_to_merch(param)
173
183
  elsif param.is_a? Hash
174
184
  julian_month = param.delete(:julian_month) || param.delete(:month)
175
185
  merch_month = param.delete(:merch_month)
186
+
176
187
  if merch_month
177
188
  return merch_month
178
189
  elsif julian_month
179
- return date_calc.julian_to_merch(julian_month)
190
+ return julian_to_merch(julian_month)
180
191
  end
181
192
  end
193
+
182
194
  raise ArgumentError
183
195
  end
184
196
 
@@ -1,3 +1,3 @@
1
1
  module MerchCalendar
2
- VERSION = '0.0.5'
2
+ VERSION = '0.1.0.rc1'
3
3
  end
@@ -6,8 +6,8 @@ Gem::Specification.new do |s|
6
6
  s.name = "merch_calendar"
7
7
  s.version = MerchCalendar::VERSION
8
8
  s.platform = Gem::Platform::RUBY
9
- s.authors = ['Mitch Dempsey']
10
- s.email = ['mitch@stitchfix.com', 'opensource@stitchfix.com']
9
+ s.authors = ['Mitch Dempsey', "Jon Worek"]
10
+ s.email = ['opensource@stitchfix.com']
11
11
  s.licenses = ['MIT']
12
12
  s.homepage = "https://github.com/stitchfix/merch_calendar"
13
13
  s.summary = "Utility for manipulating dates within a 4-5-4 retail calendar"
@@ -23,4 +23,5 @@ Gem::Specification.new do |s|
23
23
  s.add_development_dependency "simplecov"
24
24
  s.add_development_dependency "coveralls"
25
25
  s.add_development_dependency "pry"
26
+ s.add_development_dependency "pry-byebug"
26
27
  end
@@ -0,0 +1,133 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe MerchCalendar::FiscalYearCalendar do
4
+ describe "#weeks_in_year" do
5
+ it "returns 53 for a leap year - 2013" do
6
+ expect(subject.weeks_in_year(2013)).to eq 53
7
+ end
8
+
9
+ it "returns 52 for a normal year" do
10
+ expect(subject.weeks_in_year(2014)).to eq 52
11
+ end
12
+
13
+ it "returns 52 for a normal year" do
14
+ expect(subject.weeks_in_year(2015)).to eq 52
15
+ end
16
+
17
+ it "returns 52 for a normal year" do
18
+ expect(subject.weeks_in_year(2016)).to eq 52
19
+ end
20
+
21
+ it "returns 52 for a normal year" do
22
+ expect(subject.weeks_in_year(2017)).to eq 52
23
+ end
24
+
25
+ it "returns 53 for a leap year - 2018" do
26
+ expect(subject.weeks_in_year(2018)).to eq 53
27
+ end
28
+
29
+ it "returns 52 for a normal year" do
30
+ expect(subject.weeks_in_year(2019)).to eq 52
31
+ end
32
+ end
33
+
34
+ describe "#start_of_week" do
35
+ it "returns the correct date for 2017-1-1 (2017-Aug-wk1)" do
36
+ expect(subject.start_of_week(2017, 1, 1)).to eq Date.new(2016, 7, 31)
37
+ end
38
+
39
+ it "returns the correct Date for 2018-1-1 (2018-Aug-wk1)" do
40
+ expect(subject.start_of_week(2018, 1, 1)).to eq Date.new(2017, 7, 30)
41
+ end
42
+
43
+ it "returns the correct Date for 2019-1-1 (2019-Aug-wk1)" do
44
+ expect(subject.start_of_week(2019, 1, 1)).to eq Date.new(2018, 8, 5)
45
+ end
46
+ end
47
+
48
+ describe "#end_of_week" do
49
+ it "returns the correct date for 2017-6-1 (2017-Jan-wk1)" do
50
+ expect(subject.end_of_week(2017, 6, 1)).to eq Date.new(2017, 1, 7)
51
+ end
52
+
53
+ it "returns the correct Date for 2018-6-5 (2018-Jan-wk5)" do
54
+ expect(subject.end_of_week(2018, 6, 5)).to eq Date.new(2018, 2, 3)
55
+ end
56
+
57
+ it "returns the correct Date for 2019-10-3 (2019-May-wk3)" do
58
+ expect(subject.end_of_week(2019, 10, 3)).to eq Date.new(2019, 5, 25)
59
+ end
60
+ end
61
+
62
+ describe "#start_of_month" do
63
+ it "returns the correct date for 2018-1 AKA 2018-Aug" do
64
+ expect(subject.start_of_month(2018, 1)).to eq Date.new(2017, 7, 30)
65
+ end
66
+
67
+ it "returns the correct date for 2019-1 AKA 2019-Aug" do
68
+ expect(subject.start_of_month(2019, 1)).to eq Date.new(2018, 8, 5)
69
+ end
70
+ end
71
+
72
+ describe "#end_of_month" do
73
+ it "returns the correct date for 2018-1 AKA 2018-Aug" do
74
+ expect(subject.end_of_month(2018, 1)).to eq Date.new(2017, 8, 26)
75
+ end
76
+
77
+ it "returns the correct date for 2019-1 AKA 2019-Aug" do
78
+ expect(subject.end_of_month(2019, 1)).to eq Date.new(2018, 9, 1)
79
+ end
80
+ end
81
+
82
+ describe "#start_of_quarter" do
83
+ it "returns the correct date for 2018-Q1" do
84
+ expect(subject.start_of_quarter(2018, 1)).to eq Date.new(2017, 7, 30)
85
+ end
86
+
87
+ it "returns the correct date for 2018-Q4" do
88
+ expect(subject.start_of_quarter(2018, 4)).to eq Date.new(2018, 5, 6)
89
+ end
90
+
91
+ it "returns the correct date for 2019-Q1" do
92
+ expect(subject.start_of_quarter(2019, 1)).to eq Date.new(2018, 8, 5)
93
+ end
94
+ end
95
+
96
+ describe "#end_of_quarter" do
97
+ it "returns the correct date for 2018-Q1" do
98
+ expect(subject.end_of_quarter(2018, 1)).to eq Date.new(2017, 10, 28)
99
+ end
100
+
101
+ it "returns the correct date for 2018-Q4" do
102
+ expect(subject.end_of_quarter(2018, 4)).to eq Date.new(2018, 8, 4)
103
+ end
104
+
105
+ it "returns the correct date for 2019-Q1" do
106
+ expect(subject.end_of_quarter(2019, 1)).to eq Date.new(2018, 11, 3)
107
+ end
108
+ end
109
+
110
+ describe "#start_of_year" do
111
+ it "returns the correct date for 2018" do
112
+ expect(subject.start_of_year(2018)).to eq Date.new(2017, 7, 30)
113
+ end
114
+
115
+ it "returns the correct date for 2019" do
116
+ expect(subject.start_of_year(2019)).to eq Date.new(2018, 8, 5)
117
+ end
118
+ end
119
+
120
+ describe "#end_of_year" do
121
+ it "returns the correct date for 2017" do
122
+ expect(subject.end_of_year(2017)).to eq Date.new(2017, 7, 29)
123
+ end
124
+
125
+ it "returns the correct date for 2018" do
126
+ expect(subject.end_of_year(2018)).to eq Date.new(2018, 8, 4)
127
+ end
128
+
129
+ it "returns the correct date for 2019" do
130
+ expect(subject.end_of_year(2019)).to eq Date.new(2019, 8, 3)
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,119 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe MerchCalendar::RetailCalendar do
4
+ describe "#weeks_in_year" do
5
+ it "returns 53 for a leap year" do
6
+ expect(subject.weeks_in_year(2012)).to eq 53
7
+ expect(subject.weeks_in_year(2017)).to eq 53
8
+ end
9
+
10
+ it "returns 52 for a normal year" do
11
+ expect(subject.weeks_in_year(2013)).to eq 52
12
+ expect(subject.weeks_in_year(2018)).to eq 52
13
+ end
14
+ end
15
+
16
+ describe "#start_of_week" do
17
+ it "returns the correct date" do
18
+ expect(subject.start_of_week(2017, 1, 1)).to eq Date.new(2017, 1, 29)
19
+ expect(subject.start_of_week(2018, 1, 1)).to eq Date.new(2018, 2, 4)
20
+ end
21
+ end
22
+
23
+ describe "#end_of_week" do
24
+ it "returns the correct date" do
25
+ expect(subject.end_of_week(2017, 1, 1)).to eq Date.new(2017, 2, 4)
26
+ expect(subject.end_of_week(2018, 1, 1)).to eq Date.new(2018, 2, 10)
27
+ end
28
+
29
+ it "returns the correct date for 2017-12-w5" do
30
+ expect(subject.end_of_week(2017, 12, 5)).to eq Date.new(2018, 2, 3)
31
+ end
32
+ end
33
+
34
+ describe "#start_of_month" do
35
+ it "returns the correct date" do
36
+ expect(subject.start_of_month(2017, 1)).to eq Date.new(2017, 1, 29)
37
+ expect(subject.start_of_month(2018, 1)).to eq Date.new(2018, 2, 4)
38
+ end
39
+ end
40
+
41
+ describe "#start_of_quarter" do
42
+ it "returns the correct date" do
43
+ expect(subject.start_of_quarter(2017, 1)).to eq Date.new(2017, 1, 29)
44
+ expect(subject.start_of_quarter(2018, 1)).to eq Date.new(2018, 2, 4)
45
+ end
46
+ end
47
+
48
+ describe "#end_of_quarter" do
49
+ it "returns the correct date" do
50
+ expect(subject.end_of_quarter(2017, 1)).to eq Date.new(2017, 4, 29)
51
+ expect(subject.end_of_quarter(2018, 1)).to eq Date.new(2018, 5, 5)
52
+ end
53
+ end
54
+
55
+ describe "#start_of_year" do
56
+ it "returns the correct date" do
57
+ expect(subject.start_of_year(2017)).to eq Date.new(2017, 1, 29)
58
+ expect(subject.start_of_year(2018)).to eq Date.new(2018, 2, 4)
59
+ end
60
+ end
61
+
62
+ describe "#end_of_year" do
63
+ it "returns the correct date for 2016" do
64
+ expect(subject.end_of_year(2016)).to eq Date.new(2017, 1, 28)
65
+ end
66
+
67
+ it "returns the correct date for 2017 (a leap year)" do
68
+ expect(subject.end_of_year(2017)).to eq Date.new(2018, 2, 3)
69
+ end
70
+
71
+ it "returns the correct date for 2018" do
72
+ expect(subject.end_of_year(2018)).to eq Date.new(2019, 2, 2)
73
+ end
74
+
75
+ it "returns the correct date for 2019" do
76
+ expect(subject.end_of_year(2019)).to eq Date.new(2020, 2, 1)
77
+ end
78
+ end
79
+
80
+ describe "#merch_months_in" do
81
+ it "returns merch date for start_date if start_date is the same as end_date" do
82
+ start_date = Date.new(2014,8,1)
83
+ end_date = start_date
84
+ start_merch_date = MerchCalendar.start_of_month(start_date.year, merch_month: start_date.month)
85
+
86
+ merch_months = subject.merch_months_in(start_date, end_date)
87
+ expect(merch_months.count).to be(1)
88
+ expect(merch_months.first.year).to eq start_merch_date.year
89
+ expect(merch_months.first.month).to eq start_merch_date.month
90
+ expect(merch_months.first.day).to eq start_merch_date.day
91
+ end
92
+
93
+ it "returns valid merch dates for 2014" do
94
+ start_date = Date.new(2014, 1, 1)
95
+ end_date = Date.new(2014, 12, 1)
96
+
97
+ merch_months = subject.merch_months_in(start_date, end_date)
98
+ expect(merch_months.count).to be 11
99
+
100
+ merch_months.each do |merch_month|
101
+ expect(merch_month.year).to be 2014
102
+ end
103
+
104
+ expect(merch_months[0].strftime('%Y-%m-%d')).to eq '2014-02-02'
105
+ expect(merch_months[1].strftime('%Y-%m-%d')).to eq '2014-03-02'
106
+ expect(merch_months[2].strftime('%Y-%m-%d')).to eq '2014-04-06'
107
+ expect(merch_months[3].strftime('%Y-%m-%d')).to eq '2014-05-04'
108
+ expect(merch_months[4].strftime('%Y-%m-%d')).to eq '2014-06-01'
109
+ expect(merch_months[5].strftime('%Y-%m-%d')).to eq '2014-07-06'
110
+ expect(merch_months[6].strftime('%Y-%m-%d')).to eq '2014-08-03'
111
+ expect(merch_months[7].strftime('%Y-%m-%d')).to eq '2014-08-31'
112
+ expect(merch_months[8].strftime('%Y-%m-%d')).to eq '2014-10-05'
113
+ expect(merch_months[9].strftime('%Y-%m-%d')).to eq '2014-11-02'
114
+ expect(merch_months[10].strftime('%Y-%m-%d')).to eq '2014-11-30'
115
+ end
116
+ end
117
+
118
+ end
119
+
@@ -67,14 +67,14 @@ describe MerchCalendar::Util do
67
67
  end
68
68
 
69
69
  describe '#get_merch_month_param' do
70
- let(:date_calc) { MerchCalendar::DateCalculator.new }
70
+ let(:retail_calendar) { MerchCalendar::RetailCalendar.new }
71
71
  before(:each) do
72
- allow(MerchCalendar).to receive(:date_calc).and_return(date_calc)
72
+ allow(MerchCalendar).to receive(:retail_calendar).and_return(retail_calendar)
73
73
  end
74
74
 
75
75
  context "valid calls" do
76
76
  before(:each) do
77
- expect(date_calc).to receive(:start_of_month).with(2014, 1)
77
+ expect(retail_calendar).to receive(:start_of_month).with(2014, 1)
78
78
  end
79
79
  it "assumes an integer is a julian month" do
80
80
  MerchCalendar.start_of_month(2014, 2)
@@ -97,9 +97,9 @@ describe MerchCalendar::Util do
97
97
 
98
98
  context "errors" do
99
99
  before(:each) do
100
- expect(date_calc).to_not receive(:start_of_month)
100
+ expect(retail_calendar).to_not receive(:start_of_month)
101
101
  end
102
102
  it { expect { MerchCalendar.start_of_month(2014, :april) }.to raise_error(ArgumentError) }
103
103
  end
104
104
  end
105
- end
105
+ end
@@ -17,6 +17,37 @@ RSpec.describe MerchCalendar do
17
17
  end
18
18
  end
19
19
 
20
+ describe "#julian_to_merch" do
21
+ it { expect(subject.julian_to_merch(1)).to eq 12 }
22
+ it { expect(subject.julian_to_merch(2)).to eq 1 }
23
+ it { expect(subject.julian_to_merch(3)).to eq 2 }
24
+ it { expect(subject.julian_to_merch(4)).to eq 3 }
25
+ it { expect(subject.julian_to_merch(5)).to eq 4 }
26
+ it { expect(subject.julian_to_merch(6)).to eq 5 }
27
+ it { expect(subject.julian_to_merch(7)).to eq 6 }
28
+ it { expect(subject.julian_to_merch(8)).to eq 7 }
29
+ it { expect(subject.julian_to_merch(9)).to eq 8 }
30
+ it { expect(subject.julian_to_merch(10)).to eq 9 }
31
+ it { expect(subject.julian_to_merch(11)).to eq 10 }
32
+ it { expect(subject.julian_to_merch(12)).to eq 11 }
33
+ end
34
+
35
+ describe "#merch_to_julian" do
36
+ it { expect(subject.merch_to_julian(12)).to eq 1 }
37
+ it { expect(subject.merch_to_julian(1)).to eq 2 }
38
+ it { expect(subject.merch_to_julian(2)).to eq 3 }
39
+ it { expect(subject.merch_to_julian(3)).to eq 4 }
40
+ it { expect(subject.merch_to_julian(4)).to eq 5 }
41
+ it { expect(subject.merch_to_julian(5)).to eq 6 }
42
+ it { expect(subject.merch_to_julian(6)).to eq 7 }
43
+ it { expect(subject.merch_to_julian(7)).to eq 8 }
44
+ it { expect(subject.merch_to_julian(8)).to eq 9 }
45
+ it { expect(subject.merch_to_julian(9)).to eq 10 }
46
+ it { expect(subject.merch_to_julian(10)).to eq 11 }
47
+ it { expect(subject.merch_to_julian(11)).to eq 12 }
48
+ end
49
+
50
+
20
51
  describe "#start_of_week" do
21
52
  it "returns the correct date for a given merch week" do
22
53
  expect(described_class.start_of_week(2017, 4, 1)).to eq Date.new(2017, 4, 2)
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: merch_calendar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.1.0.rc1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mitch Dempsey
8
+ - Jon Worek
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2017-06-05 00:00:00.000000000 Z
12
+ date: 2017-06-30 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: rake
@@ -80,9 +81,22 @@ dependencies:
80
81
  - - ">="
81
82
  - !ruby/object:Gem::Version
82
83
  version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: pry-byebug
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
83
98
  description: Utility for manipulating dates within a 4-5-4 retail calendar
84
99
  email:
85
- - mitch@stitchfix.com
86
100
  - opensource@stitchfix.com
87
101
  executables: []
88
102
  extensions: []
@@ -103,15 +117,17 @@ files:
103
117
  - lib/merch_calendar.rb
104
118
  - lib/merch_calendar/configurable.rb
105
119
  - lib/merch_calendar/configuration.rb
106
- - lib/merch_calendar/date_calculator.rb
120
+ - lib/merch_calendar/fiscal_year_calendar.rb
107
121
  - lib/merch_calendar/merch_week.rb
122
+ - lib/merch_calendar/retail_calendar.rb
108
123
  - lib/merch_calendar/util.rb
109
124
  - lib/merch_calendar/version.rb
110
125
  - merch_calendar.gemspec
111
126
  - spec/merch_calendar/configurable_spec.rb
112
127
  - spec/merch_calendar/configuration_spec.rb
113
- - spec/merch_calendar/date_calculator_spec.rb
128
+ - spec/merch_calendar/fiscal_year_calendar_spec.rb
114
129
  - spec/merch_calendar/merch_week_spec.rb
130
+ - spec/merch_calendar/retail_calendar_spec.rb
115
131
  - spec/merch_calendar/util_spec.rb
116
132
  - spec/merch_calendar_spec.rb
117
133
  - spec/spec_helper.rb
@@ -130,9 +146,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
130
146
  version: '0'
131
147
  required_rubygems_version: !ruby/object:Gem::Requirement
132
148
  requirements:
133
- - - ">="
149
+ - - ">"
134
150
  - !ruby/object:Gem::Version
135
- version: '0'
151
+ version: 1.3.1
136
152
  requirements: []
137
153
  rubyforge_project:
138
154
  rubygems_version: 2.6.12
@@ -142,8 +158,9 @@ summary: Utility for manipulating dates within a 4-5-4 retail calendar
142
158
  test_files:
143
159
  - spec/merch_calendar/configurable_spec.rb
144
160
  - spec/merch_calendar/configuration_spec.rb
145
- - spec/merch_calendar/date_calculator_spec.rb
161
+ - spec/merch_calendar/fiscal_year_calendar_spec.rb
146
162
  - spec/merch_calendar/merch_week_spec.rb
163
+ - spec/merch_calendar/retail_calendar_spec.rb
147
164
  - spec/merch_calendar/util_spec.rb
148
165
  - spec/merch_calendar_spec.rb
149
166
  - spec/spec_helper.rb
@@ -1,90 +0,0 @@
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
- describe "#merch_months_in" do
52
- it "returns merch date for start_date if start_date is the same as end_date" do
53
- start_date = Date.new(2014,8,1)
54
- end_date = start_date
55
- start_merch_date = MerchCalendar.start_of_month(start_date.year, merch_month: start_date.month)
56
-
57
- merch_months = subject.merch_months_in(start_date, end_date)
58
- expect(merch_months.count).to be(1)
59
- expect(merch_months.first.year).to eq start_merch_date.year
60
- expect(merch_months.first.month).to eq start_merch_date.month
61
- expect(merch_months.first.day).to eq start_merch_date.day
62
- end
63
-
64
- it "returns valid merch dates for 2014" do
65
- start_date = Date.new(2014, 1, 1)
66
- end_date = Date.new(2014, 12, 1)
67
-
68
- merch_months = subject.merch_months_in(start_date, end_date)
69
- expect(merch_months.count).to be 11
70
-
71
- merch_months.each do |merch_month|
72
- expect(merch_month.year).to be 2014
73
- end
74
-
75
- expect(merch_months[0].strftime('%Y-%m-%d')).to eq '2014-02-02'
76
- expect(merch_months[1].strftime('%Y-%m-%d')).to eq '2014-03-02'
77
- expect(merch_months[2].strftime('%Y-%m-%d')).to eq '2014-04-06'
78
- expect(merch_months[3].strftime('%Y-%m-%d')).to eq '2014-05-04'
79
- expect(merch_months[4].strftime('%Y-%m-%d')).to eq '2014-06-01'
80
- expect(merch_months[5].strftime('%Y-%m-%d')).to eq '2014-07-06'
81
- expect(merch_months[6].strftime('%Y-%m-%d')).to eq '2014-08-03'
82
- expect(merch_months[7].strftime('%Y-%m-%d')).to eq '2014-08-31'
83
- expect(merch_months[8].strftime('%Y-%m-%d')).to eq '2014-10-05'
84
- expect(merch_months[9].strftime('%Y-%m-%d')).to eq '2014-11-02'
85
- expect(merch_months[10].strftime('%Y-%m-%d')).to eq '2014-11-30'
86
- end
87
- end
88
-
89
- end
90
-