Class: Topiary::Node
- Inherits:
-
Object
- Object
- Topiary::Node
- Defined in:
- lib/topiary/node.rb
Overview
Represents one vertex or node in your graph.
Each node may hold some user-defined data, so you can track what they represent.
Nodes also contain a list of "needs" (incoming edges) and "feeds" (outgoing edges). You may pass a list of connected nodes when you instantiate one, but usually you'll need to make your objects first and then add their needs/feeds. (If you didn't have to do that you probably wouldn't need this library, right?)
There are mutator methods you can use like this:
n1 = Node.new "n1"
n2 = Node.new "n2"
n1.need! n2
That will create an edge pointing from n2
to n1
.
Once your graph is ready, you can pass the list of nodes to Topiary.sort
.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#feeds ⇒ Object
readonly
Returns the value of attribute feeds.
-
#needs ⇒ Object
readonly
Returns the value of attribute needs.
Instance Method Summary collapse
- #begin! ⇒ Object
- #clone ⇒ Object
- #feed!(n) ⇒ Object
-
#initialize(data = nil, needs = [], feeds = []) ⇒ Node
constructor
A new instance of Node.
- #inspect ⇒ Object
- #name ⇒ Object
- #need!(n) ⇒ Object
-
#restore! ⇒ Object
Since the sorting algorithm mutates the edges, you can call this to restore everything to its original state.
- #to_s ⇒ Object
Constructor Details
#initialize(data = nil, needs = [], feeds = []) ⇒ Node
Returns a new instance of Node.
25 26 27 28 29 30 31 |
# File 'lib/topiary/node.rb', line 25 def initialize(data=nil, needs=[], feeds=[]) @data = data @needs = Set.new @feeds = Set.new needs.each{|n| need!(n)} feeds.each{|n| feed!(n)} end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
23 24 25 |
# File 'lib/topiary/node.rb', line 23 def data @data end |
#feeds ⇒ Object (readonly)
Returns the value of attribute feeds.
23 24 25 |
# File 'lib/topiary/node.rb', line 23 def feeds @feeds end |
#needs ⇒ Object (readonly)
Returns the value of attribute needs.
23 24 25 |
# File 'lib/topiary/node.rb', line 23 def needs @needs end |
Instance Method Details
#begin! ⇒ Object
33 34 35 36 37 38 |
# File 'lib/topiary/node.rb', line 33 def begin! @original_needs = @needs @original_feeds = @feeds @needs = @needs.clone @feeds = @feeds.clone end |
#clone ⇒ Object
75 76 77 |
# File 'lib/topiary/node.rb', line 75 def clone Topiary::Node.new(data, needs, feeds) end |
#feed!(n) ⇒ Object
54 55 56 57 |
# File 'lib/topiary/node.rb', line 54 def feed!(n) feeds << n n.needs << self end |
#inspect ⇒ Object
71 72 73 |
# File 'lib/topiary/node.rb', line 71 def inspect to_s end |
#name ⇒ Object
59 60 61 |
# File 'lib/topiary/node.rb', line 59 def name data && data.to_s || object_id.to_s end |
#need!(n) ⇒ Object
49 50 51 52 |
# File 'lib/topiary/node.rb', line 49 def need!(n) needs << n n.feeds << self end |
#restore! ⇒ Object
Since the sorting algorithm mutates the edges, you can call this to restore everything to its original state. The graph still isn't re-entrant, but at least it comes back from sorting the same as it entered.
44 45 46 47 |
# File 'lib/topiary/node.rb', line 44 def restore! @needs = @original_needs @feeds = @original_feeds end |
#to_s ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/topiary/node.rb', line 63 def to_s [ name, "needs:[" + needs.map(&:name).join(",") + "]", "feeds:[" + feeds.map(&:name).join(",") + "]", ].join(" ") end |