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, #generate_webpage, #read

Constructor Details

#initialize(calendar_file = nil, options = {}) ⇒ PolyrexCalendarBase



170
171
172
173
174
175
176
177
178
179
180
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
# File 'lib/polyrex_calendarbase.rb', line 170

def initialize(calendar_file=nil, options={})

  @calendar_file = calendar_file
  opts = {year: Time.now.year.to_s}.merge(options)
  @year = opts[:year]

  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.



167
168
169
# File 'lib/polyrex_calendarbase.rb', line 167

def calendar
  @calendar
end

#cssObject

Returns the value of attribute css.



167
168
169
# File 'lib/polyrex_calendarbase.rb', line 167

def css
  @css
end

#dayObject (readonly)

Returns the value of attribute day.



168
169
170
# File 'lib/polyrex_calendarbase.rb', line 168

def day
  @day
end

#month_cssObject

Returns the value of attribute month_css.



167
168
169
# File 'lib/polyrex_calendarbase.rb', line 167

def month_css
  @month_css
end

#month_xslObject

Returns the value of attribute month_xsl.



167
168
169
# File 'lib/polyrex_calendarbase.rb', line 167

def month_xsl
  @month_xsl
end

#xslObject

Returns the value of attribute xsl.



167
168
169
# File 'lib/polyrex_calendarbase.rb', line 167

def xsl
  @xsl
end

Instance Method Details

#find(s) ⇒ Object



216
217
218
219
# File 'lib/polyrex_calendarbase.rb', line 216

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



285
286
287
# File 'lib/polyrex_calendarbase.rb', line 285

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

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



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

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



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/polyrex_calendarbase.rb', line 289

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



305
306
307
# File 'lib/polyrex_calendarbase.rb', line 305

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

#import_sunset_times(dynarex) ⇒ Object



309
310
311
# File 'lib/polyrex_calendarbase.rb', line 309

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

#inspectObject



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

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

#month(n) ⇒ Object



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

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

#monthsObject



241
242
243
# File 'lib/polyrex_calendarbase.rb', line 241

def months
  @calendar.records
end

#parse_events(list) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/polyrex_calendarbase.rb', line 245

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



261
262
263
264
# File 'lib/polyrex_calendarbase.rb', line 261

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

#this_monthObject



281
282
283
# File 'lib/polyrex_calendarbase.rb', line 281

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

#this_weekObject



266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/polyrex_calendarbase.rb', line 266

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



221
222
223
# File 'lib/polyrex_calendarbase.rb', line 221

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