Class: RefererDiary

Inherits:
Object
  • Object
show all
Includes:
TDiary::RefererManager
Defined in:
lib/tdiary/plugin/05referer.rb

Overview

fake diary class for volatile referer

Instance Method Summary collapse

Methods included from TDiary::RefererManager

#clear_referers, #count_referers

Constructor Details

#initialize(keep) ⇒ RefererDiary

Returns a new instance of RefererDiary.



33
34
35
36
37
38
# File 'lib/tdiary/plugin/05referer.rb', line 33

def initialize( keep )
  init_referers
  @keep = keep
  @current_date = nil
  @refs = {}
end

Instance Method Details

#add_referer(ref, count = 1) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/tdiary/plugin/05referer.rb', line 56

def add_referer( ref, count = 1 )
  return nil unless ref
  current = @current_date || @refs.keys.sort[-1] || '00000101'
  ref_info = add_referer_orig( ref, count )
  uref = CGI::unescape( ref )
  @refs[current] ||= {}
  if pair = @refs[current][uref] then
    @refs[current][uref] = [pair[0] + count, ref_info[1]]
  else
    @refs[current][uref] = [count, ref_info[1]]
  end
end

#add_referer_origObject



55
# File 'lib/tdiary/plugin/05referer.rb', line 55

alias :add_referer_orig :add_referer

#clear_oldest_referer(newest) ⇒ Object



69
70
71
72
73
74
# File 'lib/tdiary/plugin/05referer.rb', line 69

def clear_oldest_referer( newest )
  return if (@refs.keys.sort[-1] || '') > newest
  @refs[newest] = {} unless @refs[newest]
  return if @refs.keys.size <= @keep
  @refs.delete( @refs.keys.sort[0] )
end

#current_date=(d) ⇒ Object

date as String ‘YYYYMMDD’



50
51
52
53
# File 'lib/tdiary/plugin/05referer.rb', line 50

def current_date=( d )
  @current_date = d
  @refs[d] ||= {} if d
end

#dateObject

return Time object



41
42
43
44
45
46
47
# File 'lib/tdiary/plugin/05referer.rb', line 41

def date
  if @current_date then
    Time::local( *(@current_date.scan( /^(\d{4})(\d\d)(\d\d)$/ )[0]) )
  else
    @date
  end
end

#each_dateObject



90
91
92
93
94
95
# File 'lib/tdiary/plugin/05referer.rb', line 90

def each_date
  @refs.keys.sort.each do |d|
    @current_date = d
    yield( self )
  end
end

#each_referer(limit = 10) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/tdiary/plugin/05referer.rb', line 77

def each_referer( limit = 10 )
  if @current_date then
    @refs[@current_date].values.sort.reverse.each_with_index do |ary,idx|
      break if idx >= limit
      yield( ary[0], ary[1] )
    end
  else
    each_referer_orig( limit ) do |count, ref|
      yield( count, ref )
    end
  end
end

#each_referer_origObject



76
# File 'lib/tdiary/plugin/05referer.rb', line 76

alias :each_referer_orig :each_referer