my_age 0.1.6 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/my_age.rb +11 -10
- data/lib/my_age/calculator.rb +37 -34
- data/lib/my_age/cli.rb +31 -29
- data/lib/my_age/helper.rb +5 -6
- data/lib/my_age/version.rb +3 -1
- data/my_age.gemspec +1 -1
- metadata +5 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5d6c4a019d9e132f5052edf89bd2d7a83e6c625940c2814f3474cccd6fc632e5
|
4
|
+
data.tar.gz: aec0ea1a792dc609d0588d993d6ad20a4bbe0ffaaf4a95c8815f3535de6e26fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d9b5dc50fa470f7f1dc6d361c90d7f591b7566de57c7044a1a40dcc23cb63bd4549aa941259d29ba45a23de2bbcc84912f026875ffde97b532d144b72d0fe73
|
7
|
+
data.tar.gz: 4eaa23a13d333ae60a933bf6d5198777092b57dc45b45966070ffe148601cd9951be176d75238a0b0a092e32a1190cbccec3d5f3150d4e85b75faee31161f5e5
|
data/lib/my_age.rb
CHANGED
@@ -1,14 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'active_support'
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
4
|
+
require 'active_support/core_ext/object/blank'
|
5
|
+
require 'active_support/core_ext/date/calculations'
|
6
|
+
require 'my_age/version'
|
7
|
+
require 'my_age/calculator'
|
8
|
+
require 'my_age/helper'
|
7
9
|
|
8
10
|
module MyAge
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
def self.included(base)
|
12
|
+
base.include MyAge::Calculator
|
13
|
+
base.include MyAge::Helper
|
14
|
+
end
|
14
15
|
end
|
data/lib/my_age/calculator.rb
CHANGED
@@ -1,37 +1,40 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'my_age/helper'
|
2
4
|
|
3
5
|
module MyAge
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
6
|
+
module Calculator
|
7
|
+
unless respond_to?(:dob)
|
8
|
+
def self.included(base)
|
9
|
+
def base.my_dob(col)
|
10
|
+
define_method(:dob) { send col }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def age(date = Date.today)
|
16
|
+
return 0 if dob.blank? || date.blank? || date <= dob
|
17
|
+
|
18
|
+
(date_to_int(date) - date_to_int(dob)) / 10_000
|
19
|
+
end
|
20
|
+
|
21
|
+
def method_missing(name, *args)
|
22
|
+
if name.to_s =~ /_as_of_/
|
23
|
+
date_method = name.to_s.split('_as_of_')[-1]
|
24
|
+
age today.send(date_method.to_sym, *args)
|
25
|
+
else
|
26
|
+
super
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def date_to_int(date)
|
33
|
+
date.strftime('%Y%m%d').to_i
|
34
|
+
end
|
35
|
+
|
36
|
+
def today
|
37
|
+
Date.today
|
38
|
+
end
|
39
|
+
end
|
37
40
|
end
|
data/lib/my_age/cli.rb
CHANGED
@@ -1,37 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'thor'
|
2
4
|
require 'my_age'
|
3
5
|
module MyAge
|
4
|
-
|
5
|
-
|
6
|
-
|
6
|
+
class CLI < Thor
|
7
|
+
attr_accessor :dob
|
8
|
+
include MyAge::Calculator
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
10
|
+
desc 'is AGE', '-d date_of_birth -a as_of_date, Dates are in yyyy-mm-dd format'
|
11
|
+
method_option :dob, aliases: 'd', desc: 'date of birth in yyyy-mm-dd', required: true
|
12
|
+
method_option :as_of, aliases: 'a', desc: 'as of date in yyyy-mm-dd', required: false
|
13
|
+
def is
|
14
|
+
self.dob = get_date(options[:dob])
|
15
|
+
date = options[:as_of].present? ? get_date(options[:as_of]) : Date.today
|
16
|
+
puts age(date)
|
17
|
+
end
|
16
18
|
|
17
|
-
|
18
|
-
def get_date(date)
|
19
|
-
if date =~ /^\d{4}-\d{2}-\d{2}$/
|
20
|
-
DateTime.strptime(date, "%Y-%m-%d")
|
21
|
-
else
|
22
|
-
date_from_active_support_core_ext_helper(date)
|
23
|
-
end
|
24
|
-
rescue NoMethodError
|
25
|
-
Date.send(date)
|
26
|
-
end
|
19
|
+
private
|
27
20
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
21
|
+
def get_date(date)
|
22
|
+
if date =~ /^\d{4}-\d{2}-\d{2}$/
|
23
|
+
DateTime.strptime(date, '%Y-%m-%d')
|
24
|
+
else
|
25
|
+
date_from_active_support_core_ext_helper(date)
|
26
|
+
end
|
27
|
+
rescue NoMethodError
|
28
|
+
Date.send(date)
|
29
|
+
end
|
35
30
|
|
36
|
-
|
31
|
+
def date_from_active_support_core_ext_helper(date)
|
32
|
+
dates = date.delete(')').split('(')
|
33
|
+
method = dates[0].to_sym
|
34
|
+
arg = dates[1]
|
35
|
+
arg.nil? ? Date.today.send(method) :
|
36
|
+
Date.today.send(method, arg.to_i)
|
37
|
+
end
|
38
|
+
end
|
37
39
|
end
|
data/lib/my_age/helper.rb
CHANGED
data/lib/my_age/version.rb
CHANGED
data/my_age.gemspec
CHANGED
@@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
|
|
26
26
|
spec.add_dependency "activesupport", ">= 4.2"
|
27
27
|
spec.add_dependency "thor", "~>0.20.0"
|
28
28
|
spec.add_development_dependency "bundler", "~> 1.13"
|
29
|
-
spec.add_development_dependency "rake", "~>
|
29
|
+
spec.add_development_dependency "rake", "~> 12.3.3"
|
30
30
|
spec.add_development_dependency "rspec", "~> 3.0"
|
31
31
|
spec.add_development_dependency "pry", "~>0.11"
|
32
32
|
spec.add_development_dependency "cucumber", "~>3.1.0"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: my_age
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Edgar
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-02-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -58,14 +58,14 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 12.3.3
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: 12.3.3
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -167,8 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
167
167
|
- !ruby/object:Gem::Version
|
168
168
|
version: '0'
|
169
169
|
requirements: []
|
170
|
-
|
171
|
-
rubygems_version: 2.6.14
|
170
|
+
rubygems_version: 3.0.3
|
172
171
|
signing_key:
|
173
172
|
specification_version: 4
|
174
173
|
summary: Calculate my age.
|