flexyear 2.0.1 → 2.1.1

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: be16123e3ada3bc97daf20dfa0878e0ec64fb3d5
4
- data.tar.gz: cdd73aa8f992531f09d9f329a76af8ec850bde19
3
+ metadata.gz: 929b57a6f2f71b83adb9d436b5304e88802ccec1
4
+ data.tar.gz: d4936577ab3e36019669b9c9222fc8611a757cbd
5
5
  SHA512:
6
- metadata.gz: 6cebdc541a73b357bf70257e1c6127d88f1cc3c18253f70d5ca066df10485f0e4bb6f59806c337dc58044b910ca87b33a78724d854dc6873ac691fa52de1ff88
7
- data.tar.gz: 0b96b4c50820649422dabf3f465d46ca4515ff937f4e99289413c6c761b869152a9d30ccb14d847a023306e18599c3cd99a415972c1d9d07d8b91dc05bd15818
6
+ metadata.gz: f20398d5815ac685692558a248e2f1eab79735186d34b8ea7fc9668287a73b152e972fcbff1be08dd0b129a1deb870adb8a812b001ae0dda6553239d10c44443
7
+ data.tar.gz: 2e746c4ee7fbb2b48d84a022674da74e78e5690a175e64ef93becb052e1ebe694ae5c07e838ae9d8ccc6a25420faac887e043069afe413e58455f970af7caed5
data/README.md CHANGED
@@ -10,6 +10,12 @@ FlexYear.new("1980s").year_high == 1989
10
10
 
11
11
  FlexYear.new("mid-80s").year_low == 1983
12
12
  FlexYear.new("mid-80s").year_high == 1986
