polyrex-calendar 0.1.12 → 0.1.13

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: 5986e6657ce73300d4e51abaf62eeb48ef0b5c40
4
- data.tar.gz: 0487cf7bf36941a86872156073e11c89d8e33e35
3
+ metadata.gz: 43046cae49a8bb07a5549b4348acfb3ad8e662f3
4
+ data.tar.gz: c02d638727efa111ff15a4a0dc96eafa00b23880
5
5
  SHA512:
6
- metadata.gz: a7a00229d4f8d9a32818c0a077b87323c4a831f6b94009590b847307c51939274902a7b6f05b60edc0415df3f83cc4a323382c286c98a337608419f8db208f69
7
- data.tar.gz: b7072d188820f64339a2d063910b99728da466eb6dbcde92f42d2a51b56e274e5b033d6dfda37eb72328273b713841a738b244d2c52a8a3d84ca6406418d49c5
6
+ metadata.gz: 53d5cc3d4c823c2fa18b18c22d8a401e94def4db6fa55b1d9f2f1dac7e01e97f69c85f606941e865d190baaabff380c1ba6a2b3e89f2867e5e123c90de9a24a1
7
+ data.tar.gz: 26be0a97522f12ed84ec7e4284b7619c1baa1b1d5276bdb694256ef3ca95eafbe58077495e7d051ad009be8b867d411a78a8803456475206f82541e48c1f81d2
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -5,31 +5,75 @@
5
5
  require 'polyrex'
6
6
  require 'date'
7
7
  require 'nokogiri'
8
+ require 'chronic_duration'
9
+
10
+ class Numeric
11
+ def ordinal
12
+ ( (10...20).include?(self) ? 'th' : %w{ th st nd rd th th th th th th }[self % 10] )
13
+ end
14
+ end
8
15
 
9
16
  class PolyrexCalendar
10
17
 
11
18
  attr_accessor :xsl, :css, :polyrex, :month_xsl, :month_css
12
19
 
13
- def initialize(year=nil)
20
+ def initialize(year=nil, options={})
21
+
22
+ @id = '1'
23
+
24
+ opts = {calendar_xsl: lib + '/calendar.xsl'
25
+ }.merge(options)
14
26
  @year = year ? year.to_s : Time.now.year.to_s
15
27
  generate_calendar
16
28
  lib = File.dirname(__FILE__)
17
- @xsl = File.open(lib + '/calendar.xsl','r').read
18
- @css = File.open(lib + '/layout.css','r').read
19
- @month_xsl = File.open(lib + '/month_calendar.xsl','r').read
20
- @month_css = File.open(lib + '/month_layout.css','r').read
21
29
 
22
- PolyrexObjects::Month.class_eval {
30
+ @xsl = File.read lib + '/calendar.xsl'
31
+ @css = File.read lib + '/layout.css'
32
+
33
+ PolyrexObjects::Month.class_eval do
34
+
35
+ def inspect()
36
+ "#<PolyrexObjects::Month:%s" % __id__
37
+ end
38
+
23
39
  def to_webpage()
24
40
  lib = File.dirname(__FILE__)
25
- month_xsl = File.open(lib + '/month_calendar.xsl','r').read
26
- month_css = File.open(lib + '/month_layout.css','r').read
27
- doc = Nokogiri::XML(self.to_xml); xslt = Nokogiri::XSLT(month_xsl)
28
- html = xslt.transform(doc).to_xml
41
+
42
+ month_xsl = File.read lib + '/month_calendar.xsl'
43
+ month_css = File.read lib + '/month_layout.css'
44
+
45
+ xslt = Nokogiri::XSLT(month_xsl)
46
+ html = xslt.transform(Nokogiri::XML(self.to_xml)).to_s
29
47
  {self.name.downcase[0..2] + '_calendar.html' => html, 'month_layout.css' => month_css}
48
+
49
+ end
50
+
51
+ def wk(n)
52
+ self.records[n-1]
53
+ end
54
+ end
55
+
56
+ PolyrexObjects::Week.class_eval do
57
+
58
+ def inspect()
59
+ "#<PolyrexObjects::Week:%s" % __id__
30
60
  end
31
- }
61
+
62
+ def to_webpage()
63
+ lib = File.dirname(__FILE__)
64
+
65
+ week_xsl = File.read lib + '/week_calendar.xsl'
66
+ week_layout_css = File.read lib + '/week_layout.css'
67
+ week_css = File.read lib + '/week.css'
32
68
 
