Class: Cascading::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/cascading/base.rb

Direct Known Subclasses

Assembly, Cascade, Flow

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent) ⇒ Node

Returns a new instance of Node.



5
6
7
8
9
10
11
# File 'lib/cascading/base.rb', line 5

def initialize(name, parent)
  @name = name
  @parent = parent
  @children = {}
  @child_names = []
  @last_child = nil
end

Instance Attribute Details

#child_namesObject

Returns the value of attribute child_names.



3
4
5
# File 'lib/cascading/base.rb', line 3

def child_names
  @child_names
end

#childrenObject

Returns the value of attribute children.



3
4
5
# File 'lib/cascading/base.rb', line 3

def children
  @children
end

#last_childObject

Returns the value of attribute last_child.



3
4
5
# File 'lib/cascading/base.rb', line 3

def last_child
  @last_child
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/cascading/base.rb', line 3

def name
  @name
end

#parentObject

Returns the value of attribute parent.



3
4
5
# File 'lib/cascading/base.rb', line 3

def parent
  @parent
end

Instance Method Details

#add_child(node) ⇒ Object

Children must be uniquely named within the scope of each Node. This ensures, for example, two assemblies are not created within the same flow with the same name, causing joins, unions, and sinks on them to be ambiguous.



17
18
19
20
21
22
23
24
# File 'lib/cascading/base.rb', line 17

def add_child(node)
  raise AmbiguousNodeNameException.new("Attempted to add '#{node.qualified_name}', but node named '#{node.name}' already exists") if @children[node.name]

  @children[node.name] = node
  @child_names << node.name
  @last_child = node
  node
end

#describe(offset = '') ⇒ Object Also known as: desc



30
31
32
# File 'lib/cascading/base.rb', line 30

def describe(offset = '')
  "#{offset}#{name}:node\n#{child_names.map{ |child| children[child].describe("#{offset}  ") }.join("\n")}"
end

#find_child(name) ⇒ Object

In order to find a child, we require it to be uniquely named within this Node and its children. This ensures, for example, branches in peer assemblies or branches and assemblies do not conflict in joins, unions, and sinks.



39
40
41
42
43
44
45
# File 'lib/cascading/base.rb', line 39

def find_child(name)
  all_children_with_name = find_all_children_with_name(name)
  qualified_names = all_children_with_name.map{ |child| child.qualified_name }
  raise AmbiguousNodeNameException.new("Ambiguous lookup of child by name '#{name}'; found '#{qualified_names.join("', '")}'") if all_children_with_name.size > 1

  all_children_with_name.first
end

#qualified_nameObject



26
27
28
# File 'lib/cascading/base.rb', line 26

def qualified_name
  parent ? "#{parent.qualified_name}.#{name}" : name
end

#rootObject



47
48
49
50
# File 'lib/cascading/base.rb', line 47

def root
  return self unless parent
  parent.root
end