Class: Zimbra::Appointment::RecurRule

Inherits:
Object
  • Object
show all
Defined in:
lib/zimbra/appointment/recur_rule.rb

Constant Summary collapse

ATTRS =
[
  :frequency,
  :interval,
  :by_day,
  :by_month,
  :by_month_day,
  :count,
  :until_date,
  :by_set_position
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ RecurRule

Returns a new instance of RecurRule.



66
67
68
# File 'lib/zimbra/appointment/recur_rule.rb', line 66

def initialize(args = {})
  self.attributes = args
end

Class Method Details

.new_from_zimbra_attributes(zimbra_attributes) ⇒ Object



5
6
7
8
# File 'lib/zimbra/appointment/recur_rule.rb', line 5

def new_from_zimbra_attributes(zimbra_attributes)
  return nil unless zimbra_attributes
  new(parse_zimbra_attributes(zimbra_attributes))
end

.parse_zimbra_attributes(zimbra_attributes) ⇒ Object



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
# File 'lib/zimbra/appointment/recur_rule.rb', line 10

def parse_zimbra_attributes(zimbra_attributes)
  attrs = {}
  
  zimbra_attributes = Zimbra::Hash.symbolize_keys(zimbra_attributes.dup, true)
  zimbra_attributes = zimbra_attributes[:add][:rule]
  
  attrs[:frequency] = zimbra_attributes[:attributes][:freq] if zimbra_attributes[:attributes]
  attrs[:until_date] = zimbra_attributes[:until][:attributes][:d] if zimbra_attributes[:until]
  attrs[:interval] = zimbra_attributes[:interval][:attributes][:ival] if zimbra_attributes[:interval]
  attrs[:count] = zimbra_attributes[:count][:attributes][:num] if zimbra_attributes[:count]
  
  if zimbra_attributes[:bysetpos]
    attrs[:by_set_position] = zimbra_attributes[:bysetpos][:attributes][:poslist]
    attrs[:by_set_position] = [attrs[:by_set_position]] unless attrs[:by_set_position].is_a?(Array)
  end
  
  if zimbra_attributes[:byday] && zimbra_attributes[:byday][:wkday] && zimbra_attributes[:byday][:wkday].is_a?(Array)
    attrs[:by_day] = zimbra_attributes[:byday][:wkday].collect do |wkday|
      wkday = Zimbra::Hash.symbolize_keys(wkday, true)
      wkday_hash = { day: wkday[:attributes][:day] }
      wkday_hash[:week_number] = wkday[:attributes][:ordwk] if wkday[:attributes][:ordwk]
      wkday_hash
    end
  elsif zimbra_attributes[:byday] && zimbra_attributes[:byday][:wkday]
    day_hash = { day: zimbra_attributes[:byday][:wkday][:attributes][:day] }
    day_hash[:week_number] = zimbra_attributes[:byday][:wkday][:attributes][:ordwk] if zimbra_attributes[:byday][:wkday][:attributes][:ordwk]
    attrs[:by_day] = [day_hash]
  end

  if zimbra_attributes[:bymonth]
    attrs[:by_month] = zimbra_attributes[:bymonth][:attributes][:molist]
    attrs[:by_month] = [attrs[:by_month]] unless attrs[:by_month].is_a?(Array)
  end
  
  if zimbra_attributes[:bymonthday]
    attrs[:by_month_day] = zimbra_attributes[:bymonthday][:attributes][:modaylist]
    attrs[:by_month_day] = [attrs[:by_month_day]] unless attrs[:by_month_day].is_a?(Array)
  end
  
  attrs
end

Instance Method Details

#attributes=(args = {}) ⇒ Object

take attributes by the xml name or our more descriptive name



71
72
73
74
75
76
77
78
79
# File 'lib/zimbra/appointment/recur_rule.rb', line 71

def attributes=(args = {})
  ATTRS.each do |attr_name|
    if args.has_key?(attr_name)
      self.send(:"#{attr_name}=", args[attr_name])
    elsif args.has_key?(attr_name.to_s)
      self.send(:"#{attr_name}=", args[attr_name.to_s])
    end
  end
end

#by_day=(val) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/zimbra/appointment/recur_rule.rb', line 101

def by_day=(val)
  @by_day = if val.is_a?(Array)
    val.collect do |day_specification|
      day_specification[:day] = Zimbra::DateHelpers::WeekDay.find(day_specification[:day]) rescue day_specification[:day]
      day_specification
    end
  else
    val
  end
end

#by_set_position=(val) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/zimbra/appointment/recur_rule.rb', line 94

def by_set_position=(val)
  if val == [0]
    @by_set_position = nil
  else
    @by_set_position = val
  end
end

#create_xml(document) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/zimbra/appointment/recur_rule.rb', line 132

def create_xml(document)
  document.add "rule" do |rule_element|
    rule_element.set_attr "freq", frequency_to_zimbra
    
    if until_date
      rule_element.add "until" do |until_element|
        until_element.set_attr "d", until_date.utc.strftime("%Y%m%dT%H%M%SZ")
      end
    end
    
    rule_element.add "interval" do |interval_element|
      interval_element.set_attr "ival", interval
    end
    
    if count && count > 0
      rule_element.add "count" do |count_element|
        count_element.set_attr "num", count
      end
    end
    
    if by_day && by_day.count > 0
      rule_element.add "byday" do |by_day_element|
        by_day.each do |day_spec|
          by_day_element.add "wkday" do |wkday_element|
            wkday_element.set_attr "day", day_spec[:day].zimbra_name
            wkday_element.set_attr "ordwk", day_spec[:week_number] if day_spec.has_key?(:week_number)
          end
        end
      end
    end
    
    if by_month && by_month.count > 0
      rule_element.add "bymonth" do |by_month_element|
        by_month_element.set_attr "molist", by_month.join(',')
      end
    end
    
    if by_month_day && by_month_day.count > 0
      rule_element.add "bymonthday" do |by_month_day_element|
        by_month_day_element.set_attr "modaylist", by_month_day.join(',')
      end
    end
    
    if by_set_position
      rule_element.add "bysetpos" do |bysetpos_element|
        bysetpos_element.set_attr "poslist", by_set_position.join(',')
      end
    end
  end
end

#frequency=(val) ⇒ Object



81
82
83
84
# File 'lib/zimbra/appointment/recur_rule.rb', line 81

def frequency=(val)
  frequency = Zimbra::DateHelpers::Frequency.find(val).name
  @frequency = frequency || val
end

#frequency_to_zimbraObject



86
87
88
# File 'lib/zimbra/appointment/recur_rule.rb', line 86

def frequency_to_zimbra
  Zimbra::DateHelpers::Frequency.find(frequency).zimbra_name rescue frequency
end

#to_hash(options = {}) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/zimbra/appointment/recur_rule.rb', line 112

def to_hash(options = {})
  hash = {
    :frequency => frequency ? frequency.to_sym : nil,
    :interval => interval,
    :by_month => by_month,
    :by_month_day => by_month_day,
    :count => count,
    :until_date => until_date,
    :by_set_position => by_set_position
  }
  hash[:by_day] = by_day.collect do |day_specification|
    day_specification[:day] = day_specification[:day].to_sym if day_specification[:day]
    day_specification
  end if by_day
  hash.reject! { |key, value| value.nil? }
  hash.reject! { |key, value| options[:except].include?(key.to_sym) || options[:except].include?(key.to_s) } if options[:except]
  hash.reject! { |key, value| !options[:only].include?(key.to_sym) && !options[:only].include?(key.to_s) } if options[:only]
  hash
end

#until_date=(val) ⇒ Object



90
91
92
# File 'lib/zimbra/appointment/recur_rule.rb', line 90

def until_date=(val)
  @until_date = Time.parse(val) rescue val
end