Class: IceCube::ValidatedRule

Constant Summary

Constants included from IceCube::Validations::Lock

IceCube::Validations::Lock::INTERVALS

Instance Attribute Summary

Attributes inherited from Rule

#uses

Instance Method Summary collapse

Methods included from IceCube::Validations::Until

#until, #until_time

Methods included from Deprecated

#deprecated, #deprecated_alias

Methods included from IceCube::Validations::Count

#count, #occurrence_count

Methods included from IceCube::Validations::DayOfYear

#day_of_year

Methods included from IceCube::Validations::MonthOfYear

#month_of_year

Methods included from IceCube::Validations::Day

#day

Methods included from IceCube::Validations::DayOfWeek

#day_of_week

Methods included from IceCube::Validations::DayOfMonth

#day_of_month

Methods included from IceCube::Validations::Lock

#validate

Methods included from IceCube::Validations::SecondOfMinute

#second_of_minute

Methods included from IceCube::Validations::MinuteOfHour

#minute_of_hour

Methods included from IceCube::Validations::HourOfDay

#hour_of_day

Methods included from IceCube::Validations::ScheduleLock

#schedule_lock

Methods inherited from Rule

#==, daily, from_hash, from_ical, from_yaml, #full_required?, #hash, hourly, minutely, monthly, #on?, #reset, secondly, #terminating?, #to_yaml, weekly, yearly

Instance Method Details

#clobber_base_validations(*types) ⇒ Object

Remove the specified base validations



100
101
102
103
104
# File 'lib/ice_cube/validated_rule.rb', line 100

def clobber_base_validations(*types)
  types.each do |type|
    @validations.delete(:"base_#{type}")
  end
end

#next_time(time, schedule, closing_time) ⇒ Object

Compute the next time after (or including) the specified time in respect to the given schedule NOTE: optimization target, sort the rules by their type, year first so we can make bigger jumps more often



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ice_cube/validated_rule.rb', line 23

def next_time(time, schedule, closing_time)
  loop do
    break if @validations.all? do |name, vals|
      # Execute each validation
      res = vals.map do |validation|
        validation.validate(time, schedule)
      end
      # If there is any nil, then we're set - otherwise choose the lowest
      if res.any? { |r| r.nil? || r == 0 }
        true
      else
        return nil if res.all? { |r| r === true } # allow quick escaping
        res.reject! { |r| r.nil? || r == 0 || r === true }
        if fwd = res.min
          type = vals.first.type # get the jump type
          dst_adjust = !vals.first.respond_to?(:dst_adjust?) || vals.first.dst_adjust?
          wrapper = TimeUtil::TimeWrapper.new(time, dst_adjust)
          wrapper.add(type, fwd)
          wrapper.clear_below(type)
          time = wrapper.to_time
        end
        false
      end
    end
    # Prevent a non-matching infinite loop
    return nil if closing_time && time > closing_time
  end
  # NOTE Uses may be 1 higher than proper here since end_time isn't
  # validated in this class.  This is okay now, since we never expose it -
  # but if we ever do - we should check that above this line, and return
  # nil if end_time is past
  @uses += 1 if time
  time
end

#replace_validations_for(key, arr) ⇒ Object

Fully replace validations



95
96
97
# File 'lib/ice_cube/validated_rule.rb', line 95

def replace_validations_for(key, arr)
  @validations[key] = arr
end

#to_hashObject



68
69
70
71
72
73
74
75
76
# File 'lib/ice_cube/validated_rule.rb', line 68

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_icalObject



78
79
80
81
82
83
84
85
86
# File 'lib/ice_cube/validated_rule.rb', line 78

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_sObject



58
59
60
61
62
63
64
65
66
# File 'lib/ice_cube/validated_rule.rb', line 58

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) ⇒ Object

Get the collection that contains validations of a certain type



89
90
91
92
# File 'lib/ice_cube/validated_rule.rb', line 89

def validations_for(key)
  @validations ||= {}
  @validations[key] ||= []
end