From 9037b06e628a67da3d672c7dd0e45d4c20cc43ea Mon Sep 17 00:00:00 2001 From: Tom Preston-Werner Date: Sat, 17 Apr 2010 16:41:05 -0400 Subject: [PATCH] properly define the public api --- lib/gollum/file.rb | 16 +++++++++------- lib/gollum/page.rb | 30 +++++++++++++++++++----------- lib/gollum/wiki.rb | 27 +++++++++++++++++++-------- 3 files changed, 47 insertions(+), 26 deletions(-) diff --git a/lib/gollum/file.rb b/lib/gollum/file.rb index fa08115f..d98cd47c 100644 --- a/lib/gollum/file.rb +++ b/lib/gollum/file.rb @@ -1,23 +1,25 @@ module Gollum class File - attr_accessor :wiki, :blob, :version - # Initialize a file. # # wiki - The Gollum::Wiki in question. # # Returns a newly initialized Gollum::File. def initialize(wiki) - self.wiki = wiki + @wiki = wiki + @blob = nil end # The raw contents of the page. # # Returns the String data. def raw_data - self.blob.data rescue nil + @blob.data rescue nil end + # The Grit::Commit version of the file. + attr_reader :version + ######################################################################### # # Private @@ -31,10 +33,10 @@ module Gollum # # Returns a Gollum::File or nil if the file could not be found. def find(name, version) - commit = self.wiki.repo.commit(version) + commit = @wiki.repo.commit(version) if blob = commit.tree / name - self.blob = blob - self.version = commit + @blob = blob + @version = commit self else nil diff --git a/lib/gollum/page.rb b/lib/gollum/page.rb index 7d290a13..e9377132 100644 --- a/lib/gollum/page.rb +++ b/lib/gollum/page.rb @@ -2,36 +2,38 @@ module Gollum class Page VALID_PAGE_RE = /^(.+)\.(md|mkdn?|mdown|markdown|textile|rdoc|org|re?st(\.txt)?|asciidoc|pod|\d)$/i - attr_accessor :wiki, :blob, :path, :version - # Initialize a page. # # wiki - The Gollum::Wiki in question. # # Returns a newly initialized Gollum::Page. def initialize(wiki) - self.wiki = wiki + @wiki = wiki + @blob = nil end # The on-disk filename of the page. # # Returns the String name. def name - self.blob.name rescue nil + @blob.name rescue nil end + # The String full path of the page. + attr_reader :path + # The raw contents of the page. # # Returns the String data. def raw_data - self.blob.data rescue nil + @blob.data rescue nil end # The formatted contents of the page. # # Returns the String data. def formatted_data - GitHub::Markup.render(self.blob.name, self.blob.data) rescue nil + GitHub::Markup.render(@blob.name, @blob.data) rescue nil end # The format of the page. @@ -40,7 +42,7 @@ module Gollum # [ :markdown | :textile | :rdoc | :org | :rest | :asciidoc | :pod | # :roff ] def format - case blob.name + case @blob.name when /\.(md|mkdn?|mdown|markdown)$/i :markdown when /\.(textile)$/i @@ -62,11 +64,14 @@ module Gollum end end + # The Grit::Commit version of the page. + attr_reader :version + # All of the versions that have touched this Page. # # Returns an Array of Grit::Commit. def versions - @wiki.repo.log('master', self.path) + @wiki.repo.log('master', @path) end ######################################################################### @@ -75,6 +80,9 @@ module Gollum # ######################################################################### + # Private: The Grit::Commit version of the page. + attr_writer :version + # Private: Convert a format Symbol into an extension String. # # format - The format Symbol. @@ -99,7 +107,7 @@ module Gollum # # Returns a Gollum::Page or nil if the page could not be found. def find(name, version) - commit = self.wiki.repo.commit(version) + commit = @wiki.repo.commit(version) if page = find_page_in_tree(commit.tree, name) page.version = commit page @@ -143,8 +151,8 @@ module Gollum # # Returns the populated Gollum::Page. def populate(blob, path) - self.blob = blob - self.path = (path + '/' + blob.name)[1..-1] + @blob = blob + @path = (path + '/' + blob.name)[1..-1] self end diff --git a/lib/gollum/wiki.rb b/lib/gollum/wiki.rb index f2ece471..28d53778 100644 --- a/lib/gollum/wiki.rb +++ b/lib/gollum/wiki.rb @@ -1,15 +1,13 @@ module Gollum class Wiki - attr_accessor :path, :repo - # Initialize a new Gollum Repo. # # repo - The String path to the Git repository that holds the Gollum site. # # Returns a fresh Gollum::Repo. def initialize(path) - self.path = path - self.repo = Grit::Repo.new(path) + @path = path + @repo = Grit::Repo.new(path) end # Get the formatted page for a given page name. @@ -45,10 +43,10 @@ module Gollum def write_page(name, format, data, commit = {}) ext = Page.format_to_ext(format) path = Gollum.canonical_name(name) + '.' + ext - index = self.repo.index + index = @repo.index index.add(path, data) actor = Grit::Actor.new(commit[:name], commit[:email]) - parent = self.repo.commit('master') + parent = @repo.commit('master') index.commit(commit[:message], [parent], actor) end @@ -64,11 +62,24 @@ module Gollum # # Returns the String SHA1 of the newly written version. def update_page(page, data, commit = {}) - index = self.repo.index + index = @repo.index index.add(page.path, data) actor = Grit::Actor.new(commit[:name], commit[:email]) - parent = self.repo.commit('master') + parent = @repo.commit('master') index.commit(commit[:message], [parent], actor) end + + ######################################################################### + # + # Private + # + ######################################################################### + + # Private: The Grit::Repo associated with this wiki. + attr_reader :repo + + # Private: The String path to the Git repository that holds the Gollum + # site. + attr_reader :path end end \ No newline at end of file