diff --git a/HISTORY.md b/HISTORY.md index 7d20d2f1..25a3c15f 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -6,6 +6,7 @@ * Added --irb option for the gollum command. * Minor Enhancements * Support a `:gollum_path` Sinatra setting for `Precious::App` + * Add Wiki#size to efficiently count pages without loading them. * Bug Fixes * Increase minimum Sanitize version requirement to 1.1.0. 1.0.x versions of Sanitize require Hpricot instead of Nokogiri diff --git a/lib/gollum/wiki.rb b/lib/gollum/wiki.rb index 440e967d..25d984f0 100644 --- a/lib/gollum/wiki.rb +++ b/lib/gollum/wiki.rb @@ -210,6 +210,17 @@ module Gollum tree_list(treeish || 'master') end + # Fill an array with a list of pages. + # + # ref - A String ref that is either a commit SHA or references one. + # + # Returns a flat Array of Gollum::Page instances. + def size(ref = nil) + tree_map_for(ref || 'master').inject(0) do |num, entry| + num + (@page_class.valid_page_name?(entry.name) ? 1 : 0) + end + end + # Public: All of the versions that have touched the Page. # # options - The options Hash: diff --git a/test/test_wiki.rb b/test/test_wiki.rb index a7d065f1..3749e443 100644 --- a/test/test_wiki.rb +++ b/test/test_wiki.rb @@ -39,6 +39,10 @@ context "Wiki" do pages.map { |p| p.filename }.sort end + test "counts pages" do + assert_equal 4, @wiki.size + end + test "normalizes commit hash" do commit = {:message => 'abc'} assert_equal({:message => 'abc', :name => 'Anonymous', :email => 'anon@anon.com'},