Class: IceCube::ValidatedRule

Inherits:
Rule
  • Object
show all
Includes:
IceCube::Validations::Count, IceCube::Validations::ScheduleLock, IceCube::Validations::Until
Defined in:
lib/ice_cube/validated_rule.rb

Constant Summary collapse

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

[
  :year, :month, :day, :wday, :hour, :min, :sec, :count, :until,
  :base_sec, :base_min, :base_day, :base_hour, :base_month, :base_wday,
  :day_of_year, :second_of_minute, :minute_of_hour, :day_of_month,
  :hour_of_day, :month_of_year, :day_of_week,
  :interval
]

Constants inherited from Rule

Rule::INTERVAL_TYPES

Instance Attribute Summary collapse

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, schedule_options

Methods included from IceCube::Validations::Count

#count, #occurrence_count

Methods included from IceCube::Validations::ScheduleLock

#schedule_lock

Methods inherited from Rule

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

Constructor Details

#initialize(interval = 1) ⇒ ValidatedRule

Returns a new instance of ValidatedRule.



26
27
28
# File 'lib/ice_cube/validated_rule.rb', line 26

def initialize(interval = 1)
  @validations = {}
end

Instance Attribute Details

#validationsObject (readonly)

Returns the value of attribute validations.



24
25
26
# File 'lib/ice_cube/validated_rule.rb', line 24

def validations
  @validations
end

Instance Method Details

#base_interval_validationObject



37
38
39
# File 'lib/ice_cube/validated_rule.rb', line 37

def base_interval_validation
  @validations[:interval].first
end

#clobber_base_validations(*types) ⇒ Object

Remove the specified base validations



111
112
113
114
115
# File 'lib/ice_cube/validated_rule.rb', line 111

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

#full_required?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/ice_cube/validated_rule.rb', line 62

def full_required?
  !occurrence_count.nil?
end

#next_time(time, start_time, closing_time) ⇒ Object

Compute the next time after (or including) the specified time in respect to the given start time



47
48
49
50
51
52
53
54
55
56
# File 'lib/ice_cube/validated_rule.rb', line 47

def next_time(time, start_time, closing_time)
  @time = time
  @start_time ||= realign(time, start_time)
  @time = @start_time if @time < @start_time

  return nil unless find_acceptable_time_before(closing_time)

  @uses += 1 if @time
  @time
end

#other_interval_validationsObject



41
42
43
# File 'lib/ice_cube/validated_rule.rb', line 41

def other_interval_validations
  Array(@validations[base_interval_validation.type])
end

#realign(opening_time, start_time) ⇒ Object



58
59
60
# File 'lib/ice_cube/validated_rule.rb', line 58

def realign(opening_time, start_time)
  start_time
end

#replace_validations_for(key, arr) ⇒ Object

Fully replace validations



102
103
104
105
106
107
108
# File 'lib/ice_cube/validated_rule.rb', line 102

def replace_validations_for(key, arr)
  if arr.nil?
    @validations.delete(key)
  else
    @validations[key] = arr
  end
end

#resetObject

Reset the uses on the rule to 0



31
32
33
34
35
# File 'lib/ice_cube/validated_rule.rb', line 31

def reset
  @time = nil
  @start_time = nil
  @uses = 0
end

#to_hashObject



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

def to_hash
  builder = HashBuilder.new(self)
  @validations.each_value do |validations|
    validations.each do |validation|
      validation.build_hash(builder)
    end
  end
  builder.to_hash
end

#to_icalObject



86
87
88
89
90
91
92
93
94
# File 'lib/ice_cube/validated_rule.rb', line 86

def to_ical
  builder = IcalBuilder.new
  @validations.each_value do |validations|
    validations.each do |validation|
      validation.build_ical(builder)
    end
  end
  builder.to_s
end

#to_sObject



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

def to_s
  builder = StringBuilder.new
  @validations.each_value do |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



97
98
99
# File 'lib/ice_cube/validated_rule.rb', line 97

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