File: validations.rb

package info (click to toggle)
mhc 1.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,452 kB
  • sloc: ruby: 12,700; lisp: 7,577; makefile: 70; sh: 68
file content (125 lines) | stat: -rw-r--r-- 3,945 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
module RiCal
  class PropertyValue
    class RecurrenceRule < PropertyValue
      #- ©2009 Rick DeNatale, All rights reserved. Refer to the file README.txt for the license
      #
      module Validations #:nodoc:
        # 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
        def valid?
          validate if @errors.nil?
          errors.empty?
        end

        # Return any errors found during validation
        # See #valid?
        def errors
          @errors ||= []
        end

        def reset_errors # :nodoc:
          @errors = nil
        end

        # 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
        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

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

        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

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

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

        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

        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

        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

        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

        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

        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
      end
    end
  end
end