Class: CronExpressionGenerator
- Inherits:
-
Object
- Object
- CronExpressionGenerator
- Defined in:
- lib/cron_expression_generator.rb
Class Method Summary collapse
- .carry_up_day_cron(last_day_of_month) ⇒ Object
- .carry_up_hour_cron ⇒ Object
- .carry_up_minutes_cron ⇒ Object
- .carry_up_months_cron ⇒ Object
- .carry_up_time(time:, unit:) ⇒ Object
- .day_is_carried_up? ⇒ Boolean
- .day_is_matched? ⇒ Boolean
- .generate(start_time:, end_time:, interval_minutes:) ⇒ Object
- .get_largest_datetime_unit_diff ⇒ Object
- .hour_is_carried_up? ⇒ Boolean
- .hour_is_matched? ⇒ Boolean
- .initialize ⇒ Object
- .match_day_cron ⇒ Object
- .match_hour_cron ⇒ Object
- .match_largest_datetime_unit ⇒ Object
- .match_minutes_cron ⇒ Object
- .match_months_cron ⇒ Object
- .match_remaining_datetime ⇒ Object
- .minutes_is_carried_up? ⇒ Boolean
- .minutes_is_matched? ⇒ Boolean
- .months_is_carried_up? ⇒ Boolean
- .months_is_matched? ⇒ Boolean
- .validate_parameters ⇒ Object
Class Method Details
.carry_up_day_cron(last_day_of_month) ⇒ Object
65 66 67 |
# File 'lib/cron_expression_generator.rb', line 65 def self.carry_up_day_cron(last_day_of_month) "*/#{@interval_minutes} * #{@count_up_time.day}-#{last_day_of_month} #{@count_up_time.month} *" end |
.carry_up_hour_cron ⇒ Object
61 62 63 |
# File 'lib/cron_expression_generator.rb', line 61 def self.carry_up_hour_cron "*/#{@interval_minutes} #{@count_up_time.hour}-23 #{@count_up_time.day} #{@count_up_time.month} *" end |
.carry_up_minutes_cron ⇒ Object
57 58 59 |
# File 'lib/cron_expression_generator.rb', line 57 def self.carry_up_minutes_cron "#{@count_up_time.min}-59/#{@interval_minutes} #{@count_up_time.hour} #{@count_up_time.day} #{@count_up_time.month} *" end |
.carry_up_months_cron ⇒ Object
69 70 71 |
# File 'lib/cron_expression_generator.rb', line 69 def self.carry_up_months_cron "*/#{@interval_minutes} * * #{@count_up_time.month}-12 *" end |
.carry_up_time(time:, unit:) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/cron_expression_generator.rb', line 139 def self.carry_up_time(time:, unit:) if unit == @min @count_up_time += time.minute elsif unit == @hour @count_up_time += time.hour elsif unit == @day @count_up_time += time.day elsif unit == @month @count_up_time += time.month else = "Unexpected value passed to received to unit.\n" += "given, unit: #{unit}" raise ArgumentError, end end |
.day_is_carried_up? ⇒ Boolean
115 116 117 |
# File 'lib/cron_expression_generator.rb', line 115 def self.day_is_carried_up? @datetime_unit_to_match != @min && @datetime_unit_to_match != @hour && @datetime_unit_to_match != @day && @count_up_time.day != 1 end |
.day_is_matched? ⇒ Boolean
131 132 133 |
# File 'lib/cron_expression_generator.rb', line 131 def self.day_is_matched? @count_up_time.day == @terminate_time.day end |
.generate(start_time:, end_time:, interval_minutes:) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/cron_expression_generator.rb', line 6 def self.generate(start_time:, end_time:, interval_minutes:) @start_time = start_time @end_time = end_time @interval_minutes = interval_minutes self.validate_parameters self.initialize cron_expressions = [] if @count_up_time == @terminate_time return cron_expressions # return empty if no time difference end @datetime_unit_to_match = self.get_largest_datetime_unit_diff cron_expressions = self.match_largest_datetime_unit cron_expressions += self.match_remaining_datetime cron_expressions end |
.get_largest_datetime_unit_diff ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/cron_expression_generator.rb', line 89 def self.get_largest_datetime_unit_diff if @count_up_time.year != @terminate_time.year @year elsif @count_up_time.month != @terminate_time.month @month elsif @count_up_time.day != @terminate_time.day @day elsif @count_up_time.hour != @terminate_time.hour @hour elsif @count_up_time.min != @terminate_time.min @min else = "Unexpected value received to time_at & terminate_time_at.\n" += "given, count_up_time: #{@count_up_time}, terminate_time: #{@terminate_time}" raise ArgumentError, end end |
.hour_is_carried_up? ⇒ Boolean
111 112 113 |
# File 'lib/cron_expression_generator.rb', line 111 def self.hour_is_carried_up? @datetime_unit_to_match != @min && @datetime_unit_to_match != @hour && @count_up_time.hour != 0 end |
.hour_is_matched? ⇒ Boolean
127 128 129 |
# File 'lib/cron_expression_generator.rb', line 127 def self.hour_is_matched? @count_up_time.hour == @terminate_time.hour end |
.initialize ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/cron_expression_generator.rb', line 26 def self.initialize if @start_time > @end_time = "start_time is larger than end_time.\n" += "given, start_time: #{@start_time}, end_time: #{@end_time}" raise ArgumentError, end @min = 'min' @hour = 'hour' @day = 'day' @month = 'month' @year = 'year' @count_up_time = @start_time @terminate_time = @end_time @interval_minutes = @interval_minutes end |
.match_day_cron ⇒ Object
81 82 83 |
# File 'lib/cron_expression_generator.rb', line 81 def self.match_day_cron "*/#{@interval_minutes} * #{@count_up_time.day}-#{@terminate_time.day.to_i - 1} #{@terminate_time.month.to_i} *" end |
.match_hour_cron ⇒ Object
77 78 79 |
# File 'lib/cron_expression_generator.rb', line 77 def self.match_hour_cron "*/#{@interval_minutes} #{@count_up_time.hour}-#{@terminate_time.hour.to_i - 1} #{@terminate_time.day} #{@terminate_time.month} *" end |
.match_largest_datetime_unit ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/cron_expression_generator.rb', line 155 def self.match_largest_datetime_unit cron_expressions = [] # minutes if self.minutes_is_carried_up? cron_expressions.append(self.carry_up_minutes_cron) remaining_minutes = 60 - @count_up_time.min.to_i self.carry_up_time(time: remaining_minutes, unit: @min) end # hour if self.hour_is_carried_up? cron_expressions.append(self.carry_up_hour_cron) remaining_hours = 24 - @count_up_time.hour.to_i self.carry_up_time(time: remaining_hours, unit: @hour) end # day if self.day_is_carried_up? last_day_of_month = @count_up_time.end_of_month.strftime('%d').to_i cron_expressions.append(self.carry_up_day_cron(last_day_of_month)) remaining_days = last_day_of_month - @count_up_time.day.to_i + 1 self.carry_up_time(time: remaining_days, unit: @day) end # months if self.months_is_carried_up? cron_expressions.append(self.carry_up_months_cron) remaining_months = 12 - @count_up_time.month.to_i + 1 self.carry_up_time(time: remaining_months, unit: @month) end cron_expressions end |
.match_minutes_cron ⇒ Object
73 74 75 |
# File 'lib/cron_expression_generator.rb', line 73 def self.match_minutes_cron "#{@count_up_time.min}-#{@terminate_time.min}/#{@interval_minutes} #{@terminate_time.hour} #{@terminate_time.day} #{@terminate_time.month} *" end |
.match_months_cron ⇒ Object
85 86 87 |
# File 'lib/cron_expression_generator.rb', line 85 def self.match_months_cron "*/#{@interval_minutes} * * #{@count_up_time.month}-#{@terminate_time.month.to_i - 1} *" end |
.match_remaining_datetime ⇒ Object
187 188 189 190 191 192 193 194 |
# File 'lib/cron_expression_generator.rb', line 187 def self.match_remaining_datetime cron_expressions = [] cron_expressions.append(self.match_months_cron) unless self.months_is_matched? cron_expressions.append(self.match_day_cron) unless self.day_is_matched? cron_expressions.append(self.match_hour_cron) unless self.hour_is_matched? cron_expressions.append(self.match_minutes_cron) unless self.minutes_is_matched? cron_expressions end |
.minutes_is_carried_up? ⇒ Boolean
107 108 109 |
# File 'lib/cron_expression_generator.rb', line 107 def self.minutes_is_carried_up? @datetime_unit_to_match != @min && @count_up_time.min != 0 end |
.minutes_is_matched? ⇒ Boolean
123 124 125 |
# File 'lib/cron_expression_generator.rb', line 123 def self.minutes_is_matched? @count_up_time.min == @terminate_time.min end |
.months_is_carried_up? ⇒ Boolean
119 120 121 |
# File 'lib/cron_expression_generator.rb', line 119 def self.months_is_carried_up? @datetime_unit_to_match != @min && @datetime_unit_to_match != @hour && @datetime_unit_to_match != @day && @datetime_unit_to_match != @month && @count_up_time.month != 1 end |
.months_is_matched? ⇒ Boolean
135 136 137 |
# File 'lib/cron_expression_generator.rb', line 135 def self.months_is_matched? @count_up_time.month == @terminate_time.month end |
.validate_parameters ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/cron_expression_generator.rb', line 44 def self.validate_parameters if @start_time.nil? || @end_time.nil? || @interval_minutes.nil? raise "Parameters are not satisfied.\nCommand in this format: $ cron_expression_generator <start_datetime> <end_datetime> <interval_minutes> \nCommand is like this:\n$ cron_expression_generator \"2022-12-31 00:000\" \"2023-01-01 23:59\" 5" end begin @start_time = DateTime.parse(@start_time) if @start_time.is_a?(String) @end_time = DateTime.parse(@end_time) if @end_time.is_a?(String) rescue raise "Invalid given date format in String. Please pass the string in this format: YYYY-MM-DD HH:MM" end end |