Class: GitObjectBrowser::Models::Directory

Inherits:
Object
  • Object
show all
Defined in:
lib/git-object-browser/models/directory.rb

Instance Method Summary collapse

Constructor Details

#initialize(root, path) ⇒ Directory

Returns a new instance of Directory.



7
8
9
10
11
# File 'lib/git-object-browser/models/directory.rb', line 7

def initialize(root, path)
  @root = root
  @path = path
  @entries = read_entries
end

Instance Method Details

#read_entriesObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/git-object-browser/models/directory.rb', line 13

def read_entries
  entries = []
  Dir.chdir(File.join(@root, @path)) do
    files = Dir.glob("*")
    files.each do |file|
      relpath = File.join(@path, file).gsub(%r{\A/}, '')
      entry = {}
      if File.directory?(file)
        entry[:type] = 'directory'
      elsif File.symlink?(file)
        entry[:type] = 'symlink'
      elsif Ref::path?(relpath)
        entry[:type] = 'ref'
      elsif Reflog::path?(relpath)
        entry[:type] = 'reflog'
      elsif InfoRefs::path?(relpath)
        entry[:type] = 'info_refs'
      elsif PackedRefs::path?(relpath)
        entry[:type] = 'packed_refs'
      elsif Index::path?(relpath)
        entry[:type] = 'index'
      elsif GitObject::path?(relpath)
        entry[:type] = 'object'
      else
        entry[:type] = "file"
      end
      entry[:basename] = file
      entry[:mtime] = File.mtime(file).to_i
      entry[:size] = File.size(file)
      entries << entry
    end
  end
  order = %w{directory ref reflog info_refs packed_refs index object file symlink}
  entries.sort do |a,b|
    (order.index(a[:type]) <=> order.index(b[:type])).nonzero? ||
      a[:basename] <=> b[:basename]
  end
end

#to_hashObject



52
53
54
55
56
57
58
# File 'lib/git-object-browser/models/directory.rb', line 52

def to_hash
  return {
    :type    => "directory",
    :path    => @path,
    :entries => @entries
  }
end