Class: Pueri::Age

Inherits:
Object
  • Object
show all
Defined in:
lib/pueri/age.rb

Overview

Parses a given date string into and Array of age. The array contains the years, months and days of life of the one who was/would be born on the given day.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(date) ⇒ Age

Creates an Age instance.

Parameters:

  • date (String)

    A date current or on the past - NOT on the future.



13
14
15
16
# File 'lib/pueri/age.rb', line 13

def initialize(date)
  @days_month = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  @age = parse_age(date)
end

Instance Attribute Details

#ageObject (readonly)

Returns the value of attribute age.



8
9
10
# File 'lib/pueri/age.rb', line 8

def age
  @age
end

#days_monthObject (readonly)

Returns the value of attribute days_month.



8
9
10
# File 'lib/pueri/age.rb', line 8

def days_month
  @days_month
end

Instance Method Details

#to_fFloat

Retrieves the age in days.

Returns:

  • (Float)

    The age in days, rounded to the second decimal place.



34
35
36
37
38
39
# File 'lib/pueri/age.rb', line 34

def to_f
  a = age[0].to_f * 365.25
  a += age[1].to_f * 30.0
  a += age[2].to_f
  a.round(2)
end

#to_s(short = false) ⇒ String

Retrieves the age on a readable format.

Parameters:

  • short (Booleann) (defaults to: false)

    Whether to output a short-format age or not. Defaults to long format.

Returns:

  • (String)

    The age in a readable format, either short or long.



23
24
25
26
27
28
29
# File 'lib/pueri/age.rb', line 23

def to_s(short = false)
  if short
    short_string
  else
    long_string
  end
end