Class: RemindersTxt
- Inherits:
-
Object
- Object
- RemindersTxt
- Includes:
- RXFReadWriteModule
- Defined in:
- lib/reminders_txt.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#dx ⇒ Object
readonly
Returns the value of attribute dx.
-
#reminders ⇒ Object
readonly
Returns the value of attribute reminders.
Instance Method Summary collapse
- #add(obj) ⇒ Object
- #after(d) ⇒ Object
- #before(d) ⇒ Object
- #find(s) ⇒ Object
- #import_vcs(filename) ⇒ Object
-
#initialize(raw_s = 'reminders.txt', now: Time.now, debug: false) ⇒ RemindersTxt
constructor
A new instance of RemindersTxt.
- #this_month ⇒ Object
- #this_week ⇒ Object (also: #weekahead)
- #this_year ⇒ Object
- #to_s ⇒ Object
- #to_xml ⇒ Object
- #today ⇒ Object
- #tomorrow ⇒ Object
- #upcoming(ndays = 5, days: ndays, months: nil) ⇒ Object
- #updated? ⇒ Boolean
Constructor Details
#initialize(raw_s = 'reminders.txt', now: Time.now, debug: false) ⇒ RemindersTxt
Returns a new instance of RemindersTxt.
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/reminders_txt.rb', line 31 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(DirX.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 |
Instance Attribute Details
#dx ⇒ Object (readonly)
Returns the value of attribute dx.
29 30 31 |
# File 'lib/reminders_txt.rb', line 29 def dx @dx end |
#reminders ⇒ Object (readonly)
Returns the value of attribute reminders.
29 30 31 |
# File 'lib/reminders_txt.rb', line 29 def reminders @reminders end |
Instance Method Details
#add(obj) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/reminders_txt.rb', line 74 def add(obj) if obj.is_a? String then s = obj s.strip! r = EventNlp.new(@now, params: {input: s}).parse(s) return if r.nil? elsif obj.is_a? OpenStruct r = obj end @reminders << r refresh() end |
#after(d) ⇒ Object
94 95 96 97 98 99 |
# File 'lib/reminders_txt.rb', line 94 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) ⇒ Object
101 102 103 104 105 106 |
# File 'lib/reminders_txt.rb', line 101 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) ⇒ Object
108 109 110 |
# File 'lib/reminders_txt.rb', line 108 def find(s) @dx.filter {|x| x.title =~ /#{s}/i} end |
#import_vcs(filename) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/reminders_txt.rb', line 112 def import_vcs(filename) puts 'filename: ' + filename.inspect if @debug icsfile = FileX.read filename cal = Vpim::Icalendar.decode(icsfile).first puts '*********************' if @debug r = cal.components[0] datestart = "%s %s" % [r.dtstart.day.ordinal, r.dtstart.strftime("%B %Y at %H:%M%P")] event = "%s: %s at %s %s" % [r.categories.join(', '), r.summary, r.location, datestart] if r.dtend then if r.dtstart.to_date == r.dtend.to_date then event += '-' + r.dtend.strftime("%H:%M%P") else dateend = "%s %s" % [r.dtend.day.ordinal, r.dtend.strftime("%B %Y at %H:%M%P")] event += ' to ' + dateend end end h = { input: event, title: "%s: %s" % [r.categories.join(', '), r.summary], date: r.dtstart } h[:venue] = r.location if r.location h[:end_date] = r.dtend if r.dtend add OpenStruct.new(h) end |
#this_month ⇒ Object
175 176 177 |
# File 'lib/reminders_txt.rb', line 175 def this_month() upcoming months: 1 end |
#this_week ⇒ Object Also known as: weekahead
169 170 171 |
# File 'lib/reminders_txt.rb', line 169 def this_week() upcoming days: 6 end |
#this_year ⇒ Object
179 180 181 |
# File 'lib/reminders_txt.rb', line 179 def this_year() upcoming months: 12 end |
#to_s ⇒ Object
183 184 185 186 187 188 |
# File 'lib/reminders_txt.rb', line 183 def to_s() filename = File.basename(@filepath).sub(/\.xml$/, '.txt') [filename, '=' * filename.length, '', *@dx.all.map(&:input)].join("\n") end |
#to_xml ⇒ Object
190 191 192 |
# File 'lib/reminders_txt.rb', line 190 def to_xml() @dx.to_xml pretty: true end |
#today ⇒ Object
161 162 163 |
# File 'lib/reminders_txt.rb', line 161 def today() upcoming 0 end |
#tomorrow ⇒ Object
165 166 167 |
# File 'lib/reminders_txt.rb', line 165 def tomorrow() upcoming days: 1 end |
#upcoming(ndays = 5, days: ndays, months: nil) ⇒ Object
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/reminders_txt.rb', line 146 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? ⇒ Boolean
157 158 159 |
# File 'lib/reminders_txt.rb', line 157 def updated?() @updated end |