flexyear 2.1.1 → 2.4.0

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
- SHA1:
3
- metadata.gz: 929b57a6f2f71b83adb9d436b5304e88802ccec1
4
- data.tar.gz: d4936577ab3e36019669b9c9222fc8611a757cbd
2
+ SHA256:
3
+ metadata.gz: 76222dec0e41ccbd4cfae4dd0d6ce3372790ae0ff9f1a0454b7003b663d03f1a
4
+ data.tar.gz: 9ae27ce940ba0e5b8e5258c7e4ccc2b405913b077b2193eef32ff7f5ca8f1702
5
5
  SHA512:
6
- metadata.gz: f20398d5815ac685692558a248e2f1eab79735186d34b8ea7fc9668287a73b152e972fcbff1be08dd0b129a1deb870adb8a812b001ae0dda6553239d10c44443
7
- data.tar.gz: 2e746c4ee7fbb2b48d84a022674da74e78e5690a175e64ef93becb052e1ebe694ae5c07e838ae9d8ccc6a25420faac887e043069afe413e58455f970af7caed5
6
+ metadata.gz: b375a2c188636a09b4598e0141649112d4845485b3c618061d86cdc16a099ccbf5aee9c5b51b84b5a9a5d8b58cffa52219b8cedd15fa8ea26aac8d610f051fb3
7
+ data.tar.gz: 1264dbafb0d737569bde093983260a23bb1d0f8468ad09ecf1d4d3b6a31a9c6eb437717cebe49e6810511052682023daede11376f28ee306b81e54aa989ed88a
data/README.md CHANGED
@@ -7,15 +7,23 @@ Examples:
7
7
  ```ruby
8
8
  FlexYear.new("1980s").year_low == 1980
9
9
  FlexYear.new("1980s").year_high == 1989
10
+ FlexYear.new("1980s").decade == 1980s
10
11
 
11
12
  FlexYear.new("mid-80s").year_low == 1983
12
13
  FlexYear.new("mid-80s").year_high == 1986
14
+ FlexYear.new("mid-80s").decade == 1980s
13
15
 
14
16
  FlexYear.new(1983).year_low == 1983
15
17
  FlexYear.new(1983).year_high == 1983
18
+ FlexYear.new(1983).decade == 1980s
19
+
20
+ FlexYear.new(198*).year_low == 1980
21
+ FlexYear.new(198*).year_high == 1989
22
+ FlexYear.new(198*).decade == 1980s
16
23
 
17
24
  FlexYear.new(["1980s", "1988 - 2000", 2001]).year_low == 1980
18
25
  FlexYear.new(["1980s", "1988 - 2000", 2001]).year_high == 2001
26
+ FlexYear.new(["1980s", "1988 - 2000", nil, 2001]).decades == [1980s, nil, nil, 2000s]
19
27
  ```
20
28
 
21
29
  It's pretty flexible in the kinds of things it takes. For more examples, see the spec.
@@ -48,3 +56,11 @@ Or install it yourself as:
48
56
  ### Test
49
57
 
50
58
  `rake`
59
+
60
+ ### Publish a new version
61
+
62
+ We use bundlers rake commands to release versions. To cut and release a new version:
63
+
64
+ 1. Update lib/flexyear/version.rb and increment to your new desired version
65
+ 2. Commit and push your version bump
66
+ 3. Run `bundle exec rake release` to package and publish that version
@@ -1,3 +1,3 @@
1
1
  class FlexYear
2
- VERSION = "2.1.1"
2
+ VERSION = "2.4.0"
3
3
  end
data/lib/flexyear.rb CHANGED
@@ -23,7 +23,7 @@ require 'date'
23
23
  class FlexYear
24
24
  class InvalidYearError < ArgumentError; end
25
25
 
26
- attr_reader :year_low, :year_high
26
+ attr_reader :year_low, :year_high, :decade, :decades
27
27
 
28
28
  def initialize(year_input)
29
29
  @year_input = year_input
@@ -39,6 +39,26 @@ class FlexYear
39
39
  @year_input
40
40
  end
41
41
 
42
+ def decade?
43
+ return false unless year_low && year_high
44
+ year_low % 10 == 0 && year_high % 10 == 9
45
+ end
46
+
47
+ def parse_decade(yr_low, yr_high)
48
+ return if @year_input.is_a?(Array)
49
+ return unless yr_low && yr_high
50
+
51
+ same_decade = yr_low.to_s[0,3] == yr_high.to_s[0, 3]
52
+ @decade = "#{yr_low.to_s[0,3]}0s" if same_decade
53
+ end
54
+
55
+ def parse_decades(years)
56
+ return unless @year_input.is_a?(Array)
57
+
58
+ parsed_decades = years.flat_map { |y| self.class.new(y).decade }
59
+ @decades = parsed_decades
60
+ end
61
+
42
62
  private
