Class: Shelley::CommandNode

Inherits:
Object
  • Object
show all
Defined in:
lib/shelley/registry.rb

Overview

A node of the command tree

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#childrenObject (readonly)

Returns the value of attribute children.



4
5
6
# File 'lib/shelley/registry.rb', line 4

def children
  @children
end

#commandObject

Returns the value of attribute command.



6
7
8
# File 'lib/shelley/registry.rb', line 6

def command
  @command
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/shelley/registry.rb', line 5

def name
  @name
end

#parentObject (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_pathObject



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_sObject



45
46
47
# File 'lib/shelley/registry.rb', line 45

def to_s
  "CommandNode(name=#{name}, command=#{command}, children={#{children.map(&:to_s)}})"
end