Class: Database::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/database/tree.rb

Constant Summary collapse

ENTRY_FORMAT =
"Z*H40"
TREE_MODE =
040000

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entries = {}) ⇒ Tree



35
36
37
# File 'lib/database/tree.rb', line 35

def initialize(entries = {})
  @entries = entries
end

Instance Attribute Details

#entriesObject

Returns the value of attribute entries.



7
8
9
# File 'lib/database/tree.rb', line 7

def entries
  @entries
end

#oidObject

Returns the value of attribute oid.



7
8
9
# File 'lib/database/tree.rb', line 7

def oid
  @oid
end

Class Method Details

.build(entries) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/database/tree.rb', line 25

def self.build(entries)
  root = Tree.new

  entries.each do |entry|
    root.add_entry(entry.parent_directories, entry)
  end

  root
end

.parse(scanner) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/database/tree.rb', line 9

def self.parse(scanner)
  entries = {}

  until scanner.eos?
    mode = scanner.scan_until(/ /).strip.to_i(8)
    name = scanner.scan_until(/\0/)[0..-2]

    oid = scanner.peek(20).unpack("H40").first
    scanner.pos += 20

    entries[name] = Entry.new(oid, mode)
  end

  Tree.new(entries)
end

Instance Method Details

#add_entry(parents, entry) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/database/tree.rb', line 43

def add_entry(parents, entry)
  if parents.empty?
    @entries[entry.basename] = entry
  else
    tree = @entries[parents.first.basename] ||= Tree.new
    tree.add_entry(parents.drop(1), entry)
  end
end

#each_entry(&block) ⇒ Object



39
40
41
# File 'lib/database/tree.rb', line 39

def each_entry(&block)
  @entries.each(&block)
end

#modeObject



59
60
61
# File 'lib/database/tree.rb', line 59

def mode
  TREE_MODE
end

#to_sObject



67
68
69
70
71
72
73
74
# File 'lib/database/tree.rb', line 67

def to_s
  entries = @entries.map do |name, entry|
    mode = entry.mode.to_s(8)
    ["#{ mode } #{ name }", entry.oid].pack(ENTRY_FORMAT)
  end

  entries.join("")
end

#traverse(&block) ⇒ Object



52
53
54
55
56
57
# File 'lib/database/tree.rb', line 52

def traverse(&block)
  @entries.each do |name, entry|
    entry.traverse(&block) if entry.is_a?(Tree)
  end
  block.call(self)
end

#typeObject



63
64
65
# File 'lib/database/tree.rb', line 63

def type
  "tree"
end