Class: AuthorCountHelper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wiki, page_name = 'Principal Authors', update_page_every = :hour, show_top = 20, cache_name = 'authorcount') ⇒ AuthorCountHelper

Returns a new instance of AuthorCountHelper.



92
93
94
95
96
97
98
99
100
# File 'lib/helpers/counter-helpers.rb', line 92

def initialize( wiki, page_name = 'Principal Authors', update_page_every = :hour, show_top = 20, cache_name = 'authorcount' )
	@wiki = wiki
	@counts = wiki.load_cache(cache_name) || CounterObject.new(show_top) 
	count_from_scratch if @counts.empty?
	@page_name =  page_name
	@wiki.watch_for(:page_revised) { |event,page,revision| count page, revision }
	@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.



90
91
92
# File 'lib/helpers/counter-helpers.rb', line 90

def counts
  @counts
end

Instance Method Details

#count(page, revision) ⇒ Object



110
111
112
113
# File 'lib/helpers/counter-helpers.rb', line 110

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

#count_from_scratchObject



102
103
104
105
106
107
108
# File 'lib/helpers/counter-helpers.rb', line 102

def count_from_scratch
	@wiki.each do |pagename,page|
		page.revisions.each do |revision|
			count( page, revision )
		end
	end
end

#count_key(page, revision) ⇒ Object



119
120
121
# File 'lib/helpers/counter-helpers.rb', line 119

def count_key( page, revision )
	revision.author
end

#render_count_pageObject



123
124
125
126
127
128
129
130
# File 'lib/helpers/counter-helpers.rb', line 123

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, revision) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/helpers/counter-helpers.rb', line 115

def should_count?( page, revision )
	revision.author !~ /^Automatic/
end