Class: Timefly

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin_time) ⇒ Timefly

initialize with either String, Time or Date instance Arguments:

origin_time: (String/Time/Date)


8
9
10
11
# File 'lib/timefly.rb', line 8

def initialize(origin_time)
  self.origin_time = origin_time
  process
end

Instance Attribute Details

#origin_timeObject

Returns the value of attribute origin_time.



3
4
5
# File 'lib/timefly.rb', line 3

def origin_time
  @origin_time
end

Instance Method Details

#age(options = {}) ⇒ Object

returns the age in years from the date of birth

Example:

>> dob = Time.new(1987,8,2)
>> Timefly.new(dob).age
=> 27
>> Timefly.new('1987.08.02').age # dob can be of format YYYY.MM.DD, YYYY-MM-DD and YYYY/MM/DD
=> 27
>> Timefly.new('1987.08.02').age({ format: '%y years, %m months' })
=> 27 years, 10 months

Arguments:

options: (Hash) { format  :(String) }
                          eg, '%y years, %m months'. %y will give years, and m will give months


27
28
29
30
31
32
33
34
35
# File 'lib/timefly.rb', line 27

def age(options = {})
  if options[:format].nil?
    years_from_origin_time
  else
    options[:format]
      .gsub(/\%y/, years_from_origin_time.to_s)
      .gsub(/\%m/, months_diff_from_origin_time_month.to_s)
  end
end

#elapsed_timeObject

returns the time elapsed in a readable format

Example:

>> Timefly.new(origin_time).time_elapsed
=> '4 hours ago'


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/timefly.rb', line 42

def elapsed_time
  if time_elapsed_in_seconds?
    elapsed_time_in_seconds
  elsif time_elapsed_in_minutes?
    elapsed_time_in_minutes
  elsif time_elapsed_in_hours?
    elapsed_time_in_hours
  elsif time_elapsed_in_days?
    elapsed_time_in_days
  elsif time_elapsed_in_weeks?
    elapsed_time_in_weeks
  elsif time_elapsed_in_months?
    elapsed_time_in_months
  else
    elapsed_time_in_years
  end
end