Class: ICalPal::Event
Overview
Class representing items from the CalendarItem
table
Constant Summary collapse
- QUERY =
<<~SQL SELECT DISTINCT Store.name AS account, Store.type, Calendar.color, Calendar.title AS calendar, Calendar.subcal_url, Calendar.symbolic_color_name, CAST(CalendarItem.end_date AS INT) AS end_date, CAST(CalendarItem.orig_date AS INT) AS orig_date, CAST(CalendarItem.start_date AS INT) AS start_date, CAST(CalendarItem.end_date - CalendarItem.start_date AS INT) AS duration, CalendarItem.all_day, CalendarItem.availability, CalendarItem.conference_url_detected, CalendarItem.description AS notes, CalendarItem.has_recurrences, CalendarItem.invitation_status, CalendarItem.orig_item_id, CalendarItem.rowid, CalendarItem.start_tz, CalendarItem.status, CalendarItem.summary AS title, CalendarItem.unique_identifier, CalendarItem.url, CalendarItem.uuid, json_group_array(DISTINCT CAST(ExceptionDate.date AS INT)) AS xdate, json_group_array(DISTINCT Identity.display_name) AS attendees, Location.address AS address, Location.title AS location, Recurrence.count, CAST(Recurrence.end_date AS INT) AS rend_date, Recurrence.frequency, Recurrence.interval, Recurrence.specifier, min(Alarm.trigger_interval) AS trigger_interval FROM Store JOIN Calendar ON Calendar.store_id = Store.rowid JOIN CalendarItem ON CalendarItem.calendar_id = Calendar.rowid LEFT OUTER JOIN Location ON Location.rowid = CalendarItem.location_id LEFT OUTER JOIN Recurrence ON Recurrence.owner_id = CalendarItem.rowid LEFT OUTER JOIN ExceptionDate ON ExceptionDate.owner_id = CalendarItem.rowid LEFT OUTER JOIN Alarm ON Alarm.calendaritem_owner_id = CalendarItem.rowid LEFT OUTER JOIN Participant ON Participant.owner_id = CalendarItem.rowid LEFT OUTER JOIN Identity ON Identity.rowid = Participant.identity_id WHERE Store.disabled IS NOT 1 GROUP BY CalendarItem.rowid ORDER BY CalendarItem.unique_identifier SQL
Constants included from ICalPal
Instance Attribute Summary
Attributes included from ICalPal
Instance Method Summary collapse
-
#[](k) ⇒ Object
Standard accessor with special handling for
age
,availability
,datetime
,location
,notes
,status
,title
, anduid
. - #[]=(k, v) ⇒ Object
-
#apply_frequency! ⇒ Object
Apply frequency and interval.
-
#clone ⇒ Object
A deep clone of self.
-
#get_occurrences(changes) ⇒ Array<IcalPal::Event>
Get next occurences of a recurring event.
-
#in_window?(s, e = s) ⇒ Boolean
Check if an event starts or ends between from and to, or if it’s running now (for -n).
-
#initialize(obj) ⇒ Event
constructor
A new instance of Event.
-
#non_recurring ⇒ Array<Event>
Check non-recurring events.
-
#recurring ⇒ Array<Event>
Check recurring events.
Methods included from ICalPal
Constructor Details
#initialize(obj) ⇒ Event #initialize(obj<DateTime>) ⇒ Event
Returns a new instance of Event.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/event.rb', line 65 def initialize(obj) # Placeholder for days with no events return @self = { $opts[:sep] => obj, 'placeholder' => true, 'title' => 'Nothing.', } if DateTime === obj @self = {} obj.keys.each { |k| @self[k] = obj[k] } # Convert JSON arrays to Arrays @self['attendees'] = JSON.parse(obj['attendees']) @self['xdate'] = JSON.parse(obj['xdate']).map do |k| k = RDT.new(*Time.at(k + ITIME).to_a.reverse[4..]) if k end # Convert iCal dates to normal dates obj.keys.select { |i| i.end_with? '_date' }.each do |k| t = Time.at(obj[k] + ITIME) if obj[k] @self["#{k[0]}date"] = RDT.new(*t.to_a.reverse[4..], t.zone) if t end if @self['start_tz'] == '_float' @self['sdate'] = RDT.new(*(@self['sdate'].to_time - Time.zone_offset($now.zone())).to_a.reverse[4..], $now.zone) @self['edate'] = RDT.new(*(@self['edate'].to_time - Time.zone_offset($now.zone())).to_a.reverse[4..], $now.zone) end # Type of calendar event is from obj['type'] = EventKit::EKSourceType.find_index { |i| i[:name] == 'Subscribed' } if obj['subcal_url'] type = EventKit::EKSourceType[obj['type']] @self['symbolic_color_name'] ||= @self['color'] @self['type'] = type[:name] end |
Instance Method Details
#[](k) ⇒ Object
Standard accessor with special handling for age
, availability
, datetime
, location
, notes
, status
, title
, and uid
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 53 54 55 56 57 |
# File 'lib/event.rb', line 16 def [](k) case k when 'age' then # pseudo-property @self['sdate'].year - @self['edate'].year when 'availability' then # Integer -> String EventKit::EKEventAvailability.select { |k, v| v == @self['availability'] }.keys when 'datetime' then # date[ at time[ - time]] unless $opts[:sd] || $opts[:days] == 1 t = @self['sdate'].to_s t += ' at ' unless @self['all_day'].positive? end unless @self['all_day'] && @self['all_day'].positive? t ||= '' t += "#{@self['sdate'].strftime($opts[:tf])}" if @self['sdate'] t += " - #{@self['edate'].strftime($opts[:tf])}" unless $opts[:eed] || !@self['edate'] end t when 'location' then # location[ address] @self['location']? [ @self['location'], @self['address'] ].join(' ').chop : nil when 'notes' then # \n -> :nnr @self['notes']? @self['notes'].strip.gsub(/\n/, $opts[:nnr]) : nil when 'sday' then # pseudo-property ICalPal::RDT.new(*@self['sdate'].to_a[0..2]) when 'status' then # Integer -> String EventKit::EKEventStatus.select { |k, v| v == @self['status'] }.keys[0] when 'title' then # title[ (age N)] @self['title'] + ((@self['calendar'] == 'Birthdays')? " (age #{self['age']})" : "") when 'uid' then # for icalBuddy @self['UUID'] else @self[k] end end |
#[]=(k, v) ⇒ Object
6 7 8 9 |
# File 'lib/event.rb', line 6 def []=(k, v) @self[k] = v @self['sday'] = ICalPal::RDT.new(*self['sdate'].to_a[0..2]) if k == 'sdate' end |
#apply_frequency! ⇒ Object
Apply frequency and interval
217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/event.rb', line 217 def apply_frequency! # Leave edate alone for birthdays to compute age dates = [ 'sdate' ] dates << 'edate' unless self['calendar'].include?('Birthday') dates.each do |d| case EventKit::EKRecurrenceFrequency[self['frequency'] - 1] when 'daily' then self[d] += self['interval'] when 'weekly' then self[d] += self['interval'] * 7 when 'monthly' then self[d] >>= self['interval'] when 'yearly' then self[d] >>= self['interval'] * 12 end end if self['frequency'] && self['interval'] end |
#clone ⇒ Object
Returns a deep clone of self.
155 156 157 158 159 160 |
# File 'lib/event.rb', line 155 def clone() self['stime'] = @self['sdate'].to_i self['etime'] = @self['edate'].to_i Marshal.load(Marshal.dump(self)) end |
#get_occurrences(changes) ⇒ Array<IcalPal::Event>
Get next occurences of a recurring event
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/event.rb', line 166 def get_occurrences(changes) ndate = self['sdate'] odays = [] retval = [] # Deconstruct specifier(s) if self['specifier'] self['specifier'].split(';').each do |k| j = k.split('=') # M=Day of the month, O=Month of the year, S=Nth case j[0] when 'M' then ndate = RDT.new(ndate.year, ndate.month, j[1].to_i) when 'O' then ndate = RDT.new(ndate.year, j[1].to_i, ndate.day) when 'S' then @self['specifier'].sub!(/D=0/, "D=+#{j[1].to_i}") end end # D=Day of the week self['specifier'].split(';').each do |k| j = k.split('=') odays = j[1].split(',') if j[0] == 'D' end end # Deconstruct occurence day(s) odays.each do |n| dow = DOW[n[-2..-1].to_sym] ndate += 1 until ndate.wday == dow ndate = ICalPal.nth(Integer(n[0..1]), n[-2..-1], ndate) unless (n[0] == '0') # Check for changes changes.detect( proc { self['sdate'] = RDT.new(*ndate.to_a[0..2], *self['sdate'].to_a[3..]) self['edate'] = RDT.new(*ndate.to_a[0..2], *self['edate'].to_a[3..]) retval.push(clone) }) { |i| @self['sday'] == i['sday'] } end # Check for changes changes.detect( proc { retval.push(clone) }) { |i| @self['sday'] == i['sday'] } unless retval.count.positive? retval end |
#in_window?(s, e = s) ⇒ Boolean
Check if an event starts or ends between from and to, or if it’s running now (for -n)
238 239 240 241 242 |
# File 'lib/event.rb', line 238 def in_window?(s, e = s) $opts[:n]? ($now >= s && $now < e) : ([ s, e ].max >= $opts[:from] && s < $opts[:to]) end |
#non_recurring ⇒ Array<Event>
Check non-recurring events
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/event.rb', line 106 def non_recurring retval = [] # Repeat for multi-day events ((self['duration'] / 86400).to_i + 1).times do |i| self['daynum'] = i + 1 retval.push(clone) if in_window?(self['sdate']) self['sdate'] += 1 self['edate'] += 1 end retval end |
#recurring ⇒ Array<Event>
Check recurring events
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/event.rb', line 124 def recurring retval = [] # See if event ends before we start stop = [ $opts[:to], (self['rdate'] || $opts[:to]) ].min return(retval) if stop < $opts[:from] # Get changes to series changes = $rows.select { |r| r['orig_item_id'] == self['ROWID'] } i = 1 while self['sdate'] <= stop return(retval) if self['count'].positive? && i > self['count'] i += 1 unless @self['xdate'].any?(@self['sdate']) # Exceptions? o = get_occurrences(changes) o.each { |r| retval.push(r) if in_window?(r['sdate'], r['edate']) } end apply_frequency! end retval end |