Class: ViewCountHelper
- Inherits:
-
Object
- Object
- ViewCountHelper
- Defined in:
- lib/helpers/counter-helpers.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#counts ⇒ Object
readonly
Returns the value of attribute counts.
Instance Method Summary collapse
- #count(page, view, author) ⇒ Object
- #count_key(page, view, author) ⇒ Object
-
#initialize(wiki, views_to_count = [ 'view' ], page_name = 'Popular Pages', update_page_every = :hour, show_top = 20, cache_name = 'viewcount') ⇒ ViewCountHelper
constructor
A new instance of ViewCountHelper.
- #render_count_page ⇒ Object
- #should_count?(page, view, author) ⇒ Boolean
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,| count page, view, } @wiki.watch_for(update_page_every) { render_count_page } @wiki.watch_for(:shutdown) { wiki.save_cache(cache_name,@counts)} end |
Instance Attribute Details
#counts ⇒ Object (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, ) return unless should_count?( page, view, ) @counts.count(count_key( page, view, )) end |
#count_key(page, view, author) ⇒ Object
62 63 64 |
# File 'lib/helpers/counter-helpers.rb', line 62 def count_key( page, view, ) page.name end |
#render_count_page ⇒ Object
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
58 59 60 |
# File 'lib/helpers/counter-helpers.rb', line 58 def should_count?( page, view, ) @views_to_count.include?( view.downcase ) end |