-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The Page class is getting complicated, so extract out the tree structure into its own class, and introduce a simple struct to represent the pages themselves.
- Loading branch information
Showing
3 changed files
with
109 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
module Obsidian | ||
class Tree | ||
def initialize(value) | ||
@value = value | ||
@children = [] | ||
end | ||
|
||
attr_reader :children | ||
attr_reader :value | ||
|
||
def inspect | ||
"Tree(#{value.inspect}) [#{children.length} children]" | ||
end | ||
|
||
def add_child(value) | ||
node = Tree.new(value) | ||
children << node | ||
node | ||
end | ||
|
||
def remove_child(value) | ||
children.delete_if { |node| node.value == value } | ||
end | ||
|
||
def is_index? | ||
children.length > 0 | ||
end | ||
|
||
def find(&block) | ||
walk_tree do |page| | ||
return page if block.call(page) | ||
end | ||
|
||
nil | ||
end | ||
|
||
def walk_tree(&block) | ||
block.call(self) | ||
|
||
children.each do |page| | ||
page.walk_tree(&block) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Obsidian::Tree do | ||
subject(:root) { described_class.new(:root) } | ||
|
||
describe("creating, retrieving, and removing nodes") do | ||
before do | ||
foo = root.add_child(:foo) | ||
foo.add_child(:bar) | ||
foo.add_child(:baz) | ||
end | ||
|
||
it "can find a node from the root" do | ||
a = root.find { |node| node.value == :baz } | ||
|
||
expect(a&.value).to eq(:baz) | ||
end | ||
|
||
it "can list the children of a node" do | ||
foo = root.children.first | ||
|
||
expect(root.children.map(&:value)).to eq([:foo]) | ||
expect(foo.children.map(&:value)).to eq([:bar, :baz]) | ||
end | ||
|
||
it "can remove nodes" do | ||
root.remove_child(:foo) | ||
expect(root.children).to be_empty | ||
end | ||
|
||
it "can tell if a node is non-terminal" do | ||
foo = root.children.first | ||
bar = foo.children.first | ||
expect(root.is_index?).to eq(true) | ||
expect(foo.is_index?).to eq(true) | ||
expect(bar.is_index?).to eq(false) | ||
end | ||
end | ||
end |