Class: Gollum::BlobEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/gollum-lib/blob_entry.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sha, path, size = nil, mode = nil) ⇒ BlobEntry

Returns a new instance of BlobEntry.



16
17
18
19
20
21
22
# File 'lib/gollum-lib/blob_entry.rb', line 16

def initialize(sha, path, size = nil, mode = nil)
  @sha  = sha
  @path = path
  @size = size
  @mode = mode
  @dir  = @name = @blob = nil
end

Instance Attribute Details

#modeObject (readonly)

Gets the Fixnum mode of this blob.



14
15
16
# File 'lib/gollum-lib/blob_entry.rb', line 14

def mode
  @mode
end

#pathObject (readonly)

Gets the full path String for this blob.



8
9
10
# File 'lib/gollum-lib/blob_entry.rb', line 8

def path
  @path
end

#shaObject (readonly)

Gets the String SHA for this blob.



5
6
7
# File 'lib/gollum-lib/blob_entry.rb', line 5

def sha
  @sha
end

#sizeObject (readonly)

Gets the Fixnum size of this blob.



11
12
13
# File 'lib/gollum-lib/blob_entry.rb', line 11

def size
  @size
end

Class Method Details

.normalize_dir(dir) ⇒ Object

Normalizes a given directory name for searching through tree paths. Ensures that a directory begins with a slash, or

normalize_dir("")      # => ""
normalize_dir(".")     # => ""
normalize_dir("foo")   # => "/foo"
normalize_dir("/foo/") # => "/foo"
normalize_dir("/")     # => ""
normalize_dir("c:/")   # => ""

dir - String directory name.

Returns a normalized String directory name, or nil if no directory is given.



86
87
88
89
90
91
92
93
94
# File 'lib/gollum-lib/blob_entry.rb', line 86

def self.normalize_dir(dir)
  return '' if dir =~ /^.:\/$/
  if dir
    dir = ::File.expand_path(dir, '/')
    dir = dir[2..-1] if dir =~ /^[a-zA-Z]:\// # expand_path may add d:/ on windows
    dir = '' if dir == '/'
  end
  dir
end

Instance Method Details

#blob(repo) ⇒ Object

Gets a Gollum::Git::Blob instance for this blob.

repo - Gollum::Git::Repo instance for the Gollum::Git::Blob.

Returns an unbaked Gollum::Git::Blob instance.



39
40
41
42
# File 'lib/gollum-lib/blob_entry.rb', line 39

def blob(repo)
  @blob ||= Gollum::Git::Blob.create(repo,
                              :id => @sha, :name => name, :size => @size, :mode => @mode)
end

#dirObject

Gets the normalized directory path String for this blob.



25
26
27
# File 'lib/gollum-lib/blob_entry.rb', line 25

def dir
  @dir ||= self.class.normalize_dir(::File.dirname(@path))
end

#file(wiki, commit) ⇒ Object

Gets a File instance for this blob.

wiki - Gollum::Wiki instance for the Gollum::File

Returns a Gollum::File instance.



61
62
63
64
65
66
# File 'lib/gollum-lib/blob_entry.rb', line 61

def file(wiki, commit)
  blob         = self.blob(wiki.repo)
  file         = wiki.file_class.new(wiki).populate(blob, self.dir)
  file.version = commit
  file
end

#inspectObject



68
69
70
# File 'lib/gollum-lib/blob_entry.rb', line 68

def inspect
  %(#<Gollum::BlobEntry #{@sha} #{@path}>)
end

#nameObject

Gets the file base name String for this blob.



30
31
32
# File 'lib/gollum-lib/blob_entry.rb', line 30

def name
  @name ||= ::File.basename(@path)
end

#page(wiki, commit) ⇒ Object

Gets a Page instance for this blob.

wiki - Gollum::Wiki instance for the Gollum::Page

Returns a Gollum::Page instance.



49
50
51
52
53
54
# File 'lib/gollum-lib/blob_entry.rb', line 49

def page(wiki, commit)
  blob         = self.blob(wiki.repo)
  page         = wiki.page_class.new(wiki).populate(blob, self.dir)
  page.version = commit
  page
end