Module: AWSCronParser::Helpers::CronExpression
- Includes:
- Calendar
- Defined in:
- lib/aws_cron_parser/helpers/cron_expression.rb
Constant Summary collapse
- AWS_CRON_REGEX =
Expression type patterns
/^cron\((?<expression>.+)\)$/i- RATE_REGEX =
/^rate\((?<value>\d+)\s+(?<unit>minute|minutes|hour|hours|day|days)\)$/i- MACRO_REGEX =
/^@(?<macro>yearly|monthly|weekly|daily|hourly)$/i- AT_REGEX =
/^at\((?<datetime>.+)\)$/i- CRON_PATTERNS =
Consolidated CRON field patterns
{ digit_only: /^\d+$/, step: %r{^\*/(\d+)$|^0/(\d+)$}, range: /^(\d+)-(\d+)$/, step_range: %r{(\d+)/(\d+)}, nth_weekday_numeric: /^\d+#\d+$/, last_weekday_numeric: /^\d+L$/, nth_weekday: /^(?<weekday>\d)#(?<occurrence>\d)$/, nth_weekday_name: /^(?<name>[A-Z]{3}|[A-Z]+)#(?<occurrence>\d+)$/i, last_weekday: /^(?<weekday>\d)L$/, last_weekday_name: /^(?<name>[A-Z]{3}|[A-Z]+)L$/i }.freeze
- DIGIT_ONLY_REGEX =
Backward compatibility - individual constants for existing code
CRON_PATTERNS[:digit_only]
- RANGE_REGEX =
CRON_PATTERNS[:range]
- STEP_RANGE_REGEX =
CRON_PATTERNS[:step_range]
- NTH_WEEKDAY_REGEX =
CRON_PATTERNS[:nth_weekday]
- LAST_WEEKDAY_REGEX =
CRON_PATTERNS[:last_weekday]
Constants included from Calendar
AWSCronParser::Helpers::Calendar::AWS_DAY_NAMES, AWSCronParser::Helpers::Calendar::AWS_WEEKDAY_NAMES, AWSCronParser::Helpers::Calendar::DAY_NAMES, AWSCronParser::Helpers::Calendar::MONTH_ABBREVIATIONS, AWSCronParser::Helpers::Calendar::MONTH_FULL_NAMES, AWSCronParser::Helpers::Calendar::MONTH_NAMES, AWSCronParser::Helpers::Calendar::WEEKDAY_NAMES
Instance Method Summary collapse
-
#day_only?(source) ⇒ Boolean
Check if day-of-month field is specified and day-of-week is '?'.
-
#detect_complex_step(source) ⇒ Object
Detect if the CRON expression uses complex step patterns (with ranges or lists).
-
#detect_dom_pattern(source, pattern_char) ⇒ Object
Detect if a specific pattern character exists in the day-of-month field.
-
#detect_pattern(source, pattern_char) ⇒ Object
Detect if a specific pattern character exists in the day-of-week field.
-
#expand_dow_step(part) ⇒ Object
Expands */2, MON/2 or 2-6/2 over the AWS weekday range (1=SUN..7=SAT).
-
#month_only?(source) ⇒ Boolean
Check if month field matches actual month.
-
#normalize_month_field(month) ⇒ Object
Normalize month field (e.g., "jan", "feb-mar", "apr/2").
-
#normalize_source(source) ⇒ Object
Normalize expression by stripping cron() wrapper if present.
-
#normalize_weekday_field(weekday, is_aws: false) ⇒ Object
Normalize weekday field (e.g., "mon", "tue-wed", "fri/2").
-
#normalize_weekday_in_last_spec(dow_spec) ⇒ Object
Normalize weekday in last spec (e.g., "MONL" to "1L").
-
#normalize_weekday_in_nth_spec(dow_spec, is_aws: false) ⇒ Object
Normalize weekday in nth spec (e.g., "MON#2" to "1#2").
-
#parse_dow_field(field) ⇒ Object
Parse the day-of-week field (0 = Sunday, 1 = Monday, ..., 6 = Saturday).
-
#parse_simple_field(field, range) ⇒ Object
Parse a simple cron field (minute, hour, day, month, or weekday).
- #weekday_only?(source) ⇒ Boolean
Methods included from Calendar
#day_name, #month_abbr_to_num, #month_name, #weekday_abbr_to_num, #weekday_name_from_abbr
Instance Method Details
#day_only?(source) ⇒ Boolean
Check if day-of-month field is specified and day-of-week is '?'
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/aws_cron_parser/helpers/cron_expression.rb', line 99 def day_only?(source) tokens = source.split(/\s+/) return false if tokens.length < 5 dom_idx = tokens.length == 5 ? 2 : 3 dow_idx = tokens.length == 5 ? 4 : 5 dom = tokens[dom_idx] dow = tokens[dow_idx] dom != '?' && dow == '?' end |
#detect_complex_step(source) ⇒ Object
Detect if the CRON expression uses complex step patterns (with ranges or lists)
74 75 76 77 78 79 80 81 82 |
# File 'lib/aws_cron_parser/helpers/cron_expression.rb', line 74 def detect_complex_step(source) tokens = source.split(/\s+/) return false if tokens.length < 5 tokens.each do |field| return true if field.include?('/') && field.exclude?('#') && field.exclude?('L') end false end |
#detect_dom_pattern(source, pattern_char) ⇒ Object
Detect if a specific pattern character exists in the day-of-month field
65 66 67 68 69 70 71 |
# File 'lib/aws_cron_parser/helpers/cron_expression.rb', line 65 def detect_dom_pattern(source, pattern_char) tokens = source.split(/\s+/) return false if tokens.length < 5 dom_field = tokens.length == 5 ? tokens[2] : tokens[3] dom_field.include?(pattern_char) end |
#detect_pattern(source, pattern_char) ⇒ Object
Detect if a specific pattern character exists in the day-of-week field
56 57 58 59 60 61 62 |
# File 'lib/aws_cron_parser/helpers/cron_expression.rb', line 56 def detect_pattern(source, pattern_char) tokens = source.split(/\s+/) return false if tokens.length < 5 dow_field = tokens.length == 5 ? tokens[4] : tokens[5] dow_field.include?(pattern_char) end |
#expand_dow_step(part) ⇒ Object
Expands */2, MON/2 or 2-6/2 over the AWS weekday range (1=SUN..7=SAT). Without this the field yields no days at all and the caller searches a whole year before giving up.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/aws_cron_parser/helpers/cron_expression.rb', line 164 def (part) base, step = part.split('/') step = step.to_i return [] if step <= 0 if base.include?('-') start_num, end_num = base.split('-').map { |v| AWS_DAY_NAMES[v.upcase] || v.to_i } elsif ['*', '?'].include?(base) start_num = 1 end_num = 7 else start_num = AWS_DAY_NAMES[base.upcase] || base.to_i end_num = 7 end (start_num..end_num).step(step).to_a end |
#month_only?(source) ⇒ Boolean
Check if month field matches actual month
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/aws_cron_parser/helpers/cron_expression.rb', line 85 def month_only?(source) tokens = source.split(/\s+/) return false if tokens.length < 5 dom_idx = tokens.length == 5 ? 2 : 3 dow_idx = tokens.length == 5 ? 4 : 5 dom = tokens[dom_idx] dow = tokens[dow_idx] ['*', '?'].include?(dom) && dow != '*' && dow != '?' && dow.exclude?('#') && dow.exclude?('L') end |
#normalize_month_field(month) ⇒ Object
Normalize month field (e.g., "jan", "feb-mar", "apr/2")
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/aws_cron_parser/helpers/cron_expression.rb', line 235 def normalize_month_field(month) return month if ['*', '?'].include?(month) || month =~ /^\d/ # Handle ranges like "jan-mar" if month.include?('-') parts = month.split('-') start_num = month_abbr_to_num(parts[0]) end_num = month_abbr_to_num(parts[1]) return "#{start_num}-#{end_num}" end # Handle lists like "jan,mar,jun" if month.include?(',') parts = month.split(',').map { |m| month_abbr_to_num(m) } return parts.join(',') end # Handle steps like "jan/2" and "*/2" - the wildcard base is not a name if month.include?('/') parts = month.split('/') start = ['*', '?'].include?(parts[0]) ? parts[0] : month_abbr_to_num(parts[0]) return "#{start}/#{parts[1]}" end # Single month abbreviation month_abbr_to_num(month).to_s end |
#normalize_source(source) ⇒ Object
Normalize expression by stripping cron() wrapper if present
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/aws_cron_parser/helpers/cron_expression.rb', line 36 def normalize_source(source) return source if source.blank? normalized = source.strip if (match = normalized.match(AWS_CRON_REGEX)) content = match[:expression].strip parts = content.split(/\s+/) normalized = if parts.length == 7 parts[0..5].join(' ') elsif parts.length == 6 parts[0..4].join(' ') else parts.join(' ') end end normalized end |
#normalize_weekday_field(weekday, is_aws: false) ⇒ Object
Normalize weekday field (e.g., "mon", "tue-wed", "fri/2")
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/aws_cron_parser/helpers/cron_expression.rb', line 195 def normalize_weekday_field(weekday, is_aws: false) return weekday if ['*', '?'].include?(weekday) || weekday =~ /^\d/ # Handle ranges like "mon-wed" if weekday.include?('-') parts = weekday.split('-') start_num = weekday_abbr_to_num(parts[0], is_aws: is_aws) end_num = weekday_abbr_to_num(parts[1], is_aws: is_aws) return "#{start_num}-#{end_num}" end # Handle lists like "mon,wed,fri" if weekday.include?(',') parts = weekday.split(',').map { |w| weekday_abbr_to_num(w, is_aws: is_aws) } return parts.join(',') end # Handle steps like "mon/2" and "*/2" - the wildcard base is not a name if weekday.include?('/') parts = weekday.split('/') start = ['*', '?'].include?(parts[0]) ? parts[0] : weekday_abbr_to_num(parts[0], is_aws: is_aws) return "#{start}/#{parts[1]}" end # Single weekday abbreviation weekday_abbr_to_num(weekday, is_aws: is_aws).to_s end |
#normalize_weekday_in_last_spec(dow_spec) ⇒ Object
Normalize weekday in last spec (e.g., "MONL" to "1L")
224 225 226 227 228 229 230 231 232 |
# File 'lib/aws_cron_parser/helpers/cron_expression.rb', line 224 def normalize_weekday_in_last_spec(dow_spec) return dow_spec if dow_spec.match?(CRON_PATTERNS[:last_weekday_numeric]) match = dow_spec.match(CRON_PATTERNS[:last_weekday_name]) return dow_spec unless match day_num = AWS_DAY_NAMES[match[:name].upcase] || match[:name] "#{day_num}L" end |
#normalize_weekday_in_nth_spec(dow_spec, is_aws: false) ⇒ Object
Normalize weekday in nth spec (e.g., "MON#2" to "1#2")
183 184 185 186 187 188 189 190 191 192 |
# File 'lib/aws_cron_parser/helpers/cron_expression.rb', line 183 def normalize_weekday_in_nth_spec(dow_spec, is_aws: false) return dow_spec if dow_spec.match?(CRON_PATTERNS[:nth_weekday_numeric]) match = dow_spec.match(CRON_PATTERNS[:nth_weekday_name]) return dow_spec unless match day_names = is_aws ? AWS_DAY_NAMES : DAY_NAMES day_num = day_names[match[:name].upcase] || match[:name] "#{day_num}##{match[:occurrence]}" end |
#parse_dow_field(field) ⇒ Object
Parse the day-of-week field (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/aws_cron_parser/helpers/cron_expression.rb', line 139 def parse_dow_field(field) return (0..6).to_a if ['*', '?'].include?(field) result = [] field.split(',').each do |part| part = part.strip if part.include?('/') result.concat((part)) elsif part.include?('-') start_num, end_num = part.split('-') start_num = AWS_DAY_NAMES[start_num.upcase] || start_num.to_i end_num = AWS_DAY_NAMES[end_num.upcase] || end_num.to_i (start_num..end_num).each { |n| result << n } elsif part.match?(/^\d+$/) result << part.to_i elsif AWS_DAY_NAMES[part.upcase] result << AWS_DAY_NAMES[part.upcase] end end result.sort.uniq end |
#parse_simple_field(field, range) ⇒ Object
Parse a simple cron field (minute, hour, day, month, or weekday)
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/aws_cron_parser/helpers/cron_expression.rb', line 113 def parse_simple_field(field, range) return range.to_a if ['*', '?'].include?(field) return [field.to_i] if field.match?(/^\d+$/) if field.include?('-') start_num, end_num = field.split('-').map(&:to_i) (start_num..end_num).to_a elsif field.include?(',') field.split(',').map(&:to_i) elsif field.include?('/') start_str, step_str = field.split('/') start = start_str == '*' ? range.first : start_str.to_i step = step_str.to_i result = [] current = start while current <= range.last result << current current += step end result else [field.to_i] end end |
#weekday_only?(source) ⇒ Boolean
263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/aws_cron_parser/helpers/cron_expression.rb', line 263 def weekday_only?(source) tokens = normalize_source(source).split(/\s+/) return false if tokens.length < 5 dom_idx = tokens.length == 5 ? 2 : 3 dow_idx = tokens.length == 5 ? 4 : 5 dom = tokens[dom_idx] dow = tokens[dow_idx] dom == '?' && dow != '?' && dow.exclude?('#') && dow.exclude?('L') end |