Module: RiCal::PropertyValue::RecurrenceRule::Validations

Included in:
RiCal::PropertyValue::RecurrenceRule
Defined in:
lib/ri_cal/property_value/recurrence_rule/validations.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#errorsObject

Return any errors found during validation See #valid?



18
19
20
# File 'lib/ri_cal/property_value/recurrence_rule/validations.rb', line 18

def errors
  @errors ||= []
end

#reset_errorsObject

:nodoc:



22
23
24
# File 'lib/ri_cal/property_value/recurrence_rule/validations.rb', line 22

def reset_errors # :nodoc:
  @errors = nil
end

#valid?Boolean

Validate that the parameters of the reciever conform to RFC 2445 If errors are found they will be added to the receivers errors

Whenever any of the parameters are set, e.g. with:

recurrence_rule.count = 2

the errors will be reset

Returns:

  • (Boolean)


11
12
13
14
# File 'lib/ri_cal/property_value/recurrence_rule/validations.rb', line 11

def valid?
  validate if @errors.nil?
  errors.empty?
end

#validateObject

Used by #valid? to validate that the parameters of the reciever conform to RFC 2445 If errors are found they will be added to the receivers errors

Whenever any of the parameters are set, e.g. with:

recurrence_rule.count = 2

the errors will be reset



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ri_cal/property_value/recurrence_rule/validations.rb', line 32

def validate
  @errors = []
  validate_termination
  validate_freq
  validate_interval
  validate_int_by_list(:bysecond, (0..59))
  validate_int_by_list(:byminute, (0..59))
  validate_int_by_list(:byhour, (0..23))
  validate_int_by_list(:bymonth, (1..12))
  validate_bysetpos
  validate_byday_list
  validate_bymonthday_list
  validate_byyearday_list
  validate_byweekno_list
  validate_wkst
end

#validate_byday_listObject



93
94
95
96
97
98
# File 'lib/ri_cal/property_value/recurrence_rule/validations.rb', line 93

def validate_byday_list
  days = by_list[:byday] || []
  days.each do |day|
    errors << "#{day.source.inspect} is not a valid day" unless day.valid?
  end
end

#validate_bymonthday_listObject



100
101
102
103
104
105
# File 'lib/ri_cal/property_value/recurrence_rule/validations.rb', line 100

def validate_bymonthday_list
  days = by_list[:bymonthday] || []
  days.each do |day|
    errors << "#{day.source.inspect} is not a valid month day" unless day.valid?
  end
end

#validate_bysetposObject



83
84
85
86
87
88
89
90
91
# File 'lib/ri_cal/property_value/recurrence_rule/validations.rb', line 83

def validate_bysetpos
  vals = by_list[:bysetpos] || []
  vals.each do |val|
    errors << "#{val} is invalid for bysetpos" unless (-366..-1) === val  || (1..366) === val
  end
  unless vals.empty?
    errors << "bysetpos cannot be used without another by_xxx rule part" unless by_list.length > 1
  end
end

#validate_byweekno_listObject



114
115
116
117
118
119
# File 'lib/ri_cal/property_value/recurrence_rule/validations.rb', line 114

def validate_byweekno_list
  days = by_list[:byweekno] || []
  days.each do |day|
    errors << "#{day.source.inspect} is not a valid week number" unless day.valid?
  end
end

#validate_byyearday_listObject



107
108
109
110
111
112
# File 'lib/ri_cal/property_value/recurrence_rule/validations.rb', line 107

def validate_byyearday_list
  days = by_list[:byyearday] || []
  days.each do |day|
    errors << "#{day.source.inspect} is not a valid year day" unless day.valid?
  end
end

#validate_freqObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ri_cal/property_value/recurrence_rule/validations.rb', line 53

def validate_freq
  if @freq
    unless %w{
      SECONDLY MINUTELY HOURLY DAILY
      WEEKLY MONTHLY YEARLY
      }.include?(@freq.upcase)
      errors <<  "Invalid frequency '#{@freq}'"
    end
  else
    errors << "RecurrenceRule must have a value for FREQ"
  end
end

#validate_int_by_list(which, test) ⇒ Object



76
77
78
79
80
81
# File 'lib/ri_cal/property_value/recurrence_rule/validations.rb', line 76

def validate_int_by_list(which, test)
  vals = by_list[which] || []
  vals.each do |val|
    errors << "#{val} is invalid for #{which}" unless test === val
  end
end

#validate_intervalObject



66
67
68
69
70
# File 'lib/ri_cal/property_value/recurrence_rule/validations.rb', line 66

def validate_interval
  if @interval
    errors << "interval must be a positive integer" unless @interval > 0
  end
end

#validate_terminationObject



49
50
51
# File 'lib/ri_cal/property_value/recurrence_rule/validations.rb', line 49

def validate_termination
  errors << "COUNT and UNTIL cannot both be specified" if @count && @until
end

#validate_wkstObject



72
73
74
# File 'lib/ri_cal/property_value/recurrence_rule/validations.rb', line 72

def validate_wkst
  errors << "#{wkst.inspect} is invalid for wkst" unless %w{MO TU WE TH FR SA SU}.include?(wkst)
end