43
63
 
44
64
  def parse_year_list(years)
@@ -47,14 +67,17 @@ class FlexYear
47
67
  [year.year_low, year.year_high]
48
68
  end
49
69
 
50
- flat_years = all_years.compact.uniq
70
+ flat_years = all_years.uniq.map { |y| y.nil? ? Date.today.year : y }
51
71
 
52
72
  @year_low = flat_years.min
53
73
  @year_high = flat_years.max
74
+
75
+ parse_decades(years)
54
76
  end
55
77
 
56
78
  def parse_year_string(year_string)
57
79
  parse_year(year_string.to_s.strip)
80
+ parse_decade(@year_low, @year_high)
58
81
  end
59
82
 
60
83
  def centuryize(year, base_year=nil)
@@ -87,6 +110,9 @@ class FlexYear
87
110
  if year_string =~ range_regex && $1 && $2
88
111
  @year_low = centuryize($1).to_i
89
112
  @year_low, @year_high = [@year_low, centuryize($2, @year_low).to_i].sort
113
+ elsif year_string =~ open_ended_range_regex
114
+ @year_low = centuryize($1).to_i
115
+ @year_high = Date.today.year
90
116
  else
91
117
  if year_string =~ decade_regex
92
118
  @base_year = centuryize($1).to_i
@@ -111,6 +137,10 @@ class FlexYear
111
137
  /(\d+)\s*-\s*(\d+)/
112
138
  end
113
139
 
140
+ def open_ended_range_regex
141
+ /(\d+)\s*-\s*(\D+)/
142
+ end
143
+
114
144
  def asterisk_regex
115
145
  /(\d+).*\*$/
116
146
  end
@@ -12,72 +12,108 @@ describe FlexYear do
12
12
  subject { flexyear_class.new("") }
13
13
  its(:year_low) { should be_nil }
14
14
  its(:year_high) { should be_nil }
15
+ its(:decade?) { should eq(false) }
16
+ its(:decade) { should be_nil }
17
+ its(:decades) { should be_nil }
15
18
  end
16
19
 
17
20
  context "given nil" do
18
21
  subject { flexyear_class.new(nil) }
19
22
  its(:year_low) { should be_nil }
20
23
  its(:year_high) { should be_nil }
24
+ its(:decade?) { should eq(false) }
25
+ its(:decade) { should be_nil }
26
+ its(:decades) { should be_nil }
21
27
  end
22
28
 
23
29
  context "text" do
24
30
  subject { flexyear_class.new("something") }
25
31
  its(:year_low) { should be_nil }
26
32
  its(:year_high) { should be_nil }
33
+ its(:decade?) { should eq(false) }
34
+ its(:decade) { should be_nil }
35
+ its(:decades) { should be_nil }
27
36
  end
28
37
 
29
38
  context "given 1979 as number" do
30
39
  subject { flexyear_class.new(1979) }
31
40
  its(:year_low) { should eq(1979) }
32
41
  its(:year_high) { should eq(1979) }
42
+ its(:decade?) { should eq(false) }
43
+ its(:decade) { should eq("1970s") }
44
+ its(:decades) { should be_nil }
33
45
  end
34
46
 
35
47
  context "given 1979" do
36
48
  subject { flexyear_class.new("1979") }
37
49
  its(:year_low) { should eq(1979) }
38
50
  its(:year_high) { should eq(1979) }
51
+ its(:decade?) { should eq(false) }
52
+ its(:decade) { should eq("1970s") }
53
+ its(:decades) { should be_nil }
39
54
  end
40
55
 
41
56
  context "given 1979 (with a space)" do
42
57
  subject { flexyear_class.new("1979 ") }
43
58
  its(:year_low) { should eq(1979) }
44
59
  its(:year_high) { should eq(1979) }
60
+ its(:decade?) { should eq(false) }
61
+ its(:decade) { should eq("1970s") }
62
+ its(:decades) { should be_nil }
45
63
  end
46
64
 
47
65
  context 'given 197*' do
48
66
  subject { flexyear_class.new('197*') }
49
67
  its(:year_low) { should eq(1970) }
50
68
  its(:year_high) { should eq(1979) }
69
+ its(:decade?) { should eq(true) }
70
+ its(:decade) { should eq("1970s") }
71
+ its(:decades) { should be_nil }
51
72
  end
52
73
 
53
74
  context "given 1970s" do
54
75
  subject { flexyear_class.new("1970s") }
