Module: Cronex::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/cronex/utils.rb

Instance Method Summary collapse

Instance Method Details

#day_of_week_name(number) ⇒ Object



23
24
25
# File 'lib/cronex/utils.rb', line 23

def day_of_week_name(number)
  Date::DAYNAMES[number.to_i % 7]
end

#format_minutes(minute_expression) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/cronex/utils.rb', line 27

def format_minutes(minute_expression)
  if minute_expression.include?(',')
    minute_expression.split(',').map { |m| format('%02d', integer(m)) }.join(',')
  else
    format('%02d', integer(minute_expression))
  end
end

#format_time(hour_expression, minute_expression, second_expression = '') ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cronex/utils.rb', line 35

def format_time(hour_expression, minute_expression, second_expression = '')
  hour = integer(hour_expression)
  period = hour >= 12 ? 'PM' : 'AM'
  hour -= 12 if hour > 12
  minute = integer(minute_expression)
  minute = format('%02d', minute)
  second = ''
  if present?(second_expression)
    second = integer(second_expression)
    second = ':' + format('%02d', second)
  end
  format('%s:%s%s %s', hour, minute, second, period)
end

#include_any?(str, chars) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/cronex/utils.rb', line 9

def include_any?(str, chars)
  chars.any? { |char| str.include?(char) }
end

#integer(str) ⇒ Object



17
18
19
20
21
# File 'lib/cronex/utils.rb', line 17

def integer(str)
  # strip leading zeros in numbers to prevent '08', '09'
  # from being treated as invalid octals
  Integer(str.sub(/^0*(\d+)/, '\1'))
end

#number?(str) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/cronex/utils.rb', line 13

def number?(str)
  integer(str) rescue nil
end

#present?(str) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/cronex/utils.rb', line 5

def present?(str)
  !str.to_s.strip.empty?
end