date_age 0.0.1
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/.gitignore +4 -0
- data/.rvmrc +3 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +4 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +34 -0
- data/Rakefile +8 -0
- data/date_age.gemspec +37 -0
- data/lib/date_age.rb +25 -0
- data/lib/date_age/version.rb +3 -0
- data/spec/date_age_spec.rb +62 -0
- data/spec/spec_helper.rb +10 -0
- metadata +71 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2005-2011 David Heinemeier Hansson
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
= \DateAge - Age Calculation with Date class
|
2
|
+
|
3
|
+
home :: https://github.com/meltedice/date_age
|
4
|
+
|
5
|
+
== Description
|
6
|
+
|
7
|
+
Calculate age
|
8
|
+
|
9
|
+
== Download and installation
|
10
|
+
|
11
|
+
The latest version of DateAge can be installed with RubyGems:
|
12
|
+
|
13
|
+
% [sudo] gem install date_age
|
14
|
+
|
15
|
+
== Synopsis
|
16
|
+
|
17
|
+
To calculate age:
|
18
|
+
|
19
|
+
gem 'date_age'
|
20
|
+
require 'date_age'
|
21
|
+
|
22
|
+
birthday = Date.parse("1989/02/11")
|
23
|
+
puts birthday.age_at("2012/02/12") # => 23
|
24
|
+
puts birthday.age # => same as birthday.age_at(Date.today)
|
25
|
+
|
26
|
+
== License
|
27
|
+
|
28
|
+
DateAge is released under the MIT license.
|
29
|
+
|
30
|
+
== Warranty
|
31
|
+
|
32
|
+
This software is provided "as is" and without any express or implied
|
33
|
+
warranties, including, without limitation, the implied warranties of
|
34
|
+
merchantibility and fitness for a particular purpose.
|
data/Rakefile
ADDED
data/date_age.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "date_age/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "date_age"
|
7
|
+
s.version = DateAge::VERSION
|
8
|
+
s.authors = ["ice"]
|
9
|
+
s.email = ["meltedise@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = "date_age-#{s.version}"
|
12
|
+
s.description = "Calculate age with Date class"
|
13
|
+
s.license = 'MIT'
|
14
|
+
|
15
|
+
s.rubyforge_project = "date_age"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
# specify any dependencies here; for example:
|
23
|
+
# s.add_development_dependency "rspec"
|
24
|
+
# s.add_runtime_dependency "rest-client"
|
25
|
+
|
26
|
+
if s.respond_to? :specification_version then
|
27
|
+
s.specification_version = 3
|
28
|
+
|
29
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
30
|
+
s.add_development_dependency %q<rspec>, ['~> 2.0.0.beta.22']
|
31
|
+
else
|
32
|
+
s.add_dependency %q<rspec>, ['~> 2.0.0.beta.22']
|
33
|
+
end
|
34
|
+
else
|
35
|
+
s.add_dependency %q<rspec>, ['~> 2.0.0.beta.22']
|
36
|
+
end
|
37
|
+
end
|
data/lib/date_age.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'date_age/version'
|
3
|
+
|
4
|
+
class Date
|
5
|
+
# Return age at specified `date`
|
6
|
+
def age_at(date)
|
7
|
+
unless date.respond_to?(:year) && date.respond_to?(:month) && date.respond_to?(:day)
|
8
|
+
raise ArgumentError, "age_at(date): date should respond to :year, :month and :day"
|
9
|
+
end
|
10
|
+
birthday = self
|
11
|
+
today = date
|
12
|
+
age = today.year - birthday.year
|
13
|
+
if today.month < birthday.month
|
14
|
+
age -= 1
|
15
|
+
elsif birthday.month == today.month
|
16
|
+
age -= 1 if today.day < birthday.day
|
17
|
+
end
|
18
|
+
age
|
19
|
+
end
|
20
|
+
|
21
|
+
# Return age of today
|
22
|
+
def age
|
23
|
+
age_at(Date.today)
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe DateAge do
|
6
|
+
def birthday(str) Date.parse(str) end
|
7
|
+
def today(str) Date.parse(str) end
|
8
|
+
|
9
|
+
describe "age_at" do
|
10
|
+
describe "between different years" do
|
11
|
+
it "should be 1 year old" do
|
12
|
+
birthday("2010/01/01").age_at(today("2011/01/01")).should == 1
|
13
|
+
birthday("2010/01/01").age_at(today("2011/05/05")).should == 1
|
14
|
+
birthday("2010/01/01").age_at(today("2011/12/31")).should == 1
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should be greater than 1 year old" do
|
18
|
+
birthday("1980/01/01").age_at(today("2011/01/01")).should == 31
|
19
|
+
birthday("1980/01/01").age_at(today("2011/05/05")).should == 31
|
20
|
+
birthday("1980/01/01").age_at(today("2011/12/31")).should == 31
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "between the same years" do
|
25
|
+
it "should be 0 years old" do
|
26
|
+
birthday("2010/01/01").age_at(today("2010/01/01")).should == 0
|
27
|
+
birthday("2010/01/01").age_at(today("2010/12/31")).should == 0
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "0 years old between different years" do
|
32
|
+
it "should be 0 years old" do
|
33
|
+
birthday("2010/02/01").age_at(today("2011/01/31")).should == 0
|
34
|
+
birthday("2010/02/02").age_at(today("2011/02/01")).should == 0
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "-1 year old" do
|
39
|
+
it "should be -1 years old" do
|
40
|
+
birthday("2011/01/31").age_at(today("2010/02/01")).should == -1
|
41
|
+
birthday("2011/02/01").age_at(today("2010/02/02")).should == -1
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# http://en.wikipedia.org/wiki/Leap_year
|
46
|
+
describe "in leap year" do
|
47
|
+
it "should treat birthday Feb. 29th as Mar. 1st in non leap year" do
|
48
|
+
birthday("1996/02/29").age_at(today("2011/02/28")).should == 14
|
49
|
+
birthday("1996/02/29").age_at(today("2011/03/01")).should == 15
|
50
|
+
birthday("1996/02/29").age_at(today("2012/02/28")).should == 15
|
51
|
+
birthday("1996/02/29").age_at(today("2012/02/29")).should == 16
|
52
|
+
birthday("1996/02/29").age_at(today("2012/03/01")).should == 16
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "exception" do
|
57
|
+
it "should raise ArgumentError when passed invalid argument" do
|
58
|
+
lambda {Date.today.age_at(nil)}.should raise_error(ArgumentError)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: date_age
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- ice
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-12 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70329499452660 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.0.0.beta.22
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70329499452660
|
25
|
+
description: Calculate age with Date class
|
26
|
+
email:
|
27
|
+
- meltedise@gmail.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- .rvmrc
|
34
|
+
- CHANGELOG.md
|
35
|
+
- Gemfile
|
36
|
+
- MIT-LICENSE
|
37
|
+
- README.rdoc
|
38
|
+
- Rakefile
|
39
|
+
- date_age.gemspec
|
40
|
+
- lib/date_age.rb
|
41
|
+
- lib/date_age/version.rb
|
42
|
+
- spec/date_age_spec.rb
|
43
|
+
- spec/spec_helper.rb
|
44
|
+
homepage: ''
|
45
|
+
licenses:
|
46
|
+
- MIT
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project: date_age
|
65
|
+
rubygems_version: 1.8.15
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: date_age-0.0.1
|
69
|
+
test_files:
|
70
|
+
- spec/date_age_spec.rb
|
71
|
+
- spec/spec_helper.rb
|