diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/chaos_calendar.rb | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/chaos_calendar.rb b/lib/chaos_calendar.rb new file mode 100644 index 0000000..22ad934 --- /dev/null +++ b/lib/chaos_calendar.rb | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | require 'vpim/repo' | ||
| 2 | |||
| 3 | class Occurrence | ||
| 4 | def initialize start, event, node | ||
| 5 | @dtstart = start | ||
| 6 | @event = event | ||
| 7 | @node = node | ||
| 8 | end | ||
| 9 | attr_reader :dtstart, :event, :node | ||
| 10 | end | ||
| 11 | |||
| 12 | class ChaosCalendar | ||
| 13 | def initialize | ||
| 14 | @calendar = {} | ||
| 15 | end | ||
| 16 | |||
| 17 | def push cal, node | ||
| 18 | Vpim::Icalendar.decode( cal ).each { |c| c.events.each { |e| @calendar[e] = node } } | ||
| 19 | end | ||
| 20 | |||
| 21 | def occurrences start_time, end_time | ||
| 22 | occurr = [] | ||
| 23 | @calendar.each { |e, node| | ||
| 24 | if e.occurs_in?( start_time, end_time ) | ||
| 25 | e.occurences( end_time ) { |t| | ||
| 26 | occurr << Occurrence.new(t,e,node) if (t + (e.duration || 0)) >= start_time | ||
| 27 | } | ||
| 28 | end | ||
| 29 | } | ||
| 30 | |||
| 31 | return occurr.sort { |lhs, rhs| lhs.dtstart <=> rhs.dtstart } | ||
| 32 | end | ||
| 33 | |||
| 34 | end | ||
