my_age 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 492f7c70dfd61156d3f1a550c5b0bf06f2361f8a
4
- data.tar.gz: 760dc167e872eb052b582c92c1b42d67769ad53b
3
+ metadata.gz: 94e8609fc1e040a51451599b8c1991d2c1d92ac2
4
+ data.tar.gz: e2136277e36b0a95e274be82e95ff25049325680
5
5
  SHA512:
6
- metadata.gz: d6bd761b63baaef480ca4a017615cf7c98aab548edc216ee2a0623366e89ad62e41c4b0d07b60945d96bc91580314fb6c4dbb4221ecab624a5e043ca94bdd006
7
- data.tar.gz: 84d4494a3e8f76b4abce25017be4650ebbe6202b5323d766feb45c8af13c6d6fb88ac9ef4f0c013bb36898e275fa4f80e16a32d49a48e5757ccedf67e0dc9977
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 calculator module
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::Calculator
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
- ## age as of today
32
+ ## but you can supply a specific date
32
33
  user.age(date)
33
- ## age as of date
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
- Use you own dob attribute
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::Calculator
44
- my_dob :date_of_birth
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
- $ #=> age as of tomorrow
74
+ $ my_age is -d 'years_ago(5)' -a tomorrow ##=> 5
61
75
 
62
76
  ```
63
77
 
@@ -2,7 +2,6 @@ require "my_age/helper"
2
2
 
3
3
  module MyAge
4
4
  module Calculator
5
- include MyAge::Helper
6
5
 
7
6
  unless respond_to?(:dob)
8
7
  def self.included(base)
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 = DateTime.strptime(options[:dob], "%Y-%m-%d")
14
- date = options[:as_of].present? ? DateTime.strptime(options[:as_of], "%Y-%m-%d") : Date.today
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
@@ -1,3 +1,3 @@
1
1
  module MyAge
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/my_age.rb CHANGED
@@ -5,5 +5,9 @@ require "my_age/calculator"
5
5
  require "my_age/helper"
6
6
 
7
7
  module MyAge
8
+ def self.included(base)
9
+ base.include MyAge::Calculator
10
+ base.include MyAge::Helper
11
+ end
8
12
 
9
13
  end
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 calendr: Gregorian. Though would like to expand to other calendars too}
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.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 calendr: Gregorian. Though would like to expand to other calendars
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