55
76
  its(:year_low) { should eq(1970) }
56
77
  its(:year_high) { should eq(1979) }
78
+ its(:decade?) { should eq(true) }
79
+ its(:decade) { should eq("1970s") }
80
+ its(:decades) { should be_nil }
57
81
  end
58
82
 
59
83
  context "given 70s" do
60
84
  subject { flexyear_class.new("70s") }
61
85
  its(:year_low) { should eq(1970) }
62
86
  its(:year_high) { should eq(1979) }
87
+ its(:decade?) { should eq(true) }
88
+ its(:decade) { should eq("1970s") }
89
+ its(:decades) { should be_nil }
63
90
  end
64
91
 
65
92
  context "given something with negative and dots" do
66
93
  subject { flexyear_class.new("approx.-2006") }
67
94
  its(:year_low) { should eq(2005) }
68
95
  its(:year_high) { should eq(2007) }
96
+ its(:decade?) { should eq(false) }
97
+ its(:decade) { should eq("2000s") }
98
+ its(:decades) { should be_nil }
69
99
  end
70
100
 
71
101
  context 'given before 1973' do
72
102
  subject { FlexYear.new('before 1973') }
73
103
  its(:year_low) { should be_nil }
74
104
  its(:year_high) { should eq(1973) }
105
+ its(:decade?) { should eq(false) }
106
+ its(:decade) { should be_nil }
107
+ its(:decades) { should be_nil }
75
108
  end
76
109
 
77
110
  context 'given after 1973' do
78
111
  subject { FlexYear.new('after 1973') }
79
112
  its(:year_low) { should eq(1973) }
80
113
  its(:year_high) { should be_nil }
114
+ its(:decade?) { should eq(false) }
115
+ its(:decade) { should be_nil }
116
+ its(:decades) { should be_nil }
81
117
  end
82
118
 
83
119
  ["mid 1970s", "mid 70s", "mid-70s", "mid-70's"].each do |year|
@@ -85,7 +121,10 @@ describe FlexYear do
85
121
  subject { flexyear_class.new(year) }
86
122
  its(:year_low) { should eq(1973) }
87
123
  its(:year_high) { should eq(1976) }
124
+ its(:decade) { should eq("1970s") }
125
+ its(:decades) { should be_nil }
88
126
  its(:to_s) { should eq(year) }
127
+ its(:decade?) { should eq(false) }
89
128
  end
90
129
  end
91
130
 
@@ -94,6 +133,9 @@ describe FlexYear do
94
133
  subject { flexyear_class.new(year) }
95
134
  its(:year_low) { should eq(1970) }
96
135
  its(:year_high) { should eq(1973) }
136
+ its(:decade?) { should eq(false) }
137
+ its(:decade) { should eq("1970s") }
138
+ its(:decades) { should be_nil }
97
139
  end
98
140
  end
99
141
 
@@ -102,6 +144,9 @@ describe FlexYear do
102
144
  subject { flexyear_class.new(year) }
103
145
  its(:year_low) { should eq(1976) }
104
146
  its(:year_high) { should eq(1979) }
147
+ its(:decade?) { should eq(false) }
148
+ its(:decade) { should eq("1970s") }
149
+ its(:decades) { should be_nil }
105
150
  end
106
151
  end
107
152
 
@@ -110,6 +155,9 @@ describe FlexYear do
110
155
  subject { flexyear_class.new(range) }
111
156
  its(:year_low) { should eq(1973) }
112
157
  its(:year_high) { should eq(1975) }
158
+ its(:decade?) { should eq(false) }
159
+ its(:decade) { should eq("1970s") }
160
+ its(:decades) { should be_nil }
113
161
  end
114
162
  end
115
163
 
@@ -117,6 +165,27 @@ describe FlexYear do
117
165
  subject { flexyear_class.new('1975-1973') }
118
166
  its(:year_low) { should eq(1973) }
119
167
  its(:year_high) { should eq(1975) }
168
+ its(:decade?) { should eq(false) }
169
+ its(:decade) { should eq("1970s") }
170
+ its(:decades) { should be_nil }
171
+ end
172
+
173
+ context "given a range from a year to present" do
174
+ subject { flexyear_class.new('1975-Present') }
175
+ its(:year_low) { should eq(1975) }
176
+ its(:year_high) { should eq(2016) }
177
+ its(:decade?) { should eq(false) }
178
+ its(:decade) { should be_nil }
179
+ its(:decades) { should be_nil }
180
+ end
181
+
182
+ context "given a range in difference decades" do
183
+ subject { flexyear_class.new('1975-1983') }
184
+ its(:year_low) { should eq(1975) }
185
+ its(:year_high) { should eq(1983) }
186
+ its(:decade?) { should eq(false) }
187
+ its(:decade) { should be_nil}
188
+ its(:decades) { should be_nil }
120
189
  end
