class Branchtree::Tree
Represent the branch topology described by a user's YAML file.
Attributes
roots[R]
Public Class Methods
load(source)
click to toggle source
Load a tree from the topology described in a YAML file.
# File lib/branchtree/tree.rb, line 9 def self.load(source) doc = YAML.safe_load(File.read(source)) new(doc.map { |node| Branchtree::Branch.load(node, nil) }) end
new(roots)
click to toggle source
# File lib/branchtree/tree.rb, line 16 def initialize(roots) @roots = roots end
Public Instance Methods
breadth_first(&block)
click to toggle source
# File lib/branchtree/tree.rb, line 32 def breadth_first(&block) level = 0 frontier = roots.dup until frontier.empty? frontier.each do |branch| block.call(level, branch) end level += 1 frontier = frontier.flat_map(&:children) end end
depth_first(&block)
click to toggle source
# File lib/branchtree/tree.rb, line 28 def depth_first(&block) depth_first_from(level: 0, branches: roots, &block) end
find_branch(name)
click to toggle source
Locate a known branch in the tree by abbreviated ref name, or return nil if none are found.
# File lib/branchtree/tree.rb, line 21 def find_branch(name) breadth_first do |level, branch| return branch if branch.name == name end nil end
Private Instance Methods
depth_first_from(level:, branches:, &block)
click to toggle source
# File lib/branchtree/tree.rb, line 48 def depth_first_from(level:, branches:, &block) branches.each do |branch| block.call(level, branch) depth_first_from(level: level + 1, branches: branch.children, &block) end end