class IceCube::ValidatedRule
Constants
- VALIDATION_ORDER
Validations ordered for efficiency in sequence of:
-
descending intervals
-
boundary limits
-
base values by cardinality (n = 60, 60, 31, 24, 12, 7)
-
locks by cardinality (n = 365, 60, 60, 31, 24, 12, 7)
-
interval multiplier
-
Attributes
validations[R]
Public Class Methods
new(interval = 1, *)
click to toggle source
# File lib/ice_cube/validated_rule.rb, line 35 def initialize(interval = 1, *) @validations = Hash.new end
Public Instance Methods
base_interval_type()
click to toggle source
# File lib/ice_cube/validated_rule.rb, line 47 def base_interval_type base_interval_validation.type end
base_interval_validation()
click to toggle source
# File lib/ice_cube/validated_rule.rb, line 39 def base_interval_validation @validations[:interval].first end
clobber_base_validations(*types)
click to toggle source
Remove the specified base validations
# File lib/ice_cube/validated_rule.rb, line 116 def clobber_base_validations(*types) types.each do |type| @validations.delete(:"base_#{type}") end end
dst_adjust?()
click to toggle source
# File lib/ice_cube/validated_rule.rb, line 67 def dst_adjust? @validations[:interval].any? &:dst_adjust? end
next_time(time, schedule, closing_time)
click to toggle source
Compute the next time after (or including) the specified time in respect to the given schedule
# File lib/ice_cube/validated_rule.rb, line 53 def next_time(time, schedule, closing_time) @time = time @schedule = schedule return nil unless find_acceptable_time_before(closing_time) @uses += 1 if @time @time end
other_interval_validations()
click to toggle source
# File lib/ice_cube/validated_rule.rb, line 43 def other_interval_validations Array(@validations[base_interval_validation.type]) end
replace_validations_for(key, arr)
click to toggle source
Fully replace validations
# File lib/ice_cube/validated_rule.rb, line 107 def replace_validations_for(key, arr) if arr.nil? @validations.delete(key) else @validations[key] = arr end end
skipped_for_dst()
click to toggle source
# File lib/ice_cube/validated_rule.rb, line 63 def skipped_for_dst @uses -= 1 if @uses > 0 end
to_hash()
click to toggle source
# File lib/ice_cube/validated_rule.rb, line 81 def to_hash builder = HashBuilder.new(self) @validations.each do |name, validations| validations.each do |validation| validation.build_hash(builder) end end builder.to_hash end
to_ical()
click to toggle source
# File lib/ice_cube/validated_rule.rb, line 91 def to_ical builder = IcalBuilder.new @validations.each do |name, validations| validations.each do |validation| validation.build_ical(builder) end end builder.to_s end
to_s()
click to toggle source
# File lib/ice_cube/validated_rule.rb, line 71 def to_s builder = StringBuilder.new @validations.each do |name, validations| validations.each do |validation| validation.build_s(builder) end end builder.to_s end
validations_for(key)
click to toggle source
Get the collection that contains validations of a certain type
# File lib/ice_cube/validated_rule.rb, line 102 def validations_for(key) @validations[key] ||= [] end
Private Instance Methods
find_acceptable_time_before(boundary)
click to toggle source
# File lib/ice_cube/validated_rule.rb, line 136 def find_acceptable_time_before(boundary) until finds_acceptable_time? return false if past_closing_time?(boundary) end true end
finds_acceptable_time?()
click to toggle source
# File lib/ice_cube/validated_rule.rb, line 130 def finds_acceptable_time? validation_names.all? do |type| validation_accepts_or_updates_time?(@validations[type]) end end
normalized_interval(interval)
click to toggle source
# File lib/ice_cube/validated_rule.rb, line 124 def normalized_interval(interval) int = interval.to_i raise ArgumentError, "'#{interval}' is not a valid input for interval. Please pass an integer." unless int > 0 int end
past_closing_time?(closing_time)
click to toggle source
# File lib/ice_cube/validated_rule.rb, line 174 def past_closing_time?(closing_time) closing_time && @time > closing_time end
shift_time_by_validation(res, validation)
click to toggle source
# File lib/ice_cube/validated_rule.rb, line 156 def shift_time_by_validation(res, validation) return unless (interval = res.min) wrapper = TimeUtil::TimeWrapper.new(@time, validation.dst_adjust?) wrapper.add(validation.type, interval) wrapper.clear_below(validation.type) # Move over DST if blocked, no adjustments if wrapper.to_time <= @time wrapper = TimeUtil::TimeWrapper.new(wrapper.to_time, false) until wrapper.to_time > @time wrapper.add(:min, 10) # smallest interval end end # And then get the correct time out @time = wrapper.to_time end
validation_accepts_or_updates_time?(validations_for_type)
click to toggle source
Returns true if all validations for the current rule match otherwise false and shifts to the first (largest) unmatched offset
# File lib/ice_cube/validated_rule.rb, line 146 def validation_accepts_or_updates_time?(validations_for_type) res = validations_for_type.each_with_object([]) do |validation, offsets| r = validation.validate(@time, @schedule) return true if r.nil? || r == 0 offsets << r end shift_time_by_validation(res, validations_for_type.first) false end
validation_names()
click to toggle source
# File lib/ice_cube/validated_rule.rb, line 178 def validation_names VALIDATION_ORDER & @validations.keys end