File: initialization_methods.rb

package info (click to toggle)
mhc 1.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,320 kB
  • ctags: 3,529
  • sloc: ruby: 12,404; lisp: 7,448; makefile: 70; sh: 69
file content (148 lines) | stat: -rw-r--r-- 4,656 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
module RiCal
  class PropertyValue
    class RecurrenceRule < PropertyValue
      #- ©2009 Rick DeNatale, All rights reserved. Refer to the file README.txt for the license
      #
      module InitializationMethods # :nodoc:
        
        attr_reader :by_day_scope
        
        def add_to_options_hash(options_hash, key, value)
          options_hash[key] = value if value
          options_hash
        end
        
        def add_byrule_strings_to_options_hash(options_hash, key)
          if (rules = by_list[key])
            if rules.length == 1
              options_hash[key] = rules.first.source
            else
              options_hash[key] = rules.map {|rule| rule.source}
            end
          end
        end
        
        def to_options_hash
          options_hash = {:freq => freq, :interval => interval}
          options_hash[:params] = params unless params.empty?
          add_to_options_hash(options_hash, :count, @count)
          add_to_options_hash(options_hash, :until, @until)
          add_to_options_hash(options_hash, :interval, @interval)
          [:bysecond, :byminute, :byhour, :bymonth, :bysetpos].each do |bypart|
              add_to_options_hash(options_hash, bypart, by_list[bypart])
            end
          [:byday, :bymonthday, :byyearday, :byweekno].each do |bypart|
             add_byrule_strings_to_options_hash(options_hash, bypart)
          end
          options_hash
        end

        def initialize_from_value_part(part, dup_hash) # :nodoc:
          part_name, value = part.split("=")
          attribute = part_name.downcase
          errors << "Repeated rule part #{attribute} last occurrence was used" if dup_hash[attribute]
          case attribute
          when "freq"
            self.freq = value
          when "wkst"
            self.wkst = value
          when "until"
            @until = PropertyValue.date_or_date_time(self, :value => value)
          when "count"
            @count = value.to_i
          when "interval"
            self.interval = value.to_i
          when "bysecond", "byminute", "byhour", "bymonthday", "byyearday", "byweekno", "bymonth", "bysetpos"
            send("#{attribute}=", value.split(",").map {|int| int.to_i})
          when "byday"
            self.byday = value.split(",")
          else
            errors << "Invalid rule part #{part}"
          end
        end

        def by_list
          @by_list ||= {}
        end
        
        def calc_by_day_scope
          case freq
          when "YEARLY"
            scope = :yearly
          when "MONTHLY"
            scope = :monthly
          when "WEEKLY"
            scope = :weekly
          else
            scope = :daily
          end
          scope = :monthly if scope != :weekly && @by_list_hash[:bymonth]
          scope = :weekly if scope != :daily && @by_list_hash[:byweekno]
          @by_day_scope = scope
        end
        
        def bysecond=(val)
          @by_list_hash[:bysecond] = val
        end

        def byminute=(val)
          @by_list_hash[:byminute] = val
        end

        def byhour=(val)
          @by_list_hash[:byhour] = val
        end

        def bymonth=(val)
          @by_list_hash[:bymonth] = val
        end

        def bysetpos=(val)
          @by_list_hash[:bysetpos] = val
        end

        def byday=(val)
          @by_list_hash[:byday] = val
        end

        def bymonthday=(val)
          @by_list_hash[:bymonthday] = val
        end

        def byyearday=(val)
          @by_list_hash[:byyearday] = val
        end

        def byweekno=(val)
          @by_list_hash[:byweekno] = val
        end

        def init_by_lists
          [:bysecond,
            :byminute,
            :byhour,
            :bymonth,
            :bysetpos
            ].each do |which|
              if val = @by_list_hash[which]
                by_list[which] = [val].flatten.sort
              end
            end
            if val = @by_list_hash[:byday]
              byday_scope =  calc_by_day_scope 
              by_list[:byday] = [val].flatten.map {|day| RecurringDay.new(day, self, byday_scope)}
            end
            if val = @by_list_hash[:bymonthday]
              by_list[:bymonthday] = [val].flatten.map {|md| RecurringMonthDay.new(md)}
            end
            if val = @by_list_hash[:byyearday]
              by_list[:byyearday] = [val].flatten.map {|yd| RecurringYearDay.new(yd)}
            end
            if val = @by_list_hash[:byweekno]
              by_list[:byweekno] = [val].flatten.map {|wkno| RecurringNumberedWeek.new(wkno, self)}
            end
          end
        end
      end
    end
  end