quando 0.0.6 → 0.0.8

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
  SHA256:
3
- metadata.gz: 154079261b51a488a0d8683470d1f956e5dbab5dc6bc6170ef3c9e53f30c316e
4
- data.tar.gz: 40b7f21bdce1e2fd3e037ead3f843d495517839a41f4ea8fe90d6545f12129c6
3
+ metadata.gz: dc9ef7c527d685d3da9942e1d53aff6017c012a7a25fc3e1fd47f667c2f94167
4
+ data.tar.gz: a1794e5af7f816d5b8edeb36bd8a762073f48afbcbc353a304d68bfd9c2a48e3
5
5
  SHA512:
6
- metadata.gz: c30142befe5484d340f148c9b99f3e272c7cf640b398eaf51e6147aee71f96bc6bc32935ef572a43f4d1ce39cf51c6e8c15b0e2d96b46946a89ec1dcca120a02
7
- data.tar.gz: '092c000f53ecf32db45765d568c840e792fb8bda963db5e3be0a1c27f3c74bd901b96635e310537e259db25e9f411c86505bfce9d236986af4c4c933737441e2'
6
+ metadata.gz: 55afbcc5d55fd62967ed3dfbbcbccf4e23933cf44f98ae9249ff47f631c7c4a62c1dea93f6961da81d62035547d516849bd1527ca98bf060eff1a659b2a61e7a
7
+ data.tar.gz: d6e38f8106e168aeb5ce8b6c9e8f14108c00eff87fdba43dcf0edc75c2c1d330f248e777b461ff7378fdaa5b894a2181883013337764662b422b91ed15c2962b
data/.gitignore CHANGED
@@ -1,6 +1,7 @@
1
1
  .DS_Store
2
- .idea/
2
+ .idea
3
+ .gem
3
4
  bundle
4
- docker
5
5
  docker-compose.yml
6
6
  pkg
7
+ up
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- quando (0.0.5)
4
+ quando (0.0.8)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -12,7 +12,7 @@ GEM
12
12
  pry (0.11.3)
13
13
  coderay (~> 1.1.0)
14
14
  method_source (~> 0.9.0)
15
- rake (10.5.0)
15
+ rake (11.3.0)
16
16
  rspec (3.6.0)
17
17
  rspec-core (~> 3.6.0)
18
18
  rspec-expectations (~> 3.6.0)
@@ -31,11 +31,11 @@ PLATFORMS
31
31
  ruby
32
32
 
33
33
  DEPENDENCIES
34
- bundler (~> 1.15)
34
+ bundler (>= 1.15)
35
35
  pry
36
36
  quando!
37
- rake (~> 10.0)
38
- rspec (~> 3.0)
37
+ rake (~> 11)
38
+ rspec (>= 3.0)
39
39
 
40
40
  BUNDLED WITH
41
41
  1.17.2
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- ![Quando](quando.png "Quando")
1
+ # Quando
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/quando.svg)](https://badge.fury.io/rb/quando)
4
4
  [![Build Status](https://semaphoreci.com/api/v1/kinkou/quando/branches/master/shields_badge.svg)](https://semaphoreci.com/kinkou/quando)
@@ -131,7 +131,7 @@ Quando.configure do |c|
131
131
  c.formats = [
132
132
  /^ #{c.day} #{c.dlm} #{c.month_txt} #{c.dlm} #{c.year} $/xi,
133
133
  # compiles into something like
134
- # /^ (?<day>…) [ .]+ (?<month>jan|feb|…) [ .]+ (?<year>…) $/xi
134
+ # /^ (?<day> …) [ -.\/]+ (?<month> jan|feb|…) [ -.\/]+ (?<year> …) $/xi
135
135
  # and returns ~ #<MatchData "14 Apr 1965" day:"14" month:"Apr" year:"1965">
136
136
  # on successful match
137
137
  ]
@@ -152,7 +152,7 @@ Quando.config.formats = [
152
152
  Quando.parse('04.2019') #=> #<Date: 2019-04-01>
153
153
  ```
154
154
 
155
- If there is a named capture ```:year```, it is used in the result. If the format matcher did not define such named group, current year is used. If the captured value is less than ```100``` (which is the case for years written as 2-digit numbers), Quando will add a value from ```Quando.config.century``` (defaults to ```2000```), effectively converting, for example, ```18``` to ```2018``` (ok, it's not really a century, but rather a *nittonhundratalet*, but let's keep things simple). Be mindful of this behaviour, adjusting ```Quando.config.century``` accordingly:
155
+ If there is a named capture ```:year```, it is used in the result. If the format matcher did not define such named group, current UTC year is used. If the captured value is less than ```100``` (which is the case for years written as 2-digit numbers), Quando will use the ```Quando.config.century``` setting (defaults to ```21```), effectively converting, for example, ```18``` to ```2018```. Be mindful of this behaviour, adjusting ```Quando.config.century``` accordingly:
156
156
 
157
157
  ```ruby
158
158
  Quando.config.formats = [Quando.config.year]
@@ -161,9 +161,9 @@ Quando.parse('2019') #=> #<Date: 2019-01-01>
161
161
  Quando.config.formats = [Quando.config.year2]
162
162
  Quando.parse('65') #=> #<Date: 2065-01-01>
163
163
 
164
- Quando.parse('65', century: 1900) #=> #<Date: 1965-01-01>
164
+ Quando.parse('65', century: 20) #=> #<Date: 1965-01-01>
165
165
  # or
166
- Quando.config.century = 1900
166
+ Quando.config.century = 20
167
167
  Quando.parse('65') #=> #<Date: 1965-01-01>
168
168
  ```
169
169
 
@@ -203,6 +203,8 @@ m = /(?<year>#{Quando.config.year}) (?<day>\d\d) (?<month>[A-Z]+)/i
203
203
  Quando.parse('1965 14 Apr', matcher: m) #=> #<Date: 1965-04-14>
204
204
  ```
205
205
 
206
+ In both cases it will not change the global configuration (but note that calling setter methods on ```Quando.config``` will).
207
+
206
208
  ### Requirements
207
209
 
208
210
  Ruby >= 1.9.3. Enjoy!
@@ -9,8 +9,8 @@ module Quando
9
9
 
10
10
  # @param date [String]
11
11
  # @param opts [Hash]
12
- # @option opts [Regexp, Array<Regexp>] :matcher (nil)
13
- # @option opts [Integer] :century (nil)
12
+ # @option opts [Regexp, Array<Regexp>] :matcher
13
+ # @option opts [Integer] :century
14
14
  # @return [Date, nil]
15
15
  def self.parse(date, opts = {})
16
16
  return if (date = date.to_s.strip).empty?
@@ -7,65 +7,77 @@ module Quando
7
7
  MONTHS = [:jan, :feb, :mar, :apr, :may, :jun, :jul, :aug, :sep, :oct, :nov, :dec]
8
8
  AUTOUPDATE = [:dlm, :year, :year2, :day, *MONTHS, :month_num]
9
9
  COMPOUND = [:month_txt, :formats]
10
- OPTIONS = [*AUTOUPDATE, *COMPOUND, :century]
10
+ OPTIONS = [*AUTOUPDATE, *COMPOUND]
11
+ CENTURY = 21
11
12
 
12
13
  private_constant :AUTOUPDATE, :COMPOUND, :OPTIONS
13
14
 
14
15
  attr_accessor *OPTIONS
15
16
 
16
17
  def initialize
17
- @century = 2000
18
-
19
- @dlm = /[ -.\/]+/
20
- @year = /(?<year> \d{4})/x
21
- @year2 = /(?<year> \d{2})/x
22
- @month_num = /(?<month> 1[0-2] | 0?[1-9])/x
23
- @day = /(?<day> 3[0-1] | [12][0-9] | 0?[1-9])/x
24
-
25
- @jan = /JAN(?:UARY)?/xi
26
- @feb = /FEB(?:RUARY)? /xi
27
- @mar = /MAR(?:CH)?/xi
28
- @apr = /APR(?:IL)?/xi
29
- @may = /MAY/xi
30
- @jun = /JUNE?/xi
31
- @jul = /JULY?/xi
32
- @aug = /AUG(?:UST)?/xi
33
- @sep = /SEP(?:TEMBER)?/xi
34
- @oct = /OCT(?:OBER)?/xi
35
- @nov = /NOV(?:EMBER)? /xi
36
- @dec = /DEC(?:EMBER)? /xi
18
+ @dlm = /[- .\/]+/
19
+ @year = /(?<year>\d{4})/
20
+ @year2 = /(?<year>\d{2})/
21
+ @month_num = /(?<month> 1[012] | 0?[1-9])/x
22
+ @day = /(?<day> 3[01] | [12]\d | 0?[1-9])/x
23
+
24
+ @jan = /JAN(?:UARY)?/i
25
+ @feb = /FEB(?:RUARY)?/i
26
+ @mar = /MAR(?:CH)?/i
27
+ @apr = /APR(?:IL)?/i
28
+ @may = /MAY/i
29
+ @jun = /JUNE?/i
30
+ @jul = /JULY?/i
31
+ @aug = /AUG(?:UST)?/i
32
+ @sep = /SEP(?:TEMBER)?/i
33
+ @oct = /OCT(?:OBER)?/i
34
+ @nov = /NOV(?:EMBER)?/i
35
+ @dec = /DEC(?:EMBER)?/i
36
+
37
+ self.century=(CENTURY)
37
38
 
38
39
  uniupdate!
39
40
  end
40
41
 
41
- # Sets @month_txt which is a compound of all month regexps and matches any month name
42
+ # Sets @month_txt, a compound of all month regexps that matches any month name
42
43
  def unimonth!
43
44
  all_months_txt_rxs = MONTHS.map { |m| instance_variable_get("@#{m}".to_sym) }.join('|')
44
45
  @month_txt = Regexp.new("(?<month>#{all_months_txt_rxs})", true)
45
46
  end
46
47
 
47
- # Sets @formats which is an array of regexps used in succession to match and identify parts of the dates
48
+ # Sets @formats which is an array of regexps used in succession to match and identify date parts
48
49
  def uniformats!
49
50
  @formats = [
50
51
  # Formats with a 4-digits year
51
52
  # 14.4.1965, 14/04/1965, 14-4-1965, 14 04 1965, …
52
- /\A\s* #{@day} #{@dlm} #{@month_num} #{@dlm} #{@year} \s*\z/xi,
53
+ /^\s* #{@day} #{@dlm} #{@month_num} #{@dlm} #{@year} \s*$/xi,
53
54
 
54
55
  # 14-APRIL-1965, 14-apr-1965, 14/Apr/1965, …
55
- /\A\s* #{@day} #{@dlm} #{@month_txt} #{@dlm} #{@year} \s*\z/xi,
56
+ /^\s* #{@day} #{@dlm} #{@month_txt} #{@dlm} #{@year} \s*$/xi,
56
57
 
57
58
  # April 1965, apr.1965, …
58
- /\A\s* #{@month_txt} #{@dlm} #{@year} \s*\z/xi,
59
+ /^\s* #{@month_txt} #{@dlm} #{@year} \s*$/xi,
59
60
 
60
61
  # Same formats with a 2-digits year
61
62
  # 13.12.05, 13/12/05, 13-12-05, …
62
- /\A\s* #{@day} #{@dlm} #{@month_num} #{@dlm} #{@year2} \s*\z/xi,
63
+ /^\s* #{@day} #{@dlm} #{@month_num} #{@dlm} #{@year2} \s*$/xi,
63
64
 
64
65
  # April, DECEMBER, sep., …
65
- /\A\s* #{@month_txt} \s*\z/xi,
66
+ /^\s* #{@month_txt} \s*$/xi,
66
67
  ]
67
68
  end
68
69
 
70
+ # @return [Integer]
71
+ def century
72
+ @century
73
+ end
74
+
75
+ # @param value [Integer]
76
+ # @return [Integer]
77
+ def century=(value)
78
+ @century = (value == 0 ? 1 : value) || CENTURY
79
+ end
80
+
69
81
  # A single method to update all compound matchers when a part matcher was changed
70
82
  def uniupdate!
71
83
  unimonth!
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Quando
4
-
5
4
  class Parser
6
5
 
7
6
  def initialize
@@ -10,7 +9,7 @@ module Quando
10
9
 
11
10
  # @return [Quando::Parser]
12
11
  def configure
13
- yield(@config = Quando.config.dup)
12
+ yield(@config ||= Quando.config.dup)
14
13
  self
15
14
  end
16
15
 
@@ -22,11 +21,11 @@ module Quando
22
21
  # @param text_date [String]
23
22
  # @return [Date, nil]
24
23
  def parse(text_date)
25
- config.formats.each do |rx|
26
- @date_parts = text_date.match(rx)
24
+ config.formats.each do |regexp|
25
+ @date_parts = text_date.match(regexp)
27
26
  next unless @date_parts
28
27
 
29
- @current_format = rx
28
+ @current_format = regexp
30
29
  year, month, day = detect_year, detect_month, detect_day
31
30
  next unless (year && month && day)
32
31
 
@@ -40,46 +39,39 @@ module Quando
40
39
 
41
40
  # @return [Integer, nil]
42
41
  def detect_year
43
- unless searched?(:year)
44
- return Time.current.year if Time.respond_to?(:current)
45
- return Time.now.getlocal.year
46
- end
47
-
42
+ return Time.now.utc.year unless wanted?(:year)
48
43
  return unless found?(:year)
49
44
 
50
45
  year = @date_parts[:year].to_i
51
- year < 100 ? year + config.century : year
46
+ year.abs < 100 ? year + century_to_hundreds : year
52
47
  end
53
48
 
54
49
  # @return [Integer, nil]
55
50
  def detect_month
56
- return 1 unless searched?(:month)
51
+ return 1 unless wanted?(:month)
57
52
  return unless found?(:month)
58
53
 
59
54
  month = @date_parts[:month]
60
55
 
61
- month_num = if config.month_num.match(month)
62
- month.to_i
63
- else
64
- month_index = Quando::Config::MONTHS.find_index do |month_name|
65
- month_name_rx = config.send(month_name)
66
- month_name_rx.match(month)
67
- end
56
+ month_index = Quando::Config::MONTHS.find_index do |month_name|
57
+ config.send(month_name) =~ month
58
+ end
68
59
 
69
- month_index + 1 if month_index
60
+ if month_index
61
+ month_index += 1
62
+ return month_index if valid_month?(month_index)
70
63
  end
71
64
 
72
- month_num if (1..12).include?(month_num)
65
+ month.to_i if valid_month?(month)
73
66
  end
74
67
 
75
68
  # @return [Integer, nil]
76
69
  def detect_day
77
- return 1 unless searched?(:day)
70
+ return 1 unless wanted?(:day)
78
71
  return unless found?(:day)
79
72
 
80
73
  day = @date_parts[:day].to_i
81
-
82
- day if (1..31).include?(day)
74
+ day if valid_day?(day)
83
75
  end
84
76
 
85
77
  # @param date_part [Symbol]
@@ -88,10 +80,23 @@ module Quando
88
80
  end
89
81
 
90
82
  # @param date_part [Symbol]
91
- def searched?(date_part)
92
- !!@current_format.named_captures[date_part.to_s]
83
+ def wanted?(date_part)
84
+ @current_format.names.include?(date_part.to_s)
93
85
  end
94
86
 
95
- end
87
+ # @param value [Integer]
88
+ def valid_month?(value)
89
+ (1..12).include?(value.to_i)
90
+ end
91
+
92
+ # @param value [Integer]
93
+ def valid_day?(value)
94
+ (1..31).include?(value.to_i)
95
+ end
96
96
 
97
+ def century_to_hundreds
98
+ (config.century > 0 ? config.century - 1 : config.century + 1) * 100
99
+ end
100
+
101
+ end
97
102
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Quando
4
- VERSION = '0.0.6'
4
+ VERSION = '0.0.8'
5
5
  end
@@ -20,8 +20,8 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.required_ruby_version = '>= 1.9.3'
22
22
 
23
- spec.add_development_dependency 'bundler', '~> 1.15'
24
- spec.add_development_dependency 'rake', '~> 10.0'
25
- spec.add_development_dependency 'rspec', '~> 3.0'
23
+ spec.add_development_dependency 'bundler', '>= 1.15'
24
+ spec.add_development_dependency 'rake', '~> 11'
25
+ spec.add_development_dependency 'rspec', '>= 3.0'
26
26
  spec.add_development_dependency 'pry'
27
27
  end
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quando
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Konotopov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-10 00:00:00.000000000 Z
11
+ date: 2020-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.15'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.15'
27
27
  - !ruby/object:Gem::Dependency
@@ -30,26 +30,26 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '11'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '11'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '3.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
55
  - !ruby/object:Gem::Dependency
@@ -88,7 +88,6 @@ files:
88
88
  - lib/quando/parser.rb
89
89
  - lib/quando/version.rb
90
90
  - quando.gemspec
91
- - quando.png
92
91
  homepage: https://github.com/kinkou/quando
93
92
  licenses:
94
93
  - MIT
data/quando.png DELETED
Binary file