sdague-icalendar 1.1.0.2 → 1.1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/Rakefile +1 -1
  2. data/lib/icalendar/rrule.rb +126 -0
  3. metadata +3 -2
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ require 'rake/rdoctask'
5
5
  require 'rake/clean'
6
6
  require 'rake/contrib/sshpublisher'
7
7
 
8
- PKG_VERSION = "1.1.0.2"
8
+ PKG_VERSION = "1.1.0.3"
9
9
 
10
10
  $VERBOSE = nil
11
11
  TEST_CHANGES_SINCE = Time.now - 600 # Recent tests = changed in last 10 minutes
@@ -0,0 +1,126 @@
1
+ =begin
2
+ Copyright (C) 2008 Rick (http://github.com/rubyredrick)
3
+
4
+ This library is free software; you can redistribute it and/or modify it
5
+ under the same terms as the ruby language itself, see the file COPYING for
6
+ details.
7
+ =end
8
+
9
+ require 'date'
10
+ require 'uri'
11
+ require 'stringio'
12
+
13
+ module Icalendar
14
+
15
+ # This class is not yet fully functional..
16
+ #
17
+ # Gem versions < 1.1.0.0 used to return a string for the recurrence_rule component,
18
+ # but now it returns this Icalendar::RRule class. ie It's not backwards compatible!
19
+ #
20
+ # To get the original RRULE value from a parsed feed, use the 'orig_value' property.
21
+ #
22
+ # Example:
23
+ # rules = event.recurrence_rules.map{ |rule| rule.orig_value }
24
+
25
+ class RRule < Icalendar::Base
26
+
27
+ class Weekday
28
+ def initialize(day, position)
29
+ @day, @position = day, position
30
+ end
31
+
32
+ def to_s
33
+ "#{@position}#{@day}"
34
+ end
35
+ end
36
+
37
+ def initialize(name, params, value, parser)
38
+ @value = value
39
+ frequency_match = value.match(/FREQ=(SECONDLY|MINUTELY|HOURLY|DAILY|WEEKLY|MONTHLY|YEARLY)/)
40
+ raise Icalendar::InvalidPropertyValue.new("FREQ must be specified for RRULE values") unless frequency_match
41
+ @frequency = frequency_match[1]
42
+ @until = parse_date_val("UNTIL", value)
43
+ @count = parse_int_val("COUNT", value)
44
+ raise Icalendar::InvalidPropertyValue.new("UNTIL and COUNT must not both be specified for RRULE values") if [@until, @count].compact.length > 1
45
+ @interval = parse_int_val("INTERVAL", value)
46
+ @by_list = {:bysecond => parse_int_list("BYSECOND", value)}
47
+ @by_list[:byminute] = parse_int_list("BYMINUTE",value)
48
+ @by_list[:byhour] = parse_int_list("BYHOUR", value)
49
+ @by_list[:byday] = parse_weekday_list("BYDAY", value)
50
+ @by_list[:bymonthday] = parse_int_list("BYMONTHDAY", value)
51
+ @by_list[:byyearday] = parse_int_list("BYYEARDAY", value)
52
+ @by_list[:byweekno] = parse_int_list("BYWEEKNO", value)
53
+ @by_list[:bymonth] = parse_int_list("BYMONTH", value)
54
+ @by_list[:bysetpos] = parse_int_list("BYSETPOS", value)
55
+ @wkst = parse_wkstart(value)
56
+ end
57
+
58
+ # Returns the original pre-parsed RRULE value.
59
+ def orig_value
60
+ @value
61
+ end
62
+
63
+ def to_ical
64
+ result = ["FREQ=#{@frequency}"]
65
+ result << ";UNTIL=#{@until.to_ical}" if @until
66
+ result << ";COUNT=#{@count}" if @count
67
+ result << ";INTERVAL=#{@interval}" if @interval
68
+ @by_list.each do |key, value|
69
+ result << ";#{key.to_s.upcase}=#{value}" if value
70
+ end
71
+ result << ";WKST=#{@wkst}" if @wkst
72
+ result.join
73
+ end
74
+
75
+ def parse_date_val(name, string)
76
+ match = string.match(/;#{name}=(.*?)(;|$)/)
77
+ match ? DateTime.parse(match[1]) : nil
78
+ end
79
+
80
+ def parse_int_val(name, string)
81
+ match = string.match(/;#{name}=(\d+)(;|$)/)
82
+ match ? match[1].to_i : nil
83
+ end
84
+
85
+ def parse_int_list(name, string)
86
+ match = string.match(/;#{name}=([+-]?.*?)(;|$)/)
87
+ if match
88
+ match[1].split(",").map {|int| int.to_i}
89
+ else
90
+ nil
91
+ end
92
+ end
93
+
94
+ def parse_weekday_list(name, string)
95
+ match = string.match(/;#{name}=(.*?)(;|$)/)
96
+ if match
97
+ match[1].split(",").map {|weekday|
98
+ wd_match = weekday.match(/([+-]?\d*)(SU|MO|TU|WE|TH|FR|SA)/)
99
+ Weekday.new(wd_match[2], wd_match[1])
100
+ }
101
+ else
102
+ nil
103
+ end
104
+ end
105
+
106
+ def parse_wkstart(string)
107
+ match = string.match(/;WKSTART=(SU|MO|TU|WE|TH|FR|SA)(;|$)/)
108
+ if match
109
+ %w{SU MO TU WE TH FR SA}.index(match[1])
110
+ else
111
+ nil
112
+ end
113
+ end
114
+
115
+ # TODO: Incomplete
116
+ def occurrences_of_event_starting(event, datetime)
117
+ initial_start = event.dtstart
118
+ (0...@count).map {|day_offset|
119
+ occurrence = event.clone
120
+ occurrence.dtstart = initial_start + day_offset
121
+ occurrence.clone
122
+ }
123
+ end
124
+ end
125
+
126
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sdague-icalendar
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0.2
4
+ version: 1.1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Dague
@@ -9,7 +9,7 @@ autorequire: icalendar
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-21 00:00:00 -07:00
12
+ date: 2009-03-26 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -56,6 +56,7 @@ files:
56
56
  - lib/icalendar/component/timezone.rb
57
57
  - lib/icalendar/component/freebusy.rb
58
58
  - lib/icalendar/conversions.rb
59
+ - lib/icalendar/rrule.rb
59
60
  - lib/icalendar/tzinfo.rb
60
61
  - lib/icalendar/helpers.rb
61
62
  - lib/meta.rb