properly define the public api

This commit is contained in:
Tom Preston-Werner
2010-04-17 16:41:05 -04:00
parent 78a9396421
commit 9037b06e62
3 changed files with 47 additions and 26 deletions
+9 -7
View File
@@ -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
+19 -11
View File
@@ -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
+19 -8
View File
@@ -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