my_age 0.1.1 → 0.1.2
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.
- checksums.yaml +4 -4
- data/README.md +23 -9
- data/lib/my_age/calculator.rb +0 -1
- data/lib/my_age/cli.rb +22 -3
- data/lib/my_age/version.rb +1 -1
- data/lib/my_age.rb +4 -0
- data/my_age.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 94e8609fc1e040a51451599b8c1991d2c1d92ac2
|
4
|
+
data.tar.gz: e2136277e36b0a95e274be82e95ff25049325680
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c142d271f738bd587b15ca62b5b8b2ff31c7633d95b8c6b38f74aa51e0f158464cb381615f021e041234f7230a1491e28d9205754e3ddc64ac99c8059d1bfd40
|
7
|
+
data.tar.gz: cf940465c100a3c566bc1a85900a80e7ad1282de4c0211887913e9b622fc1452e2801bd1bdd5424568311db624c20c19ef1f0a8c774b365056d331354b9e9f2e
|
data/README.md
CHANGED
@@ -18,34 +18,47 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
Mixin the
|
21
|
+
Mixin the `Age` module and call `age` method.
|
22
22
|
|
23
23
|
```
|
24
24
|
class User
|
25
25
|
attr_accessor :dob
|
26
|
-
include MyAge
|
26
|
+
include MyAge
|
27
27
|
end
|
28
28
|
|
29
29
|
user = User.new
|
30
|
+
## by default, age is calculated as of today
|
30
31
|
user.age
|
31
|
-
##
|
32
|
+
## but you can supply a specific date
|
32
33
|
user.age(date)
|
33
|
-
##
|
34
|
+
## there are a host of helpers starting with 'age_as_of_' to specify the date too
|
34
35
|
user.age_as_of_tomorrow
|
35
|
-
## age as of tomorrow
|
36
36
|
```
|
37
37
|
|
38
|
-
|
38
|
+
If your date-of-birth attribute is not called `dob`, you can assign it with `my_dob` so the gem knows where to look for it.
|
39
39
|
|
40
40
|
```
|
41
41
|
class User
|
42
42
|
attr_accessor :date_of_birth
|
43
|
-
include MyAge
|
44
|
-
my_dob :
|
43
|
+
include MyAge
|
44
|
+
my_dob :date_of_birthi #my attribute name
|
45
45
|
end
|
46
46
|
|
47
47
|
##everything else is the same
|
48
48
|
```
|
49
|
+
## Use ActiveSupport Helpers
|
50
|
+
You can use the helpers in `DateAndTime::Calculations`. Methods will look like `age_as_of_helper`:
|
51
|
+
|
52
|
+
```
|
53
|
+
## with #end_of_month, you can all
|
54
|
+
user.age_as_of_end_of_month
|
55
|
+
|
56
|
+
## or with #months_since
|
57
|
+
user.age_as_of_months_since(9)
|
58
|
+
|
59
|
+
## to be explicit, you can call
|
60
|
+
user.age_as_of_today ## same as user.age
|
61
|
+
```
|
49
62
|
|
50
63
|
## CLI
|
51
64
|
|
@@ -56,8 +69,9 @@ $ #=> 4
|
|
56
69
|
$ my_age is -d 1981-01-08
|
57
70
|
$ #=> age as of today
|
58
71
|
|
72
|
+
##ActiveSupport helpers work here too
|
59
73
|
$ my_age is -d 1981-01-03 -a tomorrow
|
60
|
-
$
|
74
|
+
$ my_age is -d 'years_ago(5)' -a tomorrow ##=> 5
|
61
75
|
|
62
76
|
```
|
63
77
|
|
data/lib/my_age/calculator.rb
CHANGED
data/lib/my_age/cli.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'thor'
|
2
2
|
require 'my_age'
|
3
|
-
|
3
|
+
require 'pry'
|
4
4
|
module MyAge
|
5
5
|
class CLI < Thor
|
6
6
|
attr_accessor :dob
|
@@ -10,10 +10,29 @@ module MyAge
|
|
10
10
|
method_option :dob, aliases: "d", desc: "date of birth in yyyy-mm-dd", required: true
|
11
11
|
method_option :as_of, aliases: "a", desc: "as of date in yyyy-mm-dd", required: false
|
12
12
|
def is
|
13
|
-
self.dob =
|
14
|
-
date = options[:as_of].present? ?
|
13
|
+
self.dob = get_date(options[:dob])
|
14
|
+
date = options[:as_of].present? ? get_date(options[:as_of]) : Date.today
|
15
15
|
puts self.age(date)
|
16
16
|
end
|
17
17
|
|
18
|
+
|
19
|
+
private
|
20
|
+
def get_date(date)
|
21
|
+
begin
|
22
|
+
if date =~ /^\d{4}-\d{2}-\d{2}$/
|
23
|
+
DateTime.strptime(date, "%Y-%m-%d")
|
24
|
+
else
|
25
|
+
dates = date.gsub(")", "").split("(")
|
26
|
+
if dates.length == 2
|
27
|
+
Date.today.send(dates[0].to_sym, dates[1].to_i)
|
28
|
+
else
|
29
|
+
Date.today.send(dates[0].to_sym)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
rescue NoMethodError
|
33
|
+
Date.send(date)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
18
37
|
end
|
19
38
|
end
|
data/lib/my_age/version.rb
CHANGED
data/lib/my_age.rb
CHANGED
data/my_age.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["zorro.ej@gmail.com"]
|
11
11
|
|
12
12
|
spec.summary = %q{Calculate my age.}
|
13
|
-
spec.description = %q{a simple module to be mixed in to provide an age method. Also provide a CLI. Default
|
13
|
+
spec.description = %q{a simple module to be mixed in to provide an age method. Also provide a CLI. Default calender: Gregorian, though would like to expand to other calendars too}
|
14
14
|
spec.homepage = "https://github.com/ej2015/my_age.git"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: my_age
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Edgar
|
@@ -123,7 +123,7 @@ dependencies:
|
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: 0.14.0
|
125
125
|
description: 'a simple module to be mixed in to provide an age method. Also provide
|
126
|
-
a CLI. Default
|
126
|
+
a CLI. Default calender: Gregorian, though would like to expand to other calendars
|
127
127
|
too'
|
128
128
|
email:
|
129
129
|
- zorro.ej@gmail.com
|