|
| 1 | +module IceCube |
| 2 | + class InputAlignment |
| 3 | + |
| 4 | + def initialize(rule, value, rule_part) |
| 5 | + @rule = rule |
| 6 | + @value = value |
| 7 | + @rule_part = rule_part |
| 8 | + end |
| 9 | + |
| 10 | + attr_reader :rule, :value, :rule_part |
| 11 | + |
| 12 | + def verify(freq, options={}, &block) |
| 13 | + @rule.validations[:interval] or return |
| 14 | + |
| 15 | + case @rule |
| 16 | + when DailyRule |
| 17 | + verify_wday_alignment(freq, &block) |
| 18 | + when MonthlyRule |
| 19 | + verify_month_alignment(freq, &block) |
| 20 | + else |
| 21 | + verify_freq_alignment(freq, &block) |
| 22 | + end |
| 23 | + end |
| 24 | + |
| 25 | + private |
| 26 | + |
| 27 | + def interval_validation |
| 28 | + @interval_validation ||= @rule.validations[:interval].first |
| 29 | + end |
| 30 | + |
| 31 | + def interval_value |
| 32 | + @interval_value ||= (rule_part == :interval) ? value : interval_validation.interval |
| 33 | + end |
| 34 | + |
| 35 | + def fixed_validations |
| 36 | + @fixed_validations ||= @rule.validations.values.flatten.select { |v| |
| 37 | + interval_type = (v.type == :wday ? :day : v.type) |
| 38 | + v.class < Validations::FixedValue && |
| 39 | + interval_type == rule.base_interval_validation.type |
| 40 | + } |
| 41 | + end |
| 42 | + |
| 43 | + def verify_freq_alignment(freq) |
| 44 | + interval_validation.type == freq or return |
| 45 | + (last_validation = fixed_validations.min_by(&:value)) or return |
| 46 | + |
| 47 | + alignment = (value - last_validation.value) % interval_validation.interval |
| 48 | + return if alignment.zero? |
| 49 | + |
| 50 | + validation_values = fixed_validations.map(&:value).join(', ') |
| 51 | + if rule_part == :interval |
| 52 | + message = "interval(#{value}) " \ |
| 53 | + "must be a multiple of " \ |
| 54 | + "intervals in #{last_validation.key}(#{validation_values})" |
| 55 | + else |
| 56 | + message = "intervals in #{last_validation.key}(#{validation_values}, #{value}) " \ |
| 57 | + "must be multiples of " \ |
| 58 | + "interval(#{interval_validation.interval})" |
| 59 | + end |
| 60 | + |
| 61 | + yield ArgumentError.new(message) |
| 62 | + end |
| 63 | + |
| 64 | + def verify_month_alignment(_freq) |
| 65 | + return if interval_value == 1 || (interval_value % 12).zero? |
| 66 | + return if fixed_validations.empty? |
| 67 | + |
| 68 | + message = "month_of_year can only be used with interval(1) or multiples of interval(12)" |
| 69 | + |
| 70 | + yield ArgumentError.new(message) |
| 71 | + end |
| 72 | + |
| 73 | + def verify_wday_alignment(freq) |
| 74 | + return if interval_value == 1 |
| 75 | + |
| 76 | + if freq == :wday |
| 77 | + return if (interval_value % 7).zero? |
| 78 | + return if Array(@rule.validations[:day]).empty? |
| 79 | + message = "day can only be used with multiples of interval(7)" |
| 80 | + else |
| 81 | + (fixed_validation = fixed_validations.first) or return |
| 82 | + message = "#{fixed_validation.key} can only be used with interval(1)" |
| 83 | + end |
| 84 | + |
| 85 | + yield ArgumentError.new(message) |
| 86 | + end |
| 87 | + |
| 88 | + end |
| 89 | +end |
0 commit comments