121
190
 
122
191
  context "given a range" do
@@ -124,6 +193,9 @@ describe FlexYear do
124
193
  subject { flexyear_class.new(range) }
125
194
  its(:year_low) { should eq(2003) }
126
195
  its(:year_high) { should eq(2004) }
196
+ its(:decade?) { should eq(false) }
197
+ its(:decade) { should eq("2000s") }
198
+ its(:decades) { should be_nil }
127
199
  end
128
200
  end
129
201
 
@@ -132,72 +204,108 @@ describe FlexYear do
132
204
  subject { flexyear_class.new('1973 (Circa)') }
133
205
  its(:year_low) { should eq(1972) }
134
206
  its(:year_high) { should eq(1974) }
207
+ its(:decade?) { should eq(false) }
208
+ its(:decade) { should eq("1970s") }
209
+ its(:decades) { should be_nil }
135
210
  end
136
211
 
137
212
  context 'at the beginning of the string' do
138
213
  subject { flexyear_class.new('Circa 1973') }
139
214
  its(:year_low) { should eq(1972) }
140
215
  its(:year_high) { should eq(1974) }
216
+ its(:decade?) { should eq(false) }
217
+ its(:decade) { should eq("1970s") }
218
+ its(:decades) { should be_nil }
141
219
  end
142
220
 
143
221
  context 'abbreviated' do
144
222
  subject { flexyear_class.new('ca 1973') }
145
223
  its(:year_low) { should eq(1972) }
146
224
  its(:year_high) { should eq(1974) }
225
+ its(:decade?) { should eq(false) }
226
+ its(:decade) { should eq("1970s") }
227
+ its(:decades) { should be_nil }
147
228
  end
148
229
 
149
230
  context 'with dots' do
150
231
  subject { flexyear_class.new('c.a. 1973') }
151
232
  its(:year_low) { should eq(1972) }
152
233
  its(:year_high) { should eq(1974) }
234
+ its(:decade?) { should eq(false) }
235
+ its(:decade) { should eq("1970s") }
236
+ its(:decades) { should be_nil }
153
237
  end
154
238
 
155
239
  context 'with c.' do
156
240
  subject { flexyear_class.new('c. 1973') }
157
241
  its(:year_low) { should eq(1972) }
158
242
  its(:year_high) { should eq(1974) }
243
+ its(:decade?) { should eq(false) }
244
+ its(:decade) { should eq("1970s") }
245
+ its(:decades) { should be_nil }
159
246
  end
160
247
 
161
248
  context 'with ca.' do
162
249
  subject { flexyear_class.new('ca. 1973') }
163
250
  its(:year_low) { should eq(1972) }
164
251
  its(:year_high) { should eq(1974) }
252
+ its(:decade?) { should eq(false) }
253
+ its(:decade) { should eq("1970s") }
254
+ its(:decades) { should be_nil }
165
255
  end
166
256
 
167
257
  context 'with approx.' do
168
258
  subject { flexyear_class.new('approx. 1973') }
169
259
  its(:year_low) { should eq(1972) }
170
260
  its(:year_high) { should eq(1974) }
261
+ its(:decade?) { should eq(false) }
262
+ its(:decade) { should eq("1970s") }
263
+ its(:decades) { should be_nil }
171
264
  end
172
265
 
173
266
  context 'with appxly.' do
174
267
  subject { flexyear_class.new('appxly 1973') }
175
268
  its(:year_low) { should eq(1972) }
176
269
  its(:year_high) { should eq(1974) }
270
+ its(:decade?) { should eq(false) }
271
+ its(:decade) { should eq("1970s") }
272
+ its(:decades) { should be_nil }
177
273
  end
178
274
 
179
275
  context 'with around' do
180
276
  subject { flexyear_class.new('around 1973') }
181
277
  its(:year_low) { should eq(1972) }
182
278
  its(:year_high) { should eq(1974) }
279
+ its(:decade?) { should eq(false) }
280
+ its(:decade) { should eq("1970s") }
281
+ its(:decades) { should be_nil }
183
282
  end
184
283
 
185
284
  context 'with about' do
186
285
  subject { flexyear_class.new('about 1973') }
187
286
  its(:year_low) { should eq(1972) }
188
287
  its(:year_high) { should eq(1974) }
288
+ its(:decade?) { should eq(false) }
289
+ its(:decade) { should eq("1970s") }
290
+ its(:decades) { should be_nil }
189
291
  end
