Class: Whenever::NumericSeconds

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

Constant Summary collapse

PATTERN =
/(\d+)\.(seconds?|minutes?|hours?|days?|weeks?|months?|years?)/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number) ⇒ NumericSeconds

Returns a new instance of NumericSeconds



15
16
17
# File 'lib/whenever/numeric_seconds.rb', line 15

def initialize(number)
  @number = number.to_i
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number



5
6
7
# File 'lib/whenever/numeric_seconds.rb', line 5

def number
  @number
end

Class Method Details

.process_string(string) ⇒ Object



11
12
13
# File 'lib/whenever/numeric_seconds.rb', line 11

def self.process_string(string)
  string.gsub(PATTERN) { Whenever.seconds($1, $2) }
end

.seconds(number, units) ⇒ Object



7
8
9
# File 'lib/whenever/numeric_seconds.rb', line 7

def self.seconds(number, units)
  new(number).send(units)
end

Instance Method Details

#daysObject Also known as: day



34
35
36
# File 'lib/whenever/numeric_seconds.rb', line 34

def days
  number * 86_400
end

#hoursObject Also known as: hour



29
30
31
# File 'lib/whenever/numeric_seconds.rb', line 29

def hours
  number * 3_600
end

#minutesObject Also known as: minute



24
25
26
# File 'lib/whenever/numeric_seconds.rb', line 24

def minutes
  number * 60
end

#monthsObject Also known as: month



44
45
46
# File 'lib/whenever/numeric_seconds.rb', line 44

def months
  number * 2_592_000
end

#secondsObject Also known as: second



19
20
21
# File 'lib/whenever/numeric_seconds.rb', line 19

def seconds
  number
end

#weeksObject Also known as: week



39
40
41
# File 'lib/whenever/numeric_seconds.rb', line 39

def weeks
  number * 604_800
end

#yearsObject Also known as: year



49
50
51
# File 'lib/whenever/numeric_seconds.rb', line 49

def years
  number * 31_557_600
end