Class: PolyrexCalendarBase

Inherits:
Object
  • Object
show all
Includes:
LIBRARY
Defined in:
lib/polyrex_calendarbase.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LIBRARY

#fetch_file, #fetch_filepath, #generate_webpage, #read

Constructor Details

#initialize(calendar_file = nil, year: Date.today.year.to_s) ⇒ PolyrexCalendarBase

Returns a new instance of PolyrexCalendarBase.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/polyrex_calendarbase.rb', line 181

def initialize(calendar_file=nil, year: Date.today.year.to_s)

  @calendar_file = calendar_file

  @year = year.to_s

  h = {
    calendar: 'calendar[year]',
       month: 'month[n, title]',
        week: 'week[n]',
         day: 'day[sdate, xday, event, bankholiday, title, sunrise, sunset]',
       entry: 'entry[time_start, time_end, duration, title]'
  }
  @schema = %i(calendar month day entry).map{|x| h[x]}.join '/'
  @visual_schema = h.values.join '/'


  if calendar_file then
    @calendar = Calendar.new calendar_file
    @id = @calendar.id_counter

  else
    @id = '1'
    # generate the calendar

    a = (Date.parse(@year + '-01-01')...Date.parse(@year.succ + '-01-01')).to_a

    @calendar = Calendar.new(@schema, id_counter: @id)
    @calendar.summary.year = @year

    a.group_by(&:month).each do |month, days| 

      @calendar.create.month no: month.to_s, title: Date::MONTHNAMES[month]  do |create|
        days.each do |x|
          create.day sdate: x.strftime("%Y-%b-%d"), xday: x.day.to_s, title: Date::DAYNAMES[x.wday]
        end
      end
    end

  end

  visual_schema = h.values.join '/'
  CalendarObjects.new(visual_schema)
     
end

Instance Attribute Details

#calendarObject

Returns the value of attribute calendar.



178
179
180
# File 'lib/polyrex_calendarbase.rb', line 178

def calendar
  @calendar
end

#cssObject

Returns the value of attribute css.



178
179
180
# File 'lib/polyrex_calendarbase.rb', line 178

def css
  @css
end

#dayObject (readonly)

Returns the value of attribute day.



179
180
181
# File 'lib/polyrex_calendarbase.rb', line 179

def day
  @day
end

#month_cssObject

Returns the value of attribute month_css.



178
179
180
# File 'lib/polyrex_calendarbase.rb', line 178

def month_css
  @month_css
end

#month_xslObject

Returns the value of attribute month_xsl.



178
179
180
# File 'lib/polyrex_calendarbase.rb', line 178

def month_xsl
  @month_xsl
end

#xslObject

Returns the value of attribute xsl.



178
179
180
# File 'lib/polyrex_calendarbase.rb', line 178

def xsl
  @xsl
end

Instance Method Details

#find(s) ⇒ Object



227
228
229
230
# File 'lib/polyrex_calendarbase.rb', line 227

def find(s)
  dt = Chronic.parse s, now: Time.local(@year,1,1)
  @calendar.month(dt.month).d(dt.day)
end

#import_bankholidays(dynarex) ⇒ Object



296
297
298
# File 'lib/polyrex_calendarbase.rb', line 296

def import_bankholidays(dynarex)
  import_dynarex(dynarex, :bankholiday=)
end

#import_events(objx) ⇒ Object Also known as: import!



236
237
238
239
240
# File 'lib/polyrex_calendarbase.rb', line 236

def import_events(objx)
  @id = @calendar.id_counter
  method('import_'.+(objx.class.to_s.downcase).to_sym).call(objx)
  self
end

#import_recurring_events(dynarex) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/polyrex_calendarbase.rb', line 300

def import_recurring_events(dynarex)

  title = dynarex.summary[:event_title]
  cc = ChronicCron.new dynarex.summary[:description].split(/,/,2).first
  time_start= "%s:%02d" % cc.to_time.to_a.values_at(2,1)

  dynarex.flat_records.each do |event|

    dt = DateTime.parse(event[:date])
    m, d = dt.month, dt.day
    record = {title: title, time_start: time_start}

    @calendar.month(m).d(d).create.entry record
  end
end

#import_sunrise_times(dynarex) ⇒ Object



316
317
318
# File 'lib/polyrex_calendarbase.rb', line 316

def import_sunrise_times(dynarex)
  import_suntimes dynarex, :sunrise=
end

#import_sunset_times(dynarex) ⇒ Object



320
321
322
# File 'lib/polyrex_calendarbase.rb', line 320

def import_sunset_times(dynarex)
  import_suntimes dynarex, :sunset=
end

#inspectObject



244
245
246
# File 'lib/polyrex_calendarbase.rb', line 244

def inspect()
   %Q(=> #<PolyrexCalendarBase:#{self.object_id} @id="#{@id}", @year="#{@year}">)
end

#month(n) ⇒ Object



248
249
250
# File 'lib/polyrex_calendarbase.rb', line 248

def month(n)
  @calendar.month(n)
end

#monthsObject



252
253
254
# File 'lib/polyrex_calendarbase.rb', line 252

def months
  @calendar.records
end

#parse_events(list) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/polyrex_calendarbase.rb', line 256

def parse_events(list)    
  
  polyrex = Polyrex.new('events/dayx[date, title]/entryx[start_time, end_time,' + \
    ' duration, title]')

  polyrex.format_masks[1] = '([!start_time] \([!duration]\) [!title]|' +  \
    '[!start_time]-[!end_time] [!title]|' + \
    '[!start_time] [!title])'

  polyrex.parse(list)

  self.import_events polyrex
  # for testing only
  #polyrex
end

#save(filename = @calendar_file) ⇒ Object



272
273
274
275
# File 'lib/polyrex_calendarbase.rb', line 272

def save(filename=@calendar_file)
  @calendar_file = 'calendar.xml' unless filename
  @calendar.save filename, pretty: true
end

#this_monthObject



292
293
294
# File 'lib/polyrex_calendarbase.rb', line 292

def this_month()
  @calendar.month(DateTime.now.month)
end

#this_weekObject



277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/polyrex_calendarbase.rb', line 277

def this_week()

  dt = DateTime.now
  days = @calendar.month(dt.month).day

  r = days.find {|day| day.date.cweek == dt.cweek }    
  pxweek = PolyrexObjects::Week.new
  pxweek.mon = Date::MONTHNAMES[dt.month]
  pxweek.no = dt.cweek.to_s
  pxweek.label = ''
  days[days.index(r),7].each{|day| pxweek.add day }

  pxweek
end

#to_xmlObject



232
233
234
# File 'lib/polyrex_calendarbase.rb', line 232

def to_xml()
  @calendar.to_xml pretty: true
end