Class: TimePup::IncrementalTime

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

Overview

Parses time with the following keywords; year y month mo week w day d hour h minute m

Constant Summary collapse

MATCHERS =
{
  years:    /(\d{1,2})ye?a?r?s?/,
  months:   /(\d{1,2})mo[a-zA-Z]*/,
  weeks:    /(\d{1,2})we?e?k?s?/,
  days:     /(\d{1,2}(\.\d{1,2})?)da?y?s?/,
  hours:    /(\d{1,2}(\.\d{1,2})?)ho?u?r?s?/,
  minutes:  /(\d{1,2})(?!(mo|mar))m[a-zA-Z]*/
}

Class Method Summary collapse

Class Method Details

.match_and_return_time(time) ⇒ Object



26
27
28
29
30
# File 'lib/time_pup/incremental_time.rb', line 26

def match_and_return_time(time)
  MATCHERS.keys.inject(0.seconds) do |result, m|
    result += scan_increments(time, m)
  end
end

.parse(time) ⇒ Object



21
22
23
24
# File 'lib/time_pup/incremental_time.rb', line 21

def parse(time)
  increment = match_and_return_time(time.downcase)
  increment == 0.seconds ? nil : increment.from_now
end

.scan_increments(time, unit) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/time_pup/incremental_time.rb', line 32

def scan_increments(time, unit)
  increments = time.scan MATCHERS[unit]
  if increments.empty?
    0.seconds
  else
    if unit == :hours || unit == :days
      increments.first[0].to_f.send(unit.to_sym)
    else
      increments.first[0].to_i.send(unit.to_sym)
    end
  end
end