reminders_txt 0.3.6 → 0.3.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b4386e9d7d9e6263ecbe4143ac790eb5af542f6a
4
- data.tar.gz: 985174c50753c0bc67ab75836dc89339f5bf2a8c
3
+ metadata.gz: 1092a4de1fbdc34444d6d9dcb92bea5e4fbf73d4
4
+ data.tar.gz: 5f1f05c6a3c8b8d0b744c39b14cdae771f24eb5b
5
5
  SHA512:
6
- metadata.gz: b57665a159e8bdf3c1f110451168c8c8cc7bdad0c468e6f564681b8d0e5ae2e68b1396c50688187f0e006a94c04e8d54f89ffa4e1cb3b5725b51f748ee6aed86
7
- data.tar.gz: 72453bcca75aa687f7942d458a64c0c441812574d6c42e24e750f745f6ab9aa18cbebd61dd7f969508a3b90273eefee8e55cffbf7261d49466d0b1c373f7ffed
6
+ metadata.gz: e2a84c28265f0c7e9c038927e4e0aafde74a6f625797efb24397ed6148ea6d0990754420bc2c82c55e2a3b11514c009b7651121f990e9b40aae7f319aa0a42f6
7
+ data.tar.gz: bd863d4f8fd03ec2bb558ed0190c88d938936c0939b81926570130e1ca9940ba424fccc51eacbfe3afa7cc8e3e574af086451f6c19bc37fe66ba3d4c02aeb793
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
Binary file
data/lib/reminders_txt.rb CHANGED
@@ -27,101 +27,38 @@ class RemindersTxt
27
27
 
28
28
  attr_reader :reminders
29
29
 
30
- def initialize(filename='reminders.txt', now: Time.now,
31
- dxfilepath: 'reminders.xml')
32
-
33
-
34
- s = File.read filename
35
- @file_contents, @filename = s, filename
36
- @now = now
37
-
38
- Dir.chdir File.dirname(filename)
39
-
40
- @dxfilepath = dxfilepath
41
-
42
- super()
43
- @params = {}
44
- expressions(@params)
45
- buffer = s.lines[2..-1]
30
+ def initialize(filepath='reminders.txt', now: Time.now)
46
31
 
47
- @reminders = buffer.inject([]) do |r, x|
48
- if (x.length > 1) then
49
- @params[:input] = x.strip
50
- rx = find_expression(x)
51
- r << rx
52
- end
53
- r
54
- end
55
-
56
- @updated = false
57
-
58
- update()
59
- end
60
-
61
- def save_dx()
32
+ super()
62
33
 
63
- dx = Dynarex.new(
64
- 'reminders/reminder(input, title, recurring, date, end_date)')
65
- @reminders.each {|x| dx.create x.to_h}
66
- dx.save @dxfilepath
34
+ @now = now
35
+ @filepath = filepath
67
36
 
68
- end
69
-
70
- def refresh()
71
-
72
- # synchronise with the XML file
73
- # if XML file doesn't exist, create it
74
-
75
- if File.exists? @dxfilepath then
76
-
77
- dx = Dynarex.new @dxfilepath
78
-
79
- @reminders.each do |reminder|
80
- s = reminder.input
81
- r = dx.find_by_input s
82
-
83
- # it is on file and it's not an annual event?
84
- # use the date from file if the record exists
85
-
86
- reminder.date = (r and not s[/\*$/]) ? Date.parse(r.date) : \
87
- reminder.date.to_date
88
-
89
- end
90
-
37
+ if File.extname(filepath) == '.txt' then
38
+ import_txt(filepath)
39
+ refresh()
40
+ else
41
+ @dx = Dynarex.new filepath
91
42
  end
43
+ end
92
44
 
93
- # delete expired non-recurring reminders
94
- @reminders.reject! {|x| x.date.to_time < @now if not x.recurring }
95
-
96
- @reminders.sort_by!(&:date)
97
-
98
-
99
- # did the reminders change?
100
-
101
- h1 = (Digest::MD5.new << self.to_s).to_s
102
- h2 = (Digest::MD5.new << @file_contents).to_s
103
-
104
- b = h1 != h2
105
-
106
- if b then
107
-
108
- save_dx()
109
- File.write @filename, self.to_s
110
- @updated = true
111
- end
112
-
113
- [:refresh, b]
114
-
45
+ def upcoming(ndays=5, days: ndays)
46
+ @dx.filter {|x| Date.parse(x.date) <= Date.today + days}
115
47
  end
