Class: Urbit::Graph
- Inherits:
-
Object
- Object
- Urbit::Graph
- Defined in:
- lib/urbit/graph.rb
Instance Attribute Summary collapse
-
#host_ship_name ⇒ Object
readonly
Returns the value of attribute host_ship_name.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#ship ⇒ Object
readonly
Returns the value of attribute ship.
Instance Method Summary collapse
- #add_node(node:) ⇒ Object
- #host_ship ⇒ Object
-
#initialize(ship:, graph_name:, host_ship_name:) ⇒ Graph
constructor
A new instance of Graph.
-
#newer_sibling_nodes(node:, count:) ⇒ Object
Answers the count newer sibling nodes relative to the passed #node.
- #newest_nodes(count: 10) ⇒ Object
-
#node(index:) ⇒ Object
Finds a single node in this graph by its index.
-
#nodes ⇒ Object
Answers an array with all of this Graph's currently attached Nodes, recursively inluding all of the Node's children.
-
#older_sibling_nodes(node:, count:) ⇒ Object
Answers the count older sibling nodes relative to the passed #node.
- #oldest_nodes(count: 10) ⇒ Object
- #resource ⇒ Object
-
#to_s ⇒ Object
the canonical printed representation of a Graph.
Constructor Details
#initialize(ship:, graph_name:, host_ship_name:) ⇒ Graph
Returns a new instance of Graph.
9 10 11 12 13 14 |
# File 'lib/urbit/graph.rb', line 9 def initialize(ship:, graph_name:, host_ship_name:) @ship = ship @name = graph_name @host_ship_name = host_ship_name @nodes = SortedSet.new end |
Instance Attribute Details
#host_ship_name ⇒ Object (readonly)
Returns the value of attribute host_ship_name.
7 8 9 |
# File 'lib/urbit/graph.rb', line 7 def host_ship_name @host_ship_name end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/urbit/graph.rb', line 7 def name @name end |
#ship ⇒ Object (readonly)
Returns the value of attribute ship.
7 8 9 |
# File 'lib/urbit/graph.rb', line 7 def ship @ship end |
Instance Method Details
#add_node(node:) ⇒ Object
16 17 18 |
# File 'lib/urbit/graph.rb', line 16 def add_node(node:) @nodes << node unless node.deleted? end |
#host_ship ⇒ Object
20 21 22 |
# File 'lib/urbit/graph.rb', line 20 def host_ship "~#{@host_ship_name}" end |
#newer_sibling_nodes(node:, count:) ⇒ Object
Answers the count newer sibling nodes relative to the passed #node.
76 77 78 |
# File 'lib/urbit/graph.rb', line 76 def newer_sibling_nodes(node:, count:) self.fetch_sibling_nodes(node, :newer, count)[0..(count - 1)] end |
#newest_nodes(count: 10) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/urbit/graph.rb', line 56 def newest_nodes(count: 10) count = 1 if count < 1 return self.fetch_newest_nodes(count) if @nodes.empty? || @nodes.count < count last_node = self.nodes.count - 1 self.nodes[(last_node - count)..last_node] end |
#node(index:) ⇒ Object
Finds a single node in this graph by its index. The index here should be the atom representation (as returned by Node#index).
36 37 38 |
# File 'lib/urbit/graph.rb', line 36 def node(index:) self.fetch_node(index).first end |
#nodes ⇒ Object
Answers an array with all of this Graph's currently attached Nodes, recursively inluding all of the Node's children.
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/urbit/graph.rb', line 44 def nodes self.fetch_all_nodes if @nodes.empty? @all_n = [] @nodes.each do |n| @all_n << n n.children.each do |c| @all_n << c end end @all_n end |
#older_sibling_nodes(node:, count:) ⇒ Object
Answers the count older sibling nodes relative to the passed #node.
83 84 85 |
# File 'lib/urbit/graph.rb', line 83 def older_sibling_nodes(node:, count:) self.fetch_sibling_nodes(node, :older, count)[0..(count - 1)] end |
#oldest_nodes(count: 10) ⇒ Object
63 64 65 66 67 |
# File 'lib/urbit/graph.rb', line 63 def oldest_nodes(count: 10) count = 1 if count < 1 return self.fetch_oldest_nodes(count) if @nodes.empty? || @nodes.count < count self.nodes[0..(count - 1)] end |
#resource ⇒ Object
69 70 71 |
# File 'lib/urbit/graph.rb', line 69 def resource "#{self.host_ship}/#{self.name}" end |
#to_s ⇒ Object
the canonical printed representation of a Graph
89 90 91 |
# File 'lib/urbit/graph.rb', line 89 def to_s "a Graph(#{self.resource})" end |