class MonthlyPlanner
Attributes
to_s[R]
Public Class Methods
new(filename='monthly-planner.txt', path: '.', today: Date.today)
click to toggle source
# File lib/monthly_planner.rb, line 32 def initialize(filename='monthly-planner.txt', path: '.', today: Date.today) # is it a Dynarex file s, type = RXFHelper.read(filename) return @dx = Dynarex.new(s) if type == :url or s =~ /^</ @filename, @path = filename, path fpath = File.join(path, filename) if File.exists?(fpath) then @dx = import_to_dx(File.read(fpath)) sync_archive(@dx.all) # purge any past dates @dx.all.reject! {|x| x.date < today} else @dx = new_dx end end
Public Instance Methods
dx()
click to toggle source
# File lib/monthly_planner.rb, line 56 def dx() @dx end
save(filename=@filename)
click to toggle source
# File lib/monthly_planner.rb, line 60 def save(filename=@filename) s = File.basename(filename) + "\n" + dx_to_s(@dx).lines[1..-1].join File.write File.join(@path, filename), s @dx.save File.join(@path, filename.sub(/\.txt$/,'.xml')) end
this_week()
click to toggle source
# File lib/monthly_planner.rb, line 68 def this_week() @dx.all.select {|x| x.date < Date.today + 7 } end
Private Instance Methods
dx_to_s(dx)
click to toggle source
# File lib/monthly_planner.rb, line 80 def dx_to_s(dx) title = File.basename(@filename) title + "\n" + "=" * title.length + "\n\n%s\n\n" % [dx.to_s(header: false)] end
import_to_dx(s)
click to toggle source
# File lib/monthly_planner.rb, line 87 def import_to_dx(s) regexp = %r{ (?<day>\d+(?:-|th|[rn]d|st|\s+)){0} (?<month>#{Date::ABBR_MONTHNAMES[1..-1].join('|')}){0} (?<timex>\d+(?:\:\d+)?(?:[ap]m)?){0} (?<time>\g<timex>(?:-?\s?\g<timex>)?){0} ^(?<date>\g<day>\s*\g<month>)\s*\g<time>?\s*(?<event>.*) }x dx = new_dx() a = LineTree.new(s).to_a # Remove the heading a.shift 2 a.each do |line| r = line[0].match(regexp) dx.create({date: r['date'], time: r['time'], desc: r['event']}) end return dx end
new_dx()
click to toggle source
# File lib/monthly_planner.rb, line 114 def new_dx() dx = Dynarex.new 'month[title]/day(date, time, desc)', default_key: :uid d = DateTime.now.to_date title = [d, d.next_month].map {|x| x.strftime("%b")}.join(' - ') dx.title = "Monthly Planner (%s)" % title return dx end
sync_archive(dxrows=@dx.all)
click to toggle source
# File lib/monthly_planner.rb, line 126 def sync_archive(dxrows=@dx.all) # group by month a = dxrows.group_by {|x| x.date.strftime("%b").downcase} # add the file to the archive. a.each do |month, days| d = days.first.date archive_path = File.join(@path, d.year.to_s, month) FileUtils.mkdir_p archive_path filepath = File.join archive_path, 'mp.xml' if File.exists? filepath then dx2 = Dynarex.new filepath days.each do |day| # determine if the entry already exists in the Dynarex doc rows = dx2.to_s(header: false).lines.map(&:chomp) fm = FuzzyMatch.new rows found, score = fm.find_with_score %i(date time desc)\ .map {|y| day[y]}.join ' ' if score < 0.7 then dx2.create(day) else i = rows.index found dx2.update(dx2.all[i].id, day) end end else # if the index.xml file doesn't exist just save them dx2 = Dynarex.new dx.schema dx2.title = "Monthly Planner #{month.capitalize}-#{d.year}" dx2.import days end dx2.save filepath end end