48
+
49
+ def updated?()
50
+ @updated
51
+ end
116
52
 
117
53
  def to_s()
118
- @file_contents.lines[0..2].join + @reminders.map(&:input).join("\n")
54
+
55
+ filename = File.basename(@filepath).sub(/\.xml$/, '.txt')
56
+ [filename, '=' * filename.length, '', *@dx.all.map(&:input)].join("\n")
57
+
119
58
  end
120
59
 
121
- alias update refresh
122
-
123
- def updated?()
124
- @updated
60
+ def to_xml()
61
+ @dx.to_xml pretty: true
125
62
  end
126
63
 
127
64
  protected
@@ -238,4 +175,94 @@ class RemindersTxt
238
175
 
239
176
  alias find_expression run_route
240
177
 
178
+ private
179
+
180
+ def import_txt(filepath)
181
+
182
+ s = File.read filepath
183
+ @file_contents, @filename = s, File.basename(filepath)
184
+
185
+ #Dir.chdir File.dirname(filename)
186
+
187
+ @dxfilepath = filepath.sub(/.txt$/,'.xml')
188
+
189
+
190
+ @params = {}
191
+ expressions(@params)
192
+ buffer = s.lines[2..-1]
193
+
194
+ @reminders = buffer.inject([]) do |r, x|
195
+ if (x.length > 1) then
196
+ @params[:input] = x.strip
197
+ rx = find_expression(x)
198
+ r << rx
199
+ end
200
+ r
201
+ end
202
+
203
+ @updated = false
204
+
205
+ refresh()
206
+
207
+ end
208
+
209
+ # synchronise with the XML file and remove any expired dates
210
+ #
211
+ def refresh()
212
+
213
+
214
+ # if XML file doesn't exist, create it
215
+
216
+ if File.exists? @dxfilepath then
217
+
218
+ @dx = Dynarex.new @dxfilepath
219
+
220
+ @reminders.each do |reminder|
221
+ s = reminder.input
222
+ r = @dx.find_by_input s
223
+
224
+ # it is on file and it's not an annual event?
225
+ # use the date from file if the record exists
226
+
227
+ reminder.date = (r and not s[/\*$/]) ? Date.parse(r.date) : \
228
+ reminder.date.to_date
229
+
230
+ end
231
+
232
+ end
233
+
234
+ # delete expired non-recurring reminders
235
+ @reminders.reject! {|x| x.date.to_time < @now if not x.recurring }
236
+
237
+ @reminders.sort_by!(&:date)
238
+
239
+
240
+ # did the reminders change?
241
+
242
+ h1 = (Digest::MD5.new << self.to_s).to_s
243
+ h2 = (Digest::MD5.new << @file_contents).to_s
244
+
245
+ b = h1 != h2
246
+
247
+ if b then
248
+
249
+ save_dx()
250
+ File.write @filename, self.to_s
251
+ @updated = true
252
+ end
253
+
254
+ [:refresh, b]
255
+
256
+ end
257
+
258
+ def save_dx()
259
+
260
+ @dx = Dynarex.new(
261
+ 'reminders/reminder(input, title, recurring, date, end_date)')
262
+ @reminders.each {|x| @dx.create x.to_h}
263
+ @dx.save @dxfilepath
264
+
265
+ end
266
+
267
+
241
268
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reminders_txt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -31,7 +31,7 @@ cert_chain:
31
31
  n+SSd9dm8JjhZ2pqHtX+bgMZ18hrN06xfR+UPtk5BKC8v1PC0FWxaVR4lom39rFU
32
32
  seDxJNsJgamAKg==
33
33
  -----END CERTIFICATE-----
34
- date: 2016-11-19 00:00:00.000000000 Z
34
+ date: 2016-12-24 00:00:00.000000000 Z
35
35
  dependencies:
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: dynarex
metadata.gz.sig CHANGED
Binary file