polyrex_calendarbase 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 87af5913bdf544a500b425d47caaa71ed7771dbc
4
+ data.tar.gz: 9bd96cb1d3b37da781da2d50a9aa0248d784580d
5
+ SHA512:
6
+ metadata.gz: 466d480495e1c31625932b611b537450c787a8626920e14bbade357be6275f58a219d17a927e6658d44579c15f11266a6881f5923f5d2a743a9f6f8ee0993b89
7
+ data.tar.gz: acefe190651c6c1f7a7f8c2b453f4d510ee6effa5639d811400fe57aef870e1c277812a692880f0a628192a3c837cec6ad5670e02f965f519ac553cc0e4df910
checksums.yaml.gz.sig ADDED
Binary file
@@ -0,0 +1,409 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # file: polyrex_calendarbase.rb
4
+
5
+ require 'polyrex'
6
+ require 'date'
7
+ require 'nokogiri'
8
+ require 'chronic_duration'
9
+ require 'chronic_cron'
10
+ require 'rxfhelper'
11
+
12
+
13
+ h = {
14
+ calendar: 'calendar[year]',
15
+ month: 'month[n, title]',
16
+ week: 'week[n, mon, no, label]',
17
+ day: 'day[sdate, xday, event, bankholiday, title, sunrise, sunset]',
18
+ entry: 'entry[time_start, time_end, duration, title]'
19
+ }
20
+ visual_schema = h.values.join '/'
21
+
22
+ class CalendarObjects < PolyrexObjects
23
+ end
24
+
25
+ CalendarObjects.new(visual_schema)
26
+
27
+ module LIBRARY
28
+
29
+ def fetch_file(filename)
30
+
31
+ lib = File.dirname(__FILE__)
32
+ File.read filename
33
+
34
+ end
35
+
36
+ def generate_webpage(xml, xsl)
37
+
38
+ # transform the xml to html
39
+ doc = Nokogiri::XML(xml)
40
+ xslt = Nokogiri::XSLT(xsl)
41
+ xslt.transform(doc).to_s
42
+ end
43
+
44
+ def read(s)
45
+ RXFHelper.read(s).first
46
+ end
47
+ end
48
+
49
+ class Calendar < Polyrex
50
+ include LIBRARY
51
+
52
+ attr_accessor :xslt, :css_layout, :css_style, :filename
53
+
54
+ alias months records
55
+
56
+ def inspect()
57
+ "#<Calendar:%s" % __id__
58
+ end
59
+
60
+ def to_webpage()
61
+
62
+ year_xsl = self.xslt ? read(self.xslt) : fetch_file(self.xslt)
63
+ year_layout_css = fetch_file self.css_layout
64
+ year_css = fetch_file self.css_style
65
+ File.open('self.xml','w'){|f| f.write (self.to_xml pretty: true)}
66
+ File.open(self.xslt,'w'){|f| f.write year_xsl }
67
+ #html = Rexslt.new(month_xsl, self.to_xml).to_xml
68
+
69
+ html = generate_webpage self.to_xml, year_xsl
70
+ {self.filename => html,
71
+ self.css_layout => year_layout_css, self.css_style => year_css}
72
+
73
+ end
74
+ end
75
+
76
+ class CalendarObjects
77
+
78
+ class Month
79
+ include LIBRARY
80
+
81
+ attr_accessor :xslt, :css_layout, :css_style
82
+
83
+ def inspect()
84
+ "#<CalendarObjects::Month:%s" % __id__
85
+ end
86
+
87
+ def wk(n)
88
+ self.records[n-1]
89
+ end
90
+ end
91
+
92
+ class Week
93
+ include LIBRARY
94
+
95
+ def inspect()
96
+ "#<CalendarObjects::Week:%s" % __id__
97
+ end
98
+
99
+ end
100
+
101
+ class Day
102
+
103
+ def date()
104
+ Date.parse(self.sdate)
105
+ end
106
+
107
+ def wday()
108
+ self.date.wday
109
+ end
110
+
111
+ def day()
112
+ self.date.day
113
+ end
114
+
115
+ end
116
+
117
+ end
118
+
119
+
120
+ class PolyrexCalendarBase
121
+ include LIBRARY
122
+
123
+ attr_accessor :xsl, :css, :calendar, :month_xsl, :month_css
124
+ attr_reader :day
125
+
126
+ def initialize(calendar_file=nil, options={})
127
+
128
+ opts = {year: Time.now.year.to_s}.merge(options)
129
+ @year = opts[:year]
130
+
131
+ h = {
132
+ calendar: 'calendar[year]',
133
+ month: 'month[n, title]',
134
+ week: 'week[n]',
135
+ day: 'day[sdate, xday, event, bankholiday, title, sunrise, sunset]',
136
+ entry: 'entry[time_start, time_end, duration, title]'
137
+ }
138
+ @schema = %i(calendar month day entry).map{|x| h[x]}.join '/'
139
+ @visual_schema = h.values.join '/'
140
+
141
+
142
+ if calendar_file then
143
+ @calendar = Calendar.new calendar_file
144
+ @id = @calendar.id_counter
145
+
146
+ else
147
+ @id = '1'
148
+ # generate the calendar
149
+
150
+ a = (Date.parse(@year + '-01-01')...Date.parse(@year.succ + '-01-01')).to_a
151
+
152
+ @calendar = Calendar.new(@schema, id_counter: @id)
153
+ @calendar.summary.year = @year
154
+
155
+ a.group_by(&:month).each do |month, days|
156
+
157
+ @calendar.create.month no: month.to_s, title: Date::MONTHNAMES[month] do |create|
158
+ days.each do |x|
159
+ create.day sdate: x.strftime("%Y-%b-%d"), xday: x.day.to_s, title: Date::DAYNAMES[x.wday]
160
+ end
161
+ end
162
+ end
163
+
164
+ end
165
+
166
+ visual_schema = h.values.join '/'
167
+ CalendarObjects.new(visual_schema)
168
+
169
+ end
170
+
171
+ def to_xml()
172
+ @calendar.to_xml pretty: true
173
+ end
174
+
175
+ def import_events(objx)
176
+ @id = @calendar.id_counter
177
+ method('import_'.+(objx.class.to_s.downcase).to_sym).call(objx)
178
+ self
179
+ end
180
+
181
+ alias import! import_events
182
+
183
+ def inspect()
184
+ %Q(=> #<PolyrexCalendar:#{self.object_id} @id="#{@id}", @year="#{@year}">)
185
+ end
186
+
187
+ def parse_events(list)
188
+
189
+ polyrex = Polyrex.new('events/dayx[date, title]/entryx[start_time, end_time,' + \
190
+ ' duration, title]')
191
+
192
+ polyrex.format_masks[1] = '([!start_time] \([!duration]\) [!title]|' + \
193
+ '[!start_time]-[!end_time] [!title]|' + \
194
+ '[!start_time] [!title])'
195
+
196
+ polyrex.parse(list)
197
+
198
+ self.import_events polyrex
199
+ # for testing only
200
+ #polyrex
201
+ end
202
+
203
+ def save(filename='calendar.xml')
204
+ @calendar.save filename
205
+ end
206
+
207
+ def this_week()
208
+
209
+ dt = DateTime.now
210
+ days = @calendar.records[dt.month-1].day
211
+
212
+ r = days.find {|day| day.date.cweek == dt.cweek }
213
+ pxweek = CalendarObjects::Week.new
214
+ pxweek.mon = Date::MONTHNAMES[dt.month]
215
+ pxweek.no = dt.cweek.to_s
216
+ pxweek.label = ''
217
+ days[days.index(r),7].each{|day| pxweek.add day }
218
+
219
+ pxweek
220
+ end
221
+
222
+ def this_month()
223
+ self.month(DateTime.now.month)
224
+ end
225
+
226
+ def import_bankholidays(dynarex)
227
+ import_dynarex(dynarex, :bankholiday=)
228
+ end
229
+
230
+ def import_recurring_events(dynarex)
231
+
232
+ title = dynarex.summary[:event_title]
233
+ cc = ChronicCron.new dynarex.summary[:description].split(/,/,2).first
234
+ time_start= "%s:%02d" % cc.to_time.to_a.values_at(2,1)
235
+
236
+ dynarex.flat_records.each do |event|
237
+
238
+ dt = DateTime.parse(event[:date])
239
+ m, d = dt.month, dt.day
240
+ record = {title: title, time_start: time_start}
241
+
242
+ @calendar.records[m-1].day[d-1].create.entry record
243
+ end
244
+ end
245
+
246
+ def import_sunrise_times(dynarex)
247
+ import_suntimes dynarex, :sunrise=
248
+ end
249
+
250
+ def import_sunset_times(dynarex)
251
+ import_suntimes dynarex, :sunset=
252
+ end
253
+
254
+ private
255
+
256
+ def import_suntimes(dynarex, event_type=:sunrise=)
257
+
258
+ dynarex.flat_records.each do |event|
259
+
260
+ dt = DateTime.parse(event[:date])
261
+ m, d = dt.month, dt.day
262
+ @calendar.records[m-1].day[d-1].method(event_type).call event[:time]
263
+ end
264
+ end
265
+
266
+ def import_dynarex(dynarex, daytype=:event=)
267
+
268
+ dynarex.flat_records.each do |event|
269
+
270
+ dt = DateTime.parse(event[:date])
271
+ m, d = dt.month, dt.day
272
+
273
+ case daytype
274
+
275
+ when :event=
276
+
277
+ if dynarex.fields.include?(:time) then
278
+
279
+ match = event[:time].match(/(\S+)\s*(?:to|-)\s*(\S+)/)
280
+
281
+ if match then
282
+
283
+ start_time, end_time = match.captures
284
+ # add an event entry
285
+ title = [event[:title], dynarex.summary[:label],
286
+ event[:desc]].compact.join(': ')
287
+ record = {
288
+ title: title,
289
+ time_start: Time.parse(start_time).strftime("%H:%M%p"),
290
+ time_end: Time.parse(end_time).strftime("%H:%M%p")
291
+ }
292
+
293
+ @calendar.records[m-1].day[d-1].create.entry record
294
+ else
295
+
296
+ dt = DateTime.parse(event[:date] + ' ' + event[:time])
297
+ # add the event
298
+ title = [event[:title], dynarex.summary[:label],
299
+ event[:desc]].compact.join(': ')
300
+ event_label = "%s at %s" % [title, dt.strftime("%H:%M%p")]
301
+
302
+ @calendar.records[m-1].day[d-1].method(daytype).call event_label
303
+ end
304
+
305
+ else
306
+
307
+ event_label = "%s at %s" % [event[:title], dt.strftime("%H:%M%p")]
308
+ @calendar.records[m-1].day[d-1].method(daytype).call event_label
309
+ end
310
+
311
+ else
312
+
313
+ event_label = "%s" % event[:title]
314
+ @calendar.records[m-1].day[d-1].method(daytype).call event_label
315
+ end
316
+
317
+ end
318
+ end
319
+
320
+ def import_polyrex(polyrex)
321
+
322
+ polyrex.records.each do |day|
323
+
324
+ dt = day.date
325
+
326
+ sd = dt.strftime("%Y-%b-%d ")
327
+ m, i = dt.month, dt.day
328
+ cal_day = @calendar.records[m - 1].day[i-1]
329
+
330
+ cal_day.event = day.title
331
+
332
+ if day.records.length > 0 then
333
+
334
+ raw_entries = day.records
335
+
336
+ entries = raw_entries.inject({}) do |r,entry|
337
+
338
+ start_time = entry.start_time
339
+
340
+ if entry.end_time.length > 0 then
341
+
342
+ end_time = entry.end_time
343
+ duration = ChronicDuration.output(Time.parse(sd + end_time) \
344
+ - Time.parse(sd + start_time))
345
+ else
346
+
347
+ if entry.duration.length > 0 then
348
+ duration = entry.duration
349
+ else
350
+ duration = '10 mins'
351
+ end
352
+
353
+ end_time = (Time.parse(sd + start_time) + ChronicDuration.parse(duration))\
354
+ .strftime("%H:%M")
355
+ end
356
+
357
+ r.merge!(start_time => {time_start: start_time, time_end: end_time, \
358
+ duration: duration, title: entry.title})
359
+
360
+ end
361
+
362
+ seconds = entries.keys.map{|x| Time.parse(x) - Time.parse('08:00')}
363
+
364
+ unless dt.saturday? or dt.sunday? then
365
+ rows = slotted_sort(seconds).map do |x|
366
+ (Time.parse('08:00') + x.to_i).strftime("%H:%M") if x
367
+ end
368
+ else
369
+ rows = entries.keys
370
+ end
371
+
372
+ rows.each do |row|
373
+ create = cal_day.create
374
+ create.id = @id
375
+ create.entry entries[row] || {}
376
+ end
377
+
378
+ end
379
+ end
380
+
381
+ end
382
+
383
+ def slotted_sort(a)
384
+
385
+ upper = 36000 # upper slot value
386
+ slot_width = 9000 # 2.5 hours
387
+ max_slots = 3
388
+
389
+ b = a.reverse.inject([]) do |r,x|
390
+
391
+ upper ||= 10; i ||= 0
392
+ diff = upper - x
393
+
394
+ if diff >= slot_width and (i + a.length) < max_slots then
395
+ r << nil
396
+ i += 1
397
+ upper -= slot_width
398
+ redo
399
+ else
400
+ upper -= slot_width if x <= upper
401
+ r << x
402
+ end
403
+ end
404
+
405
+ a = b.+([nil] * max_slots).take(max_slots).reverse
406
+ end
407
+
408
+ end
409
+
data.tar.gz.sig ADDED
Binary file
metadata ADDED
@@ -0,0 +1,167 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: polyrex_calendarbase
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - James Robertson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIDljCCAn6gAwIBAgIBATANBgkqhkiG9w0BAQUFADBIMRIwEAYDVQQDDAlnZW1t
14
+ YXN0ZXIxHjAcBgoJkiaJk/IsZAEZFg5qYW1lc3JvYmVydHNvbjESMBAGCgmSJomT
15
+ 8ixkARkWAmV1MB4XDTE0MDkyOTIwMDkxN1oXDTE1MDkyOTIwMDkxN1owSDESMBAG
16
+ A1UEAwwJZ2VtbWFzdGVyMR4wHAYKCZImiZPyLGQBGRYOamFtZXNyb2JlcnRzb24x
17
+ EjAQBgoJkiaJk/IsZAEZFgJldTCCASIw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Z
35
+ dependencies:
36
+ - !ruby/object:Gem::Dependency
37
+ name: polyrex
38
+ requirement: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '0.9'
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: 0.9.3
46
+ type: :runtime
47
+ prerelease: false
48
+ version_requirements: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - "~>"
51
+ - !ruby/object:Gem::Version
52
+ version: '0.9'
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 0.9.3
56
+ - !ruby/object:Gem::Dependency
57
+ name: nokogiri
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '1.6'
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 1.6.2.1
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - "~>"
71
+ - !ruby/object:Gem::Version
72
+ version: '1.6'
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 1.6.2.1
76
+ - !ruby/object:Gem::Dependency
77
+ name: chronic_duration
78
+ requirement: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.10'
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: 0.10.4
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - "~>"
91
+ - !ruby/object:Gem::Version
92
+ version: '0.10'
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: 0.10.4
96
+ - !ruby/object:Gem::Dependency
97
+ name: chronic_cron
98
+ requirement: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '0.2'
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: 0.2.33
106
+ type: :runtime
107
+ prerelease: false
108
+ version_requirements: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - "~>"
111
+ - !ruby/object:Gem::Version
112
+ version: '0.2'
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: 0.2.33
116
+ - !ruby/object:Gem::Dependency
117
+ name: rxfhelper
118
+ requirement: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - "~>"
121
+ - !ruby/object:Gem::Version
122
+ version: '0.1'
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: 0.1.12
126
+ type: :runtime
127
+ prerelease: false
128
+ version_requirements: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - "~>"
131
+ - !ruby/object:Gem::Version
132
+ version: '0.1'
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: 0.1.12
136
+ description:
137
+ email: james@r0bertson.co.uk
138
+ executables: []
139
+ extensions: []
140
+ extra_rdoc_files: []
141
+ files:
142
+ - lib/polyrex_calendarbase.rb
143
+ homepage: https://github.com/jrobertson/polyrex_calendarbase
144
+ licenses:
145
+ - MIT
146
+ metadata: {}
147
+ post_install_message:
148
+ rdoc_options: []
149
+ require_paths:
150
+ - lib
151
+ required_ruby_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ required_rubygems_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ requirements: []
162
+ rubyforge_project:
163
+ rubygems_version: 2.2.2
164
+ signing_key:
165
+ specification_version: 4
166
+ summary: Provides building blocks to create a calendar from a Polyrex document object
167
+ test_files: []
metadata.gz.sig ADDED
@@ -0,0 +1 @@
1
+ ��I�W2�ͳ��w6�(�kJE"ek"����)���$� �"�z�F���:���� =�.���Ղ��z#YUL����!�#�5�j7�~�F����>��^�� ��֮o��֪˷�T��Yr9{??yI@Q�u�����R���c����+g��[�J2��