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

  class Event < SimpleDelegator

    BASIC_ATTRIBUTES = [:summary, :dtstart, :dtend]

    def to_h
      BASIC_ATTRIBUTES.inject({}) {|hsh, attr| hsh[attr] = send(attr).to_s; hsh }
    end

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

    def to_s
      "<#{self.class} #{self.to_h}>"
    end

    def inspect
      to_s
    end

    def recurring?
      ! rrule.empty?
    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