summaryrefslogtreecommitdiff
path: root/lib/sublab_calendar
diff options
context:
space:
mode:
authorLars Henrik Mai <lars.mai@kontinui.de>2014-09-21 18:18:21 +0200
committerLars Henrik Mai <lars.mai@kontinui.de>2014-09-21 18:24:51 +0200
commit0740cac0afa46fbc87062eef7003ff653bd5208a (patch)
treeff47df8d43aa2f4b2fb43f3dc103f4c1f739c603 /lib/sublab_calendar
parent1955959b3bbd997a6a7f585f0293a295c019c935 (diff)
make Event and Occurrence sortable and adds some convenience
Diffstat (limited to 'lib/sublab_calendar')
-rw-r--r--lib/sublab_calendar/calendar.rb41
-rw-r--r--lib/sublab_calendar/event.rb23
-rw-r--r--lib/sublab_calendar/occurrence.rb28
3 files changed, 77 insertions, 15 deletions
diff --git a/lib/sublab_calendar/calendar.rb b/lib/sublab_calendar/calendar.rb
index e136ff5..0f95816 100644
--- a/lib/sublab_calendar/calendar.rb
+++ b/lib/sublab_calendar/calendar.rb
@@ -4,6 +4,15 @@ module SublabCalendar
attr_reader :calendar
+ PERIODS = {
+ future: ->(ev) { ev.dtstart >= DateTime.now },
+ past: ->(ev) { ev.dtstart < DateTime.now },
+ this_month: ->(ev) {
+ ev.dtstart >= DateTime.now.beginning_of_month &&
+ ev.dtstart < DateTime.now.end_of_month
+ }
+ }
+
def initialize(ical)
@calendar = Icalendar.parse(ical).first
end
@@ -23,16 +32,24 @@ module SublabCalendar
calendar.events.map(&method(:readable))
end
- def future
- calendar.events.select {|ev| ev.dtstart >= DateTime.now}.map(&method(:readable))
+ PERIODS.each do |name, selector|
+ define_method "#{name}_events" do # def future_events
+ calendar.events. # calendar.events.
+ select(&selector). # select(&PERIODS[:future]).
+ map(&method(:readable)) # map(&method(:readable))
+ end # end
end
- def past
- calendar.events.select {|ev| ev.dtstart < DateTime.now}.map(&method(:readable))
+ def recurring
+ events.select(&:recurring?)
end
- def recurring
- events.select(&:"recurring?")
+ def non_recurring
+ events.reject(&:recurring?)
+ end
+
+ def all_day
+ events.select(&:all_day?)
end
def next(count=1)
@@ -43,6 +60,18 @@ module SublabCalendar
SublabCalendar::Event.new(event)
end
+ def everything_this_month
+ normal = events.
+ select(&PERIODS[:this_month]).
+ reject(&:recurring?).
+ map(&method(:readable)).
+ flatten
+
+ occs = recurring.map(&:occurrences_this_month).flatten
+
+ EventList.new(normal + occs)
+ end
+
end
end
diff --git a/lib/sublab_calendar/event.rb b/lib/sublab_calendar/event.rb
index d350d51..e5042d8 100644
--- a/lib/sublab_calendar/event.rb
+++ b/lib/sublab_calendar/event.rb
@@ -2,10 +2,12 @@ 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 }
+ {
+ summary: summary,
+ start: dtstart,
+ end: dtend
+ }
end
def to_json(*args)
@@ -15,15 +17,26 @@ module SublabCalendar
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 inspect
- to_s
+ 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) }
diff --git a/lib/sublab_calendar/occurrence.rb b/lib/sublab_calendar/occurrence.rb
index fad58a8..085a814 100644
--- a/lib/sublab_calendar/occurrence.rb
+++ b/lib/sublab_calendar/occurrence.rb
@@ -17,10 +17,24 @@ module SublabCalendar
}
end
- def inspect
- to_s
+ 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 ==(o)
+ o.class == self.class && o.state == state
+ end
+ alias_method :eql?, :==
+
def summary
event.summary
end
@@ -29,8 +43,14 @@ module SublabCalendar
event.description
end
- def to_s
- "<#{self.class} #{self.to_h}>"
+ def dtstart
+ start_time.to_datetime
+ end
+
+ protected
+
+ def state
+ [event.uid, start_time, end_time]
end
end