Skip to content

Commit

Permalink
Added Hide::Updater to return updated pages and blog posts based on G…
Browse files Browse the repository at this point in the history
…it log
  • Loading branch information
karmi committed Mar 1, 2011
1 parent c84cb42 commit 6fe623f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/hide.rb
Expand Up @@ -4,6 +4,7 @@
require 'hide/site'
require 'hide/page'
require 'hide/indexer'
require 'hide/updater'
require 'hide/git'

module Hide
Expand Down
2 changes: 0 additions & 2 deletions lib/hide/page.rb
@@ -1,8 +1,6 @@
module Hide
class Page

attr_reader :meta

def initialize(page)
@page = page
end
Expand Down
35 changes: 35 additions & 0 deletions lib/hide/updater.rb
@@ -0,0 +1,35 @@
module Hide
class Updater

def initialize(site, sha1, sha2)
@site, @sha1, @sha2 = site, sha1, sha2
end

def git
Git.new @site.path, @sha1, @sha2
end

def updated_pages
updated_pages = []
@site.pages.each do |page|
updated_paths.each { |path| updated_pages << page if page.url.include?(path) }
end
updated_pages
end

def updated_posts
updated_posts = []
@site.posts.each do |post|
updated_paths.each { |path| updated_posts << post if post.url.include?(path) }
end
updated_posts
end

def updated_paths
git.changed_files.map do |file|
[ File.dirname(file), File.basename(file, '.*') ].join('/')
end
end

end
end
35 changes: 35 additions & 0 deletions test/unit/updater_test.rb
@@ -0,0 +1,35 @@
require 'test_helper'

module Hide

class UpdaterTest < Test::Unit::TestCase

context "Updater" do

setup do
@site = Hide::Site.new path_to_test_site, :pages_directories => ['guide']
@updater = Updater.new @site, 'abc123', 'def456'
end

should "return updated paths based on sha1 and sha2" do
Git.any_instance.expects(:changed_files).returns( %w|blog/_posts/2011-02-08-percolator.textile guide/appendix/clients.textile| )
assert_equal %w|blog/_posts/2011-02-08-percolator guide/appendix/clients|, @updater.updated_paths
end

should "return selected pages based on updated paths" do
@updater.stubs(:updated_paths).returns( ['guide/reference/api/search/request-body'] )
assert_equal 1, @updater.updated_pages.size
assert_equal 'Search API - Request Body', @updater.updated_pages.first.title
end

should "return selected blog posts based on updated paths" do
@updater.stubs(:updated_paths).returns( ['blog/2010/02/08/youknowforsearch'] )
assert_equal 1, @updater.updated_posts.size
assert_equal 'You Know, for Search', @updater.updated_posts.first.title
end

end

end

end

0 comments on commit 6fe623f

Please sign in to comment.