Module: MyAge::Calculator

Included in:
CLI
Defined in:
lib/my_age/calculator.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/my_age/calculator.rb', line 21

def method_missing(name, *args)
  if name.to_s =~ /_as_of_/
    date_method = name.to_s.split('_as_of_')[-1]
    age today.send(date_method.to_sym, *args)
  else
    super
  end
end

Class Method Details

.included(base) ⇒ Object



8
9
10
11
12
# File 'lib/my_age/calculator.rb', line 8

def self.included(base)
  def base.my_dob(col)
    define_method(:dob) { send col }
  end
end

Instance Method Details

#age(date = Date.today) ⇒ Object



15
16
17
18
19
# File 'lib/my_age/calculator.rb', line 15

def age(date = Date.today)
  return 0 if dob.blank? || date.blank? || date <= dob

  (date_to_int(date) - date_to_int(dob)) / 10_000
end