Class: Database::Tree
- Inherits:
-
Object
- Object
- Database::Tree
- Defined in:
- lib/database/tree.rb
Constant Summary collapse
- ENTRY_FORMAT =
"Z*H40"- TREE_MODE =
040000
Instance Attribute Summary collapse
-
#entries ⇒ Object
Returns the value of attribute entries.
-
#oid ⇒ Object
Returns the value of attribute oid.
Class Method Summary collapse
Instance Method Summary collapse
- #add_entry(parents, entry) ⇒ Object
- #each_entry(&block) ⇒ Object
-
#initialize(entries = {}) ⇒ Tree
constructor
A new instance of Tree.
- #mode ⇒ Object
- #to_s ⇒ Object
- #traverse(&block) ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(entries = {}) ⇒ Tree
35 36 37 |
# File 'lib/database/tree.rb', line 35 def initialize(entries = {}) @entries = entries end |
Instance Attribute Details
#entries ⇒ Object
Returns the value of attribute entries.
7 8 9 |
# File 'lib/database/tree.rb', line 7 def entries @entries end |
#oid ⇒ Object
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 |
#mode ⇒ Object
59 60 61 |
# File 'lib/database/tree.rb', line 59 def mode TREE_MODE end |
#to_s ⇒ Object
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 |
#type ⇒ Object
63 64 65 |
# File 'lib/database/tree.rb', line 63 def type "tree" end |