69
+ xslt = Nokogiri::XSLT(week_xsl)
70
+ html = xslt.transform(Nokogiri::XML(self.to_xml)).to_s
71
+ {'week' + self.no + '_planner.html' => html, 'week_layout.css' => week_layout_css, \
72
+ 'week.css' => week_css}
73
+ end
74
+
75
+ end
76
+
33
77
  end
34
78
 
35
79
  def to_a()
@@ -40,18 +84,23 @@ class PolyrexCalendar
40
84
  @polyrex.to_xml pretty: true
41
85
  end
42
86
 
43
- def to_webpage()
87
+ def to_webpage()
88
+ #html = Rexslt.new(@xsl, @polyrex.to_xml).to_xml
44
89
  html = generate_webpage(@polyrex.to_xml, @xsl)
45
90
  {'calendar.html' => html, 'layout.css' => @css}
46
91
  end
47
92
 
48
- def import_events(dynarex)
49
- dynarex.flat_records.each do |event|
50
- m,w,i = @day[Date.parse(event[:date])]
51
- @polyrex.records[m].week[w].day[i].event = event[:title]
52
- end
93
+ def import_events(objx)
94
+ @id = @polyrex.id_counter
95
+ method('import_'.+(objx.class.to_s.downcase).to_sym).call(objx)
53
96
  self
54
97
  end
