Module: Crony::TimePresenter

Defined in:
lib/crony/presenters/time_presenter.rb

Class Method Summary collapse

Class Method Details

.parse(minute, hour) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/crony/presenters/time_presenter.rb', line 3

def self.parse(minute, hour)
  minute = Formatters::MinuteFormatter.new(Parser.parse(minute))
  hour = Formatters::HourFormatter.new(Parser.parse(hour))

  case [minute, hour].map(&:sym).join('')
  when /[ev]e/ then minute.format
  when 're' then "every hour from xx:#{minute.start.two_digits} to xx:#{minute.stop.two_digits}"
  when 'ss' then "#{time(hour.single_element, minute.single_element)}"
  when 'sc' then "#{hour.collection.map{|h| time(h, minute.single_element)}.to_sentence}"
  when 'us' then "#{minute.format(false)} between #{time_range(hour.single_element, minute.start, hour.single_element, 59)}"
  when 'uc' then "#{minute.format(false)} between #{hour.collection.map{|h| time_range(h, minute.start, h, 59)}.to_sentence}"
  when /c[cs]/ then "#{hour.collection.map{|h| minute.collection.map{|m| time(h, m)}}.flatten.to_sentence}"
  when /[ucsfr]r/ then ("%s of every hour between %s" % [minute.format, hour.format]).squeeze(' ')
  when /^[fr][cs]$/ then "#{minute.format(hour.collection)}"
  when /^.[rsc]$/ then ("%s between %s" % [minute.format, hour.format]).squeeze(' ')
  else ("%s of %s" % [minute.format, hour.format]).squeeze(' ')
  end
end

.time(hour, minute) ⇒ Object



22
23
24
# File 'lib/crony/presenters/time_presenter.rb', line 22

def self.time(hour, minute)
  [hour, minute].map(&:two_digits).join(':')
end

.time_range(s_hour, s_minute, e_hour, e_minute) ⇒ Object



26
27
28
# File 'lib/crony/presenters/time_presenter.rb', line 26

def self.time_range(s_hour, s_minute, e_hour, e_minute)
  "#{time(s_hour, s_minute)} and #{time(e_hour, e_minute)}"
end