class Patterns::Leaf
Acts as a leaf branch to a Composite structure; as a leaf, it may not have children
Public Class Methods
new(name)
click to toggle source
Initializes the Leaf class
Calls superclass method
# File app/models/composite/leaf.rb, line 10 def initialize(name) super(name) end
Public Instance Methods
add(component)
click to toggle source
Raises an error, since a leaf may not have a child node
# File app/models/composite/leaf.rb, line 15 def add(component) raise AddChildToLeafError, 'Cannot add children to a leaf' end
display(depth)
click to toggle source
Displays this leaf node with dashes indicating its current depth
# File app/models/composite/leaf.rb, line 25 def display(depth) str = '' depth.times do str << '-' end puts str + @name end
remove(component)
click to toggle source
Raises an error, since a leaf can not remove what it doesn't have
# File app/models/composite/leaf.rb, line 20 def remove(component) raise RemoveChildFromLeafError, 'Cannot remove children from a leaf.' end