98
+
99
+ alias import! import_events
100
+
101
+ def inspect()
102
+ %Q(=> #<PolyrexCalendar:#{self.object_id} @id="#{@id}", @year="#{@year}">)
103
+ end
55
104
 
56
105
  def month(m)
57
106
  @polyrex.records[m-1]
@@ -60,8 +109,105 @@ class PolyrexCalendar
60
109
  def months
61
110
  @polyrex.records
62
111
  end
112
+
113
+ def parse_events(list)
114
+
115
+ polyrex = Polyrex.new('events/dayx[date, title]/entryx[start_time, end_time,' + \
116
+ ' duration, title]')
117
+
118
+ polyrex.format_masks[1] = '([!start_time] \([!duration]\) [!title]|' + \
119
+ '[!start_time]-[!end_time] [!title]|' + \
120
+ '[!start_time] [!title])'
121
+
122
+ polyrex.parse(list)
123
+
124
+ self.import_events polyrex
125
+ end
126
+
127
+ def this_week()
128
+ m = DateTime.now.month
129
+ self.month(m).records.find do |week|
130
+ now = DateTime.now
131
+ week_no = now.cwday < 7 ? now.cweek - 1: now.cweek
132
+ week.no == week_no.to_s
133
+ end
134
+ end
63
135
 
136
+ def this_month()
137
+ self.month(DateTime.now.month)
138
+ end
139
+
64
140
  private
141
+
142
+ def import_dynarex(dynarex)
143
+ dynarex.flat_records.each do |event|
144
+ m,w,i = @day[Date.parse(event[:date])]
145
+ @polyrex.records[m].week[w].day[i].event = event[:title]
146
+ end
147
+ end
148
+
149
+ def import_polyrex(polyrex)
150
+
151
+ polyrex.records.each do |day|
152
+
153
+ d1 = Date.parse(day.date)
154
+ sd = d1.strftime("%Y-%b-%d ")
155
+ m,w,i = @day[d1]
156
+
157
+ cal_day = @polyrex.records[m].week[w].day[i]
158
+
159
+ cal_day.event = day.title
160
+
161
+ if day.records.length > 0 then
162
+
163
+ raw_entries = day.records
164
+
165
+ entries = raw_entries.inject({}) do |r,entry|
166
+
167
+ start_time = entry.start_time
168
+
169
+ if entry.end_time then
170
+ end_time = entry.end_time
171
+ duration = ChronicDuration.output(Time.parse(sd + end_time) \
172
+ - Time.parse(sd + start_time))
173
+ else
174
+
175
+ if entry.duration then
176
+ duration = entry.duration
177
+ else
178
+ duration = '10 mins'
179
+ end
180
+
181
+ end_time = (Time.parse(sd + start_time) + ChronicDuration.parse(duration))\
182
+ .strftime("%H:%M")
183
+ end
184
+
185
+ r.merge!(start_time => {time_start: start_time, time_end: end_time, \
186
+ duration: duration, title: entry.title})
187
+
188
+ end
189
+
190
+ seconds = entries.keys.map{|x| Time.parse(x) - Time.parse('08:00')}
191
+
192
+ unless d1.saturday? or d1.sunday? then
193
+ rows = slotted_sort(seconds).map do |x|
194
+ (Time.parse('08:00') + x.to_i).strftime("%H:%M") if x
195
+ end
196
+ else
197
+ rows = entries.keys
198
+ end
199
+
200
+ rows.each do |row|
201
+ create = cal_day.create
202
+ create.id = @id
203
+ create.entry entries[row] || {}
204
+ end
205
+
206
+ end
207
+ end
208
+
209
+ end
210
+
65
211
 
66
212
  def generate_calendar()
67
213
 
@@ -90,8 +236,10 @@ class PolyrexCalendar
90
236
  end
91
237
 
92
238
  @a = months
93
-
94
- @polyrex = Polyrex.new('calendar[year]/month[no,name,year]/week[no, rel_no]/day[wday, xday, name, event, date]')
239
+ schema = 'calendar[year]/month[no,name,year]/week[no, rel_no, mon, label]/' + \
240
+ 'day[wday, xday, name, event, date, ordinal, overlap]/' + \
241
+ 'entry[time_start, time_end, duration, title]'
242
+ @polyrex = Polyrex.new(schema, id_counter: @id)
95
243
  year_start = months[0][0][-1]
96
244
  @polyrex.summary.year = @year
97
245
  old_year_week = (year_start - 7).cweek
@@ -99,24 +247,46 @@ class PolyrexCalendar
99
247
  week_i = 0
100
248
 
101
249
  months.each_with_index do |month,i|
102
-
103
- @polyrex.create.month no: (i+1).to_s, name: Date::MONTHNAMES[i+1], year: @year do |create|
250
+ month_name = Date::MONTHNAMES[i+1]
251
+ @polyrex.create.month no: (i+1).to_s, name: month_name, year: @year do |create|
104
252
  month.each_with_index do |week,j|
105
253
 
106
254
  week_s = (week_i == 0 ? old_year_week : week_i).to_s
107
- create.week rel_no: (j+1).to_s, no: week_s do |create|
108
- week.each_with_index do |x,k|
109
-
110
- if x then
255
+
256
+ if week[0].nil? then
257
+ label = Date::MONTHNAMES[(i > 0 ? i : 12)] + " - " + month_name
258
+ end
259
+
260
+ if week[-1].nil? then
261
+ label = month_name + " - " + Date::MONTHNAMES[(i+2 <= 12 ? i+2 : 1)]
262
+ end
263
+
264
+ week_record = {
265
+ rel_no: (j+1).to_s,
266
+ no: week_s,
267
+ mon: month_name,
268
+ label: label
269
+ }
270
+
271
+ create.week week_record do |create|
272
+ week.each_with_index do |day, k|
273
+
274
+ # if it's a day in the month then ...
275
+ if day then
276
+ x = day
111
277
  week_i += 1 if x.wday == 6
112
- h = {wday: x.wday.to_s, xday: x.day.to_s, name: Date::DAYNAMES[k], \
113
- date: x.strftime("%Y-%b-%d")}
278
+ h = {wday: x.wday.to_s, xday: x.day.to_s, \
279
+ name: Date::DAYNAMES[k], date: x.strftime("%Y-%b-%d"), \
280
+ ordinal: x.day.to_i.ordinal}
114
281
  else
115
282
  #if blank find the nearest date in the week and calculate this date
116
283
  # check right and if nothing then it's at the end of the month
117
284
  x = week[-1] ? (week[-1] - (7-(k+1))) : week[0] + k
118
- h = {wday: x.wday.to_s, xday: x.day.to_s, date: x.strftime("%Y-%b-%d")}
285
+ h = {wday: x.wday.to_s, xday: x.day.to_s, \
286
+ name: Date::DAYNAMES[k], date: x.strftime("%Y-%b-%d"), \
287
+ ordinal: x.day.to_i.ordinal, overlap: 'true'}
119
288
  end
289
+
120
290
  create.day(h)
121
291
  end
122
292
  end
@@ -125,7 +295,32 @@ class PolyrexCalendar
125
295
  end
126
296
 
127
297
  end
298
+
299
+ def slotted_sort(a)
300
+
301
+ upper = 36000 # upper slot value
302
+ slot_width = 9000 # 2.5 hours
303
+ max_slots = 3
304
+
305
+ b = a.reverse.inject([]) do |r,x|
128
306
 
307
+ upper ||= 10; i ||= 0
308
+ diff = upper - x
309
+
310
+ if diff >= slot_width and (i + a.length) < max_slots then
311
+ r << nil
312
+ i += 1
313
+ upper -= slot_width
314
+ redo
315
+ else
316
+ upper -= slot_width if x <= upper
317
+ r << x
318
+ end
319
+ end
320
+
321
+ a = b.+([nil] * max_slots).take(max_slots).reverse
322
+ end
323
+
129
324
  def generate_webpage(xml, xsl)
130
325
 
131
326
  # transform the xml to html
@@ -134,5 +329,6 @@ class PolyrexCalendar
134
329
  html = xslt.transform(doc).to_xml
135
330
  html
136
331
 
137
- end
138
- end
332
+ end
333
+
334
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polyrex-calendar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.12
4
+ version: 0.1.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -29,34 +29,48 @@ cert_chain:
29
29
  CIpMEIXEnQwmcmWL07xvpUquKTab0tCXtmcfjr74KP2KCm5guZyxeXaj9lD1OrnC
30
30
  Kgt/mRI2beG8K7QY81GGMsQjiG95Dcko
31
31
  -----END CERTIFICATE-----
32
- date: 2013-07-27 00:00:00.000000000 Z
32
+ date: 2013-12-16 00:00:00.000000000 Z
33
33
  dependencies:
34
34
  - !ruby/object:Gem::Dependency
35
35
  name: polyrex
36
36
  requirement: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  type: :runtime
42
42
  prerelease: false
43
43
  version_requirements: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: nokogiri
50
50
  requirement: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
57
  version_requirements: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: chronic_duration
64
+ requirement: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ type: :runtime
70
+ prerelease: false
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
60
74
  - !ruby/object:Gem::Version
61
75
  version: '0'
62
76
  description:
@@ -82,12 +96,12 @@ require_paths:
82
96
  - lib
83
97
  required_ruby_version: !ruby/object:Gem::Requirement
84
98
  requirements:
85
- - - '>='
99
+ - - ">="
86
100
  - !ruby/object:Gem::Version
87
101
  version: '0'
88
102
  required_rubygems_version: !ruby/object:Gem::Requirement
89
103
  requirements:
90
- - - '>='
104
+ - - ">="
91
105
  - !ruby/object:Gem::Version
92
106
  version: '0'
93
107
  requirements: []
metadata.gz.sig CHANGED
Binary file