Class: Colonel::Parser::TimeParser

Inherits:
Object
  • Object
show all
Defined in:
lib/colonel/parser.rb

Overview

Class to parse time fields from crontab line.

Constant Summary collapse

DIGIT =
/\d{1,2}/
ALL =
/\*/
COMMA =
/\,/
DASH =
/\-/
RANGE =
/#{DIGIT}#{DASH}#{DIGIT}/
DEVIDE =
/#{ALL}\/#{DIGIT}|#{RANGE}\/#{DIGIT}/
DEVIDER =
/\//

Instance Method Summary collapse

Constructor Details

#initialize(time_expression, time_type, all_flag = true) ⇒ TimeParser

if all_flag is true -> return :all result if all?



125
126
127
128
129
# File 'lib/colonel/parser.rb', line 125

def initialize(time_expression, time_type, all_flag=true)
  @time_type = time_type
  @tokens = time_expression.scan(/#{COMMA}|#{DEVIDE}|#{RANGE}|#{DIGIT}|#{ALL}/)
  @all_flag = all_flag
end

Instance Method Details

#astObject



131
132
133
# File 'lib/colonel/parser.rb', line 131

def ast
  @_ast ||= expression
end

#max_time_valueObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/colonel/parser.rb', line 160

def max_time_value
  @_max_time_value ||= case @time_type
                         when :minute
                           59
                         when :hour
                           23
                         when :day
                           31
                         when :month
                           12
                         when :weekday
                           6
                       end
end

#min_time_valueObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/colonel/parser.rb', line 145

def min_time_value
  @_min_time_value ||= case @time_type
                         when :minute
                           0
                         when :hour
                           0
                         when :day
                           1
                         when :month
                           1
                         when :weekday
                           0
                       end
end

#resultObject



135
136
137
138
139
140
141
142
143
# File 'lib/colonel/parser.rb', line 135

def result
  unique = ast.flatten.uniq
  unique.pop if unique.last.nil? #remove nil element
  if all?(unique) && @all_flag
    :all
  else
    unique
  end
end

#tokensObject



120
121
122
# File 'lib/colonel/parser.rb', line 120

def tokens
  @tokens
end