Class: TDiary::TDiaryLatest

Inherits:
TDiaryView show all
Defined in:
lib/tdiary/view.rb

Overview

class TDiaryLatest

show latest mode view

Constant Summary

Constants inherited from TDiaryBase

TDiary::TDiaryBase::DIRTY_COMMENT, TDiary::TDiaryBase::DIRTY_DIARY, TDiary::TDiaryBase::DIRTY_NONE, TDiary::TDiaryBase::DIRTY_REFERER

Instance Attribute Summary

Attributes inherited from TDiaryBase

#cgi, #conf, #cookies, #date, #diaries, #ignore_parser_cache, #rhtml

Instance Method Summary collapse

Methods inherited from TDiaryView

#last_modified

Methods inherited from TDiaryBase

#[], #calendar, #eval_rhtml, #last_modified

Methods included from ViewHelper

#base_url, #bot?

Constructor Details

#initialize(cgi, rhtml, conf) ⇒ TDiaryLatest

Returns a new instance of TDiaryLatest.



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/tdiary/view.rb', line 322

def initialize( cgi, rhtml, conf )
  super

  if @cgi.params['date'][0] then
    ym = [@cgi.params['date'][0][0,4].to_i, @cgi.params['date'][0][4,2].to_i]
    @date = nil
  else
    ym = latest_month
  end

  unless @date then
    @date = ym ? Time::local( ym[0], ym[1] ) : Time::now
    @io.transaction( @date ) do |diaries|
      @diaries = diaries
      if @cgi.params['date'][0] then
        @diary = @diaries[@cgi.params['date'][0][0,8]]
        @date = @diary.date if @diary
      end
      unless @diary then
        @diaries.keys.sort.reverse_each do |d|
          diary = @diaries[d]
          if diary.visible?
            @diary = diary
            break
          end
        end
        @diary = @diaries[@diaries.keys.sort.reverse[0]] unless @diary
        @date = @diary.date if @diary
      end
      DIRTY_NONE
    end
  end

  if ym then
    # read +2 days for calc ndays.prev in count_diaries method
    limit = limit_size( @conf.latest_limit ) + 2

    # read next month data until limit
    y = ym[0].to_i
    m = ym[1].to_i
    latest = latest_month
    diaries_tmp = {}.update( @diaries )
    diaries_size = count_diaries_after( diaries_tmp )
    while ( latest and diaries_size < limit )
      date = if m == 12 then
        Time::local( y += 1, m = 1 )
      else
        Time::local( y, m += 1 )
      end
      break if date > Time::local( *latest )
      @io.transaction( date ) do |diaries|
        diaries_tmp.update( diaries )
        diaries_size = count_diaries_after( diaries_tmp )
        DIRTY_NONE
      end
    end

    # read prev month data until limit
    y = ym[0].to_i
    m = ym[1].to_i
    oldest = oldest_month
    diaries_size = count_diaries_before( @diaries )
    while ( oldest and diaries_size < limit )
      date = if m == 1 then
        Time::local( y -= 1, m = 12 )
      else
        Time::local( y, m -= 1 )
      end
      break if date < Time::local( *oldest )
      @io.transaction( date ) do |diaries|
        @diaries.update( diaries )
        diaries_size = count_diaries_before( @diaries )
        DIRTY_NONE
      end
    end
  end
end

Instance Method Details

#latest(limit = 5) ⇒ Object



400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/tdiary/view.rb', line 400

def latest( limit = 5 )
  start = start_date
  limit = limit_size( limit )
  idx = 0
  @diaries.keys.sort.reverse_each do |date|
    next if date > start
    diary = @diaries[date]
    next unless diary.visible?
    yield diary
    idx += 1
    break if idx >= limit
  end
end