190
292
 
191
293
  context 'with circ' do
192
294
  subject { flexyear_class.new('circ. 1973') }
193
295
  its(:year_low) { should eq(1972) }
194
296
  its(:year_high) { should eq(1974) }
297
+ its(:decade?) { should eq(false) }
298
+ its(:decade) { should eq("1970s") }
299
+ its(:decades) { should be_nil }
195
300
  end
196
301
 
197
302
  context 'with cca' do
198
303
  subject { flexyear_class.new('cca 1973') }
199
304
  its(:year_low) { should eq(1972) }
200
305
  its(:year_high) { should eq(1974) }
306
+ its(:decade?) { should eq(false) }
307
+ its(:decade) { should eq("1970s") }
308
+ its(:decades) { should be_nil }
201
309
  end
202
310
 
203
311
  context 'given 2 character fields' do
@@ -205,33 +313,57 @@ describe FlexYear do
205
313
  subject { flexyear_class.new("73") }
206
314
  its(:year_low) { should eq(1973) }
207
315
  its(:year_high) { should eq(1973) }
316
+ its(:decade?) { should eq(false) }
317
+ its(:decade) { should eq("1970s") }
318
+ its(:decades) { should be_nil }
208
319
  end
209
320
 
210
321
  context "from current century" do
211
322
  subject { flexyear_class.new("06") }
212
323
  its(:year_low) { should eq(2006) }
213
324
  its(:year_high) { should eq(2006) }
325
+ its(:decade?) { should eq(false) }
326
+ its(:decade) { should eq("2000s") }
327
+ its(:decades) { should be_nil }
214
328
  end
215
329
  end
216
330
  end
217
331
 
218
332
  context "given a list" do
219
333
  context "mixed years" do
220
- subject { flexyear_class.new(["1980s", "mid-80s", "1988 - 1999", 2001,]) }
334
+ subject { flexyear_class.new(["1980s", "mid-80s", "1988 - 1999", 2001, "199*"]) }
221
335
  its(:year_low) { should eq(1980) }
222
336
  its(:year_high) { should eq(2001) }
337
+ its(:decade?) { should eq(false) }
338
+ its(:decade) { should be_nil }
339
+ its(:decades) { should eq(["1980s", "1980s", nil, "2000s", "1990s"])}
223
340
  end
224
341
 
225
342
  context "same years" do
226
343
  subject { flexyear_class.new(["1988", "1988"]) }
227
344
  its(:year_low) { should eq(1988) }
228
345
  its(:year_high) { should eq(1988) }
346
+ its(:decade?) { should eq(false) }
347
+ its(:decade) { should be_nil }
348
+ its(:decades) { should eq(["1980s", "1980s"])}
229
349
  end
230
350
 
231
351
  context "mixed years with nil" do
232
352
  subject { flexyear_class.new(["1988", "1990s", nil]) }
233
353
  its(:year_low) { should eq(1988) }
234
- its(:year_high) { should eq(1999) }
354
+ its(:year_high) { should eq(2016) }
355
+ its(:decade?) { should eq(false) }
356
+ its(:decade) { should be_nil }
357
+ its(:decades) { should eq(["1980s", "1990s", nil])}
358
+ end
359
+
360
+ context "with a year with an undefined end date" do
361
+ subject { flexyear_class.new(["1980s", "mid-80s", "1988 - 1999", 2001, "199*", "1999 - Present"]) }
362
+ its(:year_low) { should eq(1980) }
363
+ its(:year_high) { should eq(2016) }
364
+ its(:decade?) { should eq(false) }
365
+ its(:decade) { should be_nil }
366
+ its(:decades) { should eq(["1980s", "1980s", nil, "2000s", "1990s", nil])}
235
367
  end
236
368
  end
237
369
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flexyear
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 2.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Melnick & Yan Pritzker
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-21 00:00:00.000000000 Z
11
+ date: 2021-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -100,7 +100,7 @@ homepage: http://github.com/reverbdotcom/flexyear
100
100
  licenses:
101
101
  - MIT
102
102
  metadata: {}
103
- post_install_message:
103
+ post_install_message:
104
104
  rdoc_options: []
105
105
  require_paths:
106
106
  - lib
@@ -115,9 +115,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
115
  - !ruby/object:Gem::Version
116
116
  version: '0'
117
117
  requirements: []
118
- rubyforge_project:
119
- rubygems_version: 2.4.5
120
- signing_key:
118
+ rubygems_version: 3.1.6
119
+ signing_key:
121
120
  specification_version: 4
122
121
  summary: Natural language year range parser
123
122
  test_files: