summaryrefslogtreecommitdiff
path: root/lib/sublab_calendar/event.rb
blob: e5042d8378baaceeaca81dc0a066a4001a98df44 (plain)
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
module SublabCalendar

  class Event < SimpleDelegator

    def to_h
      {
        summary: summary,
        start:   dtstart,
        end:     dtend
      }
    end

    def to_json(*args)
      to_h.to_json(*args)
    end

    def to_s
      "<#{self.class} #{self.to_h}>"
    end
    alias_method :inspect, :to_s

    def <=>(other)
      self.dtstart.to_datetime <=> other.dtstart.to_datetime
    end

    def ==(other)
      self.uid == other.uid
    end
    alias_method :eql?, :==

    def recurring?
      ! rrule.empty?
    end

    def all_day?
      # all day events do not have a time, and get parsed to a Icalendar::Values:Date class
      dtstart.is_a? Icalendar::Values::Date
    end

    def occurrences_this_month
      return nil unless recurring?
      occurrences_between(*this_month).map {|occ| Occurrence.new(occ, self) }
    end

    private

    def this_month
      [Date.today.beginning_of_month, Date.today.next_month.beginning_of_month]
    end

  end
end