13
+
14
+ FlexYear.new(1983).year_low == 1983
15
+ FlexYear.new(1983).year_high == 1983
16
+
17
+ FlexYear.new(["1980s", "1988 - 2000", 2001]).year_low == 1980
18
+ FlexYear.new(["1980s", "1988 - 2000", 2001]).year_high == 2001
13
19
  ```
14
20
 
15
21
  It's pretty flexible in the kinds of things it takes. For more examples, see the spec.
@@ -30,6 +36,7 @@ Or install it yourself as:
30
36
 
31
37
  $ gem install flexyear
32
38
 
39
+
33
40
  ## Contributing
34
41
 
35
42
  1. Fork it
@@ -37,3 +44,7 @@ Or install it yourself as:
37
44
  3. Commit your changes (`git commit -am 'Add some feature'`)
38
45
  4. Push to the branch (`git push origin my-new-feature`)
39
46
  5. Create new Pull Request
47
+
48
+ ### Test
49
+
50
+ `rake`
@@ -1,3 +1,3 @@
1
1
  class FlexYear
2
- VERSION = "2.0.1"
2
+ VERSION = "2.1.1"
3
3
  end
data/lib/flexyear.rb CHANGED
@@ -25,20 +25,38 @@ class FlexYear
25
25
 
26
26
  attr_reader :year_low, :year_high
27
27
 
28
- def initialize(year_string)
29
- @year_string = year_string.to_s.strip
28
+ def initialize(year_input)
29
+ @year_input = year_input
30
30
 
31
- @low, @high = RangeParser.parse(@year_string)
32
-
33
- parse_year
31
+ if year_input.is_a?(Array)
32
+ parse_year_list(year_input)
33
+ else
34
+ parse_year_string(year_input)
35
+ end
34
36
  end
35
37
 
36
38
  def to_s
37
- @year_string
39
+ @year_input
38
40
  end
39
41
 
40
42
  private
41
43
 
44
+ def parse_year_list(years)
45
+ all_years = years.flat_map do |y|
46
+ year = self.class.new(y)
47
+ [year.year_low, year.year_high]
48
+ end
49
+
50
+ flat_years = all_years.compact.uniq
51
+
52
+ @year_low = flat_years.min
53
+ @year_high = flat_years.max
54
+ end
55
+
56
+ def parse_year_string(year_string)
57
+ parse_year(year_string.to_s.strip)
58
+ end
59
+
42
60
  def centuryize(year, base_year=nil)
43
61
  base = default_base_year(year)
44
62
  if base_year
@@ -63,27 +81,29 @@ class FlexYear
63
81
  end
64
82
  end
65
83
 
66
- def parse_year
67
- if @year_string =~ range_regex && $1 && $2
84
+ def parse_year(year_string)
85
+ low, high = RangeParser.parse(year_string)
86
+
87
+ if year_string =~ range_regex && $1 && $2
68
88
  @year_low = centuryize($1).to_i
69
89
  @year_low, @year_high = [@year_low, centuryize($2, @year_low).to_i].sort
70
90
  else
71
- if @year_string =~ decade_regex
91
+ if year_string =~ decade_regex
72
92
  @base_year = centuryize($1).to_i
73
- elsif @year_string =~ asterisk_regex
93
+ elsif year_string =~ asterisk_regex
74
94
  @base_year = centuryize($1).to_i * 10
75
- elsif @year_string =~ starts_with_word_regex
95
+ elsif year_string =~ starts_with_word_regex
76
96
  @base_year = centuryize($1).to_i
77
97
  else
78
- @base_year = centuryize(@year_string.gsub(/\D+/,'')).to_i
98
+ @base_year = centuryize(year_string.gsub(/\D+/,'')).to_i
79
99
  end
80
100
 
81
101
  if @base_year > 9999
82
102
  raise InvalidYearError, "Please use a four digit year."
83
103
  end
84
104
 
85
- @year_low = @base_year + @low unless @low.nil?
86
- @year_high = @base_year + @high unless @high.nil?
105
+ @year_low = @base_year + low unless low.nil?
106
+ @year_high = @base_year + high unless high.nil?
87
107
  end
88
108
  end
89
109
 
@@ -107,11 +127,11 @@ class FlexYear
107
127
  class Historical < FlexYear
108
128
  private
109
129
 
110
- def parse_year
111
- super
130
+ def parse_year(year_string)
131
+ super(year_string)
112
132
 
113
133
  if (!@year_low.nil? && @year_low > DateTime.now.year) || (!@year_high.nil? && @year_high > DateTime.now.year)
114
- raise InvalidYearError, "The year must be in the past. You specified #{@year_string}; Today is #{DateTime.now.year}"
134
+ raise InvalidYearError, "The year must be in the past. You specified #{year_string}; Today is #{DateTime.now.year}"
115
135
  end
116
136
  end
117
137
  end
@@ -215,11 +215,32 @@ describe FlexYear do
215
215
  end
216
216
  end
217
217
 
218
+ context "given a list" do
219
+ context "mixed years" do
220
+ subject { flexyear_class.new(["1980s", "mid-80s", "1988 - 1999", 2001,]) }
221
+ its(:year_low) { should eq(1980) }
222
+ its(:year_high) { should eq(2001) }
223
+ end
224
+
225
+ context "same years" do
226
+ subject { flexyear_class.new(["1988", "1988"]) }
227
+ its(:year_low) { should eq(1988) }
228
+ its(:year_high) { should eq(1988) }
229
+ end
230
+
231
+ context "mixed years with nil" do
232
+ subject { flexyear_class.new(["1988", "1990s", nil]) }
233
+ its(:year_low) { should eq(1988) }
234
+ its(:year_high) { should eq(1999) }
235
+ end
236
+ end
237
+
218
238
  context "given 12345 (five digit year)" do
219
239
  specify do
220
240
  expect { flexyear_class.new('12345') }.to raise_error(FlexYear::InvalidYearError)
221
241
  end
222
242
  end
243
+
223
244
  end
224
245
 
225
246
  describe FlexYear::Historical do
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.0.1
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Melnick & Yan Pritzker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-24 00:00:00.000000000 Z
11
+ date: 2019-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -75,8 +75,6 @@ extensions: []
75
75
  extra_rdoc_files: []
76
76
  files:
77
77
  - ".gitignore"
78
- - ".ruby-gemset"
79
- - ".ruby-version"
80
78
  - ".travis.yml"
81
79
  - CHANGELOG
82
80
  - Gemfile
@@ -118,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
116
  version: '0'
119
117
  requirements: []
120
118
  rubyforge_project:
121
- rubygems_version: 2.2.2
119
+ rubygems_version: 2.4.5
122
120
  signing_key:
123
121
  specification_version: 4
124
122
  summary: Natural language year range parser
data/.ruby-gemset DELETED
@@ -1 +0,0 @@
1
- flexyear
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- ruby-2.1.1