Class: Mercy::PeriodParser

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(period) ⇒ PeriodParser

Returns a new instance of PeriodParser.



11
12
13
# File 'lib/mercy/period_parser.rb', line 11

def initialize(period)
  @period = period
end

Class Method Details

.parse(period) ⇒ Object



4
5
6
7
8
# File 'lib/mercy/period_parser.rb', line 4

def parse(period)
  return unless period
  return period if period.is_a?(Integer)
  new(period).to_seconds
end

Instance Method Details

#to_secondsObject



15
16
17
18
# File 'lib/mercy/period_parser.rb', line 15

def to_seconds
  return unless period.match(/^\d+(years|months|days|hours|minutes|seconds)?/)
  type.blank? ? value.seconds : value.public_send(type)
end