icu_ratings 1.7.0 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -156,7 +156,7 @@ module ICU
156
156
 
157
157
  # Set the start date. Raises exception on error.
158
158
  def start=(date)
159
- @start = ICU::Util.parsedate!(date)
159
+ @start = ICU::Util::Date.parsedate!(date)
160
160
  end
161
161
 
162
162
  # Set whether there are no bonuses (false by default).
@@ -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
- # Parses strings into date objects, interpreting nn/nn/nnnn as dd/mm/yyyy. It raises an exception on error.
7
- #
8
- # Util.parsedate!('1955-11-09') # => Date (1955-11-09)
9
- # Util.parsedate!('02/03/2009') # => Date (2009-03-02)
10
- # Util.parsedate!('02/23/2009') # => Date (2009-02-23)
11
- # Util.parsedate!('16th June 1986') # => Date (1986-06-16)
12
- # Util.parsedate!('not a date') # exception raised
13
- #
14
- # Note that the parse method of the Date class behaves differently in Ruby 1.8.7 and 1.9.1.
15
- # In 1.8.7 it assumes American dates and will raise ArgumentError on "30/03/2003".
16
- # In 1.9.1 it assumes European dates and will raise ArgumentError on "03/30/2003".
17
- #
18
- def self.parsedate!(date)
19
- return date.clone if date.is_a?(Date)
20
- string = date.to_s.strip
21
- raise "invalid date (#{date})" unless string.match(/[1-9]/)
22
- string = [$3].concat($2.to_i > 12 ? [$1, $2] : [$2, $1]).join('-') if string.match(/^(\d{1,2}).(\d{1,2}).(\d{4})$/)
23
- begin
24
- Date.parse(string, true)
25
- rescue
26
- raise "invalid date (#{date})"
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
- # Returns the difference of two dates:
31
- #
32
- # Util.age(born, date) # age in years at the given date (Float)
33
- # Util.age(born) # age in years now (today)
34
- #
35
- # Internally it uses _parsedate!_ so can throw a exception if an invalid date is supplied.
36
- def self.age(born, date=Date.today)
37
- born = parsedate!(born)
38
- date = parsedate!(date)
39
- date.year - born.year + (date.yday - born.yday) / 366.0
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ICU
4
4
  class Ratings
5
- VERSION = "1.7.0"
5
+ VERSION = "1.8.0"
6
6
  end
7
7
  end
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
- describe Util do
5
- context "#parsedate!" do
6
- it "should return instances of class Date" do
7
- Util.parsedate!('2001-01-01').should be_a(Date)
8
- end
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
- it "should parse standard dates" do
11
- Util.parsedate!('2001-01-01').to_s.should == '2001-01-01'
12
- Util.parsedate!('1955-11-09').to_s.should == '1955-11-09'
13
- end
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
- it "should handle US format" do
16
- Util.parsedate!('03/30/2009').to_s.should == '2009-03-30'
17
- end
16
+ it "should handle US format" do
17
+ Date.parsedate!('03/30/2009').to_s.should == '2009-03-30'
18
+ end
18
19
 
19
- it "should handle European format" do
20
- Util.parsedate!('30/03/2009').to_s.should == '2009-03-30'
21
- end
20
+ it "should handle European format" do
21
+ Date.parsedate!('30/03/2009').to_s.should == '2009-03-30'
22
+ end
22
23
 
23
- it "should prefer European format" do
24
- Util.parsedate!('02/03/2009').to_s.should == '2009-03-02'
25
- end
24
+ it "should prefer European format" do
25
+ Date.parsedate!('02/03/2009').to_s.should == '2009-03-02'
26
+ end
26
27
 
27
- it "should handle US style when there's no alternative" do
28
- Util.parsedate!('02/23/2009').to_s.should == '2009-02-23'
29
- end
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
- it "should handle single digits" do
32
- Util.parsedate!('9/8/2006').to_s.should == '2006-08-09'
33
- end
32
+ it "should handle single digits" do
33
+ Date.parsedate!('9/8/2006').to_s.should == '2006-08-09'
34
+ end
34
35
 
35
- it "should handle names of months" do
36
- Util.parsedate!('9th Nov 1955').to_s.should == '1955-11-09'
37
- Util.parsedate!('16th June 1986').to_s.should == '1986-06-16'
38
- end
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
- it "should raise exception on error" do
41
- lambda { Util.parsedate!('2010-13-32') }.should raise_error(/invalid date/)
42
- end
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
- it "should accept Date objects as well as strings" do
45
- Util.parsedate!(Date.parse('2013-07-01')).should == Date.parse('2013-07-01')
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
- context "#age" do
50
- it "should return age in years" do
51
- Util.age('2001-01-01', '2001-01-01').should == 0.0
52
- Util.age('2001-01-01', '2002-01-01').should == 1.0
53
- Util.age('2001-01-01', '2001-01-02').should be_within(0.01).of(1/365)
54
- Util.age('2001-01-01', '2001-02-01').should be_within(0.01).of(1/12.0)
55
- Util.age('1955-11-09', '2010-01-17').should be_within(0.01).of(54.2)
56
- Util.age('2001-01-01', '2000-01-01').should == -1.0
57
- end
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
- it "should default second date to today" do
60
- Util.age(Date.today).should == 0.0
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.7.0
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-08-31 00:00:00.000000000 Z
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: -3613568099200182560
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: -3613568099200182560
124
+ hash: -933146652784732237
125
125
  requirements: []
126
126
  rubyforge_project: icu_ratings
127
127
  rubygems_version: 1.8.23