class CronSpeak

Attributes

to_s[R]

Public Class Methods

new(s) click to toggle source
# File lib/cronspeak.rb, line 24
def initialize(s)

  @count = 0
  m, h, d, mon, raw_dow = s.scan(/([\d,\*-\/]+)\s*/).flatten(1)
  @dow = dow(raw_dow)

  @to_s = if m+h =~ /^\d+$/ then
    [Time.parse(h + ':' + m)\
                   .strftime("%l:%M%P ").lstrip, days(d), months(mon)].join
  else
    [mins(m), hrs(h), days(d), months(mon)].join
  end

end

Private Instance Methods

days(d='*') click to toggle source
# File lib/cronspeak.rb, line 80
def days(d='*') 

  return " %s in " % @dow if @count >= 8
    
  return case d
  when /^\*$/ then ' every day'
  when/^\*\//
    @count += 4
    " of every %s day" % [d[/^\*\/(\d+)/,1].to_i.ordinal] 
  when /,/
    @count += 8
    "the %s of " % d.split(/,/).map {|x| x.to_i.ordinal }.join(' and ')
  when /\-/
    @count += 8
    " the %s " % d.split('-',2).map {|x| x.to_i.ordinal }.join(' through ')
  else
    @count += 4
    " the %s" % [d.to_i.ordinal]
  end

end
dow(raw_dow='*') click to toggle source
# File lib/cronspeak.rb, line 131
def dow(raw_dow='*')
 
  case raw_dow
  when /^\*$/ then ''
  when /,/
    @count += 8
    raw_dow.split(/,/).map {|x| Date::DAYNAMES[x.to_i] + 's' }.join(' and ')
  when /\-/
    @count += 8
    raw_dow.split('-',2).map {|x| Date::DAYNAMES[x.to_i] + 's' }\
                                                          .join(' through ')
  else
    @count += 8
    Date::DAYNAMES[raw_dow.to_i] + 's'
  end

end
hrs(h='*') click to toggle source
# File lib/cronspeak.rb, line 58
def hrs(h='*')

  return 'every hour' if h == '*'

  @count += 2    

  return case h  
  when /^\*\// then " of every %s hour" % [h[/^\*\/(\d+)/,1].to_i.ordinal]
  when /,/
    h.split(/,/)\
      .map {|x| Time.parse(x+':00').strftime("%l%P").lstrip }.join(' and ')
  when /,/
    "%s of " % h.split(/,/).map {|x| x.to_i.ordinal }.join(' and ')
  when /\-/
    h.split('-',2).map {|x| Time.parse(x+':00').strftime("%l%P").lstrip }\
                                                          .join(' through ')
  else
    [Time.parse(h+':00').strftime("%l%P").lstrip]
  end
  
end
mins(m='*') click to toggle source
# File lib/cronspeak.rb, line 41
def mins(m='*')
    
  case m
  when /^\*$/ then 'every minute of '
  when /^\*\//
    "every %s minute of " % [m[/^\*\/(\d+)/,1].to_i.ordinal]
  when /,/
    "%s minutes of " % m.split(/,/).map {|x| x.to_i.ordinal }.join(' and ')
  when /\-/
    "the %s minutes of " % m.split('-',2)\
                               .map {|x| x.to_i.ordinal }.join(' through ')
  else
    "%s minute of " % [m.to_i.ordinal]
  end

end
months(mon='*') click to toggle source
# File lib/cronspeak.rb, line 102
def months(mon='*') 

  r = case mon
  when /^\*$/

    if @count < 4 then
      '' 
    else
      s = @count < 8 ? ' of ' : ''
      s + 'every month'
    end

  when /^\*\// then " of every %s month" % [mon[/^\*\/(\d+)/,1].to_i.ordinal]
  when /,/
    " of %s" % mon.split(/,/)\
                      .map {|x| Date::MONTHNAMES[x.to_i] }.join(' and ')  
  when /\-/
    " of %s" % mon.split('-',2).map {|x| Date::MONTHNAMES[x.to_i] }\
                                                        .join(' through ')
  else
    s = @count < 8 ? 'of ' : ''
    " %s%s" % [s, Date::MONTHNAMES[mon.to_i]]
  end

  @count += 16

  return r
end