class RemindersTxt
Attributes
dx[R]
reminders[R]
Public Class Methods
new(raw_s='reminders.txt', now: Time.now, debug: false)
click to toggle source
Calls superclass method
# File lib/reminders_txt.rb, line 24 def initialize(raw_s='reminders.txt', now: Time.now, debug: false) super() @now, @debug = now, debug puts ('@now: ' + @now.inspect).debug if @debug @filepath = raw_s if raw_s.lines.length > 1 then if raw_s.lstrip[0] == '<' then @filepath = 'reminders.xml' @dx = Dynarex.new raw_s else @filepath = File.join(Dir.pwd, 'reminders.txt') @dxfilepath = @filepath.sub(/.txt$/,'.xml') @dx = Dynarex.new import_txt(raw_s) end elsif File.extname(@filepath) == '.txt' s = FileX.read @filepath @filename = File.basename(@filepath) @dxfilepath = @filepath.sub(/.txt$/,'.xml') import_txt(s) else @dx = Dynarex.new @filepath end end
Public Instance Methods
add(s)
click to toggle source
# File lib/reminders_txt.rb, line 67 def add(s) s.strip! r = EventNlp.new(@now, params: {input: s}).parse(s) return if r.nil? @reminders << r refresh() end
after(d)
click to toggle source
# File lib/reminders_txt.rb, line 78 def after(d) date = d.is_a?(String) ? Chronic.parse(d).to_datetime : d @dx.filter {|x| DateTime.parse(x.date) > date} end
before(d)
click to toggle source
# File lib/reminders_txt.rb, line 85 def before(d) future_date = d.is_a?(String) ? Chronic.parse(d).to_datetime : d @dx.filter {|x| DateTime.parse(x.date) < future_date} end
find(s)
click to toggle source
# File lib/reminders_txt.rb, line 92 def find(s) @dx.filter {|x| x.title =~ /#{s}/i} end
this_week()
click to toggle source
# File lib/reminders_txt.rb, line 119 def this_week() upcoming days: 6 end
Also aliased as: weekahead
this_year()
click to toggle source
# File lib/reminders_txt.rb, line 125 def this_year() upcoming months: 12 end
to_s()
click to toggle source
# File lib/reminders_txt.rb, line 129 def to_s() filename = File.basename(@filepath).sub(/\.xml$/, '.txt') [filename, '=' * filename.length, '', *@dx.all.map(&:input)].join("\n") end
to_xml()
click to toggle source
# File lib/reminders_txt.rb, line 136 def to_xml() @dx.to_xml pretty: true end
today()
click to toggle source
# File lib/reminders_txt.rb, line 111 def today() upcoming 0 end
tomorrow()
click to toggle source
# File lib/reminders_txt.rb, line 115 def tomorrow() upcoming days: 1 end
upcoming(ndays=5, days: ndays, months: nil)
click to toggle source
# File lib/reminders_txt.rb, line 96 def upcoming(ndays=5, days: ndays, months: nil) next_date = if months then @now.to_datetime >> months.to_i else ((@now.to_date + days.to_i + 1).to_time - 1).to_datetime end @dx.filter {|x| DateTime.parse(x.date) <= next_date} end
updated?()
click to toggle source
# File lib/reminders_txt.rb, line 107 def updated?() @updated end
Private Instance Methods
import_txt(s)
click to toggle source
# File lib/reminders_txt.rb, line 142 def import_txt(s) @file_contents = s buffer = s.lines[2..-1] @reminders = buffer.inject([]) do |r, x| x.strip! if (x.length > 1) then rx = EventNlp.new(@now, params: {input: x}, debug: @debug).parse(x) puts ('rx: ' + rx.inspect).debug if @debug r << rx if rx end r end @updated = false refresh() end
refresh()
click to toggle source
synchronise with the XML file and remove any expired dates
# File lib/reminders_txt.rb, line 169 def refresh() puts 'inside refresh()' if @debug reminders = @reminders.clone # if XML file doesn't exist, create it if FileX.exists? @dxfilepath then @dx = Dynarex.new @dxfilepath @reminders.each do |reminder| s = reminder.input puts ('refresh() checking s: ' + s).debug if @debug r = @dx.find_by_input s # it is on file and it's not a recurring or annual event? # use the date from file if the record exists if (r and r.recurring.empty? and not s[/\*$/]) then DateTime.parse(r.date) else if reminder.date then reminder.date.to_datetime else raise RemindersTxtException, 'nil date for reminder : ' \ + reminder.inspect end end end else save_dx() end # delete expired non-recurring reminders @reminders.reject! do |x| if @debug then puts 'rejects filter: ' puts ' x.input: ' + x.input.inspect puts ' x.date.to_time: ' + x.date.to_time.inspect end x.date.to_time < @now if not x.recurring end @reminders.sort_by!(&:date) # did the reminders change? puts 'self.to_s: ' + self.to_s if @debug h1 = (Digest::MD5.new << self.to_s).to_s h2 = (Digest::MD5.new << @file_contents).to_s b = h1 != h2 if @debug then puts 'reminders: ' + reminders.inspect puts '@reminders: ' + @reminders.inspect end if b or @reminders != reminders then save_dx() FileX.write File.join(File.dirname(@filepath), 'reminders.txt'), self.to_s @updated = true else puts 'no update' end [:refresh, b] end
save_detail()
click to toggle source
# File lib/reminders_txt.rb, line 250 def save_detail() # fetch the notes file if it exists filepath = File.dirname @dxfilepath notesfile = File.join(filepath, 'reminder_notes.xml') return unless File.exists? notesfile dx = Dynarex.new notesfile h = dx.all.inject({}) do |r,x| a = x.info.lines tag = a.shift[/\w+/] body = a.join.strip r.merge(tag.to_sym => body) end rows = @dx.all.map do |x| hashtag = x.title[/#(\w+)/,1] hashtag ? x.to_h.merge(info: h[hashtag.to_sym]) : x.to_h end dx2 = Dynarex.new 'reminders/reminder(input, title, recurring, ' + 'date, end_date, venue, info)' dx2.import rows dx2.save File.join(filepath, 'reminder_details.xml') end
save_dx()
click to toggle source
# File lib/reminders_txt.rb, line 282 def save_dx() @dx = Dynarex.new( 'reminders/reminder(input, title, recurring, date, end_date, venue)') @reminders.each {|x| @dx.create x.to_h} @dx.save @dxfilepath save_detail() end