Class: ViewCountHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/helpers/counter-helpers.rb

Direct Known Subclasses

ViewerCountHelper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wiki, views_to_count = [ 'view' ], page_name = 'Popular Pages', update_page_every = :hour, show_top = 20, cache_name = 'viewcount') ⇒ ViewCountHelper

Returns a new instance of ViewCountHelper.



44
45
46
47
48
49
50
51
# File 'lib/helpers/counter-helpers.rb', line 44

def initialize( wiki, views_to_count = [ 'view' ], page_name = 'Popular Pages', update_page_every = :hour, show_top = 20, cache_name = 'viewcount' )
	@wiki = wiki
	@counts = wiki.load_cache(cache_name) || CounterObject.new(show_top)
	@views_to_count, @page_name = views_to_count, page_name
	@wiki.watch_for(:page_viewed) { |event,page,view,author| count page, view, author }
	@wiki.watch_for(update_page_every) { render_count_page }
	@wiki.watch_for(:shutdown) { wiki.save_cache(cache_name,@counts)}
end

Instance Attribute Details

#countsObject (readonly)

Returns the value of attribute counts.



42
43
44
# File 'lib/helpers/counter-helpers.rb', line 42

def counts
  @counts
end

Instance Method Details

#count(page, view, author) ⇒ Object



53
54
55
56
# File 'lib/helpers/counter-helpers.rb', line 53

def count( page, view, author )
	return unless should_count?( page, view, author )
	@counts.count(count_key( page, view, author ))
end

#count_key(page, view, author) ⇒ Object



62
63
64
# File 'lib/helpers/counter-helpers.rb', line 62

def count_key( page, view, author )
	page.name
end

#render_count_pageObject



66
67
68
69
70
71
72
73
# File 'lib/helpers/counter-helpers.rb', line 66

def render_count_page
	content = "h1. #{@page_name}\n\n"
	content << "Count since #{@counts.start_time}. Updated on #{Time.now}\n\n"
	@counts.each do |page,count|
		content << "| #{page} | #{count} |\n"
	end
	@wiki.revise( @page_name, content, 'AutomaticCounter' )
end

#should_count?(page, view, author) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/helpers/counter-helpers.rb', line 58

def should_count?( page, view, author )
	@views_to_count.include?( view.downcase )
end