icu_ratings 1.7.0 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/icu_ratings/player.rb +2 -2
- data/lib/icu_ratings/tournament.rb +1 -1
- data/lib/icu_ratings/util.rb +34 -32
- data/lib/icu_ratings/version.rb +1 -1
- data/spec/util_spec.rb +47 -45
- metadata +4 -4
data/lib/icu_ratings/player.rb
CHANGED
@@ -211,9 +211,9 @@ module ICU
|
|
211
211
|
case
|
212
212
|
when args[:rating] >= 2100 then
|
213
213
|
16
|
214
|
-
when ICU::Util.age(args[:dob], args[:start]) < 21 then
|
214
|
+
when ICU::Util::Date.age(args[:dob], args[:start]) < 21 then
|
215
215
|
40
|
216
|
-
when ICU::Util.age(args[:joined], args[:start]) < 8 then
|
216
|
+
when ICU::Util::Date.age(args[:joined], args[:start]) < 8 then
|
217
217
|
32
|
218
218
|
else
|
219
219
|
24
|
data/lib/icu_ratings/util.rb
CHANGED
@@ -2,41 +2,43 @@ require 'date' # needed for 1.9.1
|
|
2
2
|
|
3
3
|
module ICU
|
4
4
|
module Util
|
5
|
+
module Date
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
7
|
+
# Parses strings into date objects, interpreting nn/nn/nnnn as dd/mm/yyyy. It raises an exception on error.
|
8
|
+
#
|
9
|
+
# parsedate!('1955-11-09') # => Date (1955-11-09)
|
10
|
+
# parsedate!('02/03/2009') # => Date (2009-03-02)
|
11
|
+
# parsedate!('02/23/2009') # => Date (2009-02-23)
|
12
|
+
# parsedate!('16th June 1986') # => Date (1986-06-16)
|
13
|
+
# parsedate!('not a date') # exception raised
|
14
|
+
#
|
15
|
+
# Note that the parse method of the Date class behaves differently in Ruby 1.8.7 and 1.9.1.
|
16
|
+
# In 1.8.7 it assumes American dates and will raise ArgumentError on "30/03/2003".
|
17
|
+
# In 1.9.1 it assumes European dates and will raise ArgumentError on "03/30/2003".
|
18
|
+
#
|
19
|
+
def self.parsedate!(date)
|
20
|
+
return date.clone if date.is_a?(::Date)
|
21
|
+
string = date.to_s.strip
|
22
|
+
raise "invalid date (#{date})" unless string.match(/[1-9]/)
|
23
|
+
string = [$3].concat($2.to_i > 12 ? [$1, $2] : [$2, $1]).join('-') if string.match(/^(\d{1,2}).(\d{1,2}).(\d{4})$/)
|
24
|
+
begin
|
25
|
+
::Date.parse(string, true)
|
26
|
+
rescue
|
27
|
+
raise "invalid date (#{date})"
|
28
|
+
end
|
27
29
|
end
|
28
|
-
end
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
31
|
+
# Returns the difference of two dates:
|
32
|
+
#
|
33
|
+
# age(born, date) # age in years at the given date (Float)
|
34
|
+
# age(born) # age in years now (today)
|
35
|
+
#
|
36
|
+
# Internally it uses _parsedate!_ so can throw a exception if an invalid date is supplied.
|
37
|
+
def self.age(born, date=::Date.today)
|
38
|
+
born = parsedate!(born)
|
39
|
+
date = parsedate!(date)
|
40
|
+
date.year - born.year + (date.yday - born.yday) / 366.0
|
41
|
+
end
|
40
42
|
end
|
41
43
|
end
|
42
44
|
end
|
data/lib/icu_ratings/version.rb
CHANGED
data/spec/util_spec.rb
CHANGED
@@ -1,63 +1,65 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
3
|
module ICU
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
module Util
|
5
|
+
describe Date do
|
6
|
+
context "#parsedate!" do
|
7
|
+
it "should return instances of class Date" do
|
8
|
+
Date.parsedate!('2001-01-01').should be_a(::Date)
|
9
|
+
end
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
it "should parse standard dates" do
|
12
|
+
Date.parsedate!('2001-01-01').to_s.should == '2001-01-01'
|
13
|
+
Date.parsedate!('1955-11-09').to_s.should == '1955-11-09'
|
14
|
+
end
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
16
|
+
it "should handle US format" do
|
17
|
+
Date.parsedate!('03/30/2009').to_s.should == '2009-03-30'
|
18
|
+
end
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
it "should handle European format" do
|
21
|
+
Date.parsedate!('30/03/2009').to_s.should == '2009-03-30'
|
22
|
+
end
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
24
|
+
it "should prefer European format" do
|
25
|
+
Date.parsedate!('02/03/2009').to_s.should == '2009-03-02'
|
26
|
+
end
|
26
27
|
|
27
|
-
|
28
|
-
|
29
|
-
|
28
|
+
it "should handle US style when there's no alternative" do
|
29
|
+
Date.parsedate!('02/23/2009').to_s.should == '2009-02-23'
|
30
|
+
end
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
|
32
|
+
it "should handle single digits" do
|
33
|
+
Date.parsedate!('9/8/2006').to_s.should == '2006-08-09'
|
34
|
+
end
|
34
35
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
36
|
+
it "should handle names of months" do
|
37
|
+
Date.parsedate!('9th Nov 1955').to_s.should == '1955-11-09'
|
38
|
+
Date.parsedate!('16th June 1986').to_s.should == '1986-06-16'
|
39
|
+
end
|
39
40
|
|
40
|
-
|
41
|
-
|
42
|
-
|
41
|
+
it "should raise exception on error" do
|
42
|
+
lambda { Date.parsedate!('2010-13-32') }.should raise_error(/invalid date/)
|
43
|
+
end
|
43
44
|
|
44
|
-
|
45
|
-
|
45
|
+
it "should accept Date objects as well as strings" do
|
46
|
+
Date.parsedate!(::Date.parse('2013-07-01')).should == ::Date.parse('2013-07-01')
|
47
|
+
end
|
46
48
|
end
|
47
|
-
end
|
48
49
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
50
|
+
context "#age" do
|
51
|
+
it "should return age in years" do
|
52
|
+
Date.age('2001-01-01', '2001-01-01').should == 0.0
|
53
|
+
Date.age('2001-01-01', '2002-01-01').should == 1.0
|
54
|
+
Date.age('2001-01-01', '2001-01-02').should be_within(0.01).of(1/365)
|
55
|
+
Date.age('2001-01-01', '2001-02-01').should be_within(0.01).of(1/12.0)
|
56
|
+
Date.age('1955-11-09', '2010-01-17').should be_within(0.01).of(54.2)
|
57
|
+
Date.age('2001-01-01', '2000-01-01').should == -1.0
|
58
|
+
end
|
58
59
|
|
59
|
-
|
60
|
-
|
60
|
+
it "should default second date to today" do
|
61
|
+
Date.age(::Date.today).should == 0.0
|
62
|
+
end
|
61
63
|
end
|
62
64
|
end
|
63
65
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: icu_ratings
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.8.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-10-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -112,7 +112,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
112
112
|
version: '0'
|
113
113
|
segments:
|
114
114
|
- 0
|
115
|
-
hash: -
|
115
|
+
hash: -933146652784732237
|
116
116
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
117
|
none: false
|
118
118
|
requirements:
|
@@ -121,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
121
121
|
version: '0'
|
122
122
|
segments:
|
123
123
|
- 0
|
124
|
-
hash: -
|
124
|
+
hash: -933146652784732237
|
125
125
|
requirements: []
|
126
126
|
rubyforge_project: icu_ratings
|
127
127
|
rubygems_version: 1.8.23
|