Class: Shelley::CommandNode
- Inherits:
-
Object
- Object
- Shelley::CommandNode
- Defined in:
- lib/shelley/registry.rb
Overview
A node of the command tree
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#command ⇒ Object
Returns the value of attribute command.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
- #ensure_exists(*path) ⇒ Object
- #full_path ⇒ Object
-
#initialize(parent = nil) ⇒ CommandNode
constructor
A new instance of CommandNode.
- #node_by_path(*path) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(parent = nil) ⇒ CommandNode
Returns a new instance of CommandNode.
8 9 10 11 |
# File 'lib/shelley/registry.rb', line 8 def initialize(parent = nil) @children = [] @parent = parent end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
4 5 6 |
# File 'lib/shelley/registry.rb', line 4 def children @children end |
#command ⇒ Object
Returns the value of attribute command.
6 7 8 |
# File 'lib/shelley/registry.rb', line 6 def command @command end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/shelley/registry.rb', line 5 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
4 5 6 |
# File 'lib/shelley/registry.rb', line 4 def parent @parent end |
Instance Method Details
#ensure_exists(*path) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/shelley/registry.rb', line 13 def ensure_exists(*path) return self if path.count.zero? curr_name = path.shift node = @children.find { |n| n.name == curr_name } if node.nil? node = CommandNode.new(self) node.name = curr_name @children << node end return node if path.count.zero? node.ensure_exists(*path) end |
#full_path ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/shelley/registry.rb', line 35 def full_path curr_node = self path = [] until curr_node.parent.nil? path << curr_node.name curr_node = curr_node.parent end path.reverse end |
#node_by_path(*path) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/shelley/registry.rb', line 26 def node_by_path(*path) return self if path.count.zero? curr_name = path.shift node = @children.find { |n| n.name == curr_name } return nil if node.nil? return node if path.count.zero? node.node_by_path(*path) end |
#to_s ⇒ Object
45 46 47 |
# File 'lib/shelley/registry.rb', line 45 def to_s "CommandNode(name=#{name}, command=#{command}, children={#{children.map(&:to_s)}})" end |