Class: Urbit::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/urbit/graph.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ship:, graph_name:, host_ship_name:) ⇒ Graph

Returns a new instance of Graph.



9
10
11
12
13
14
15
# File 'lib/urbit/graph.rb', line 9

def initialize(ship:, graph_name:, host_ship_name:)
  @ship           = ship
  @group          = nil
  @host_ship_name = host_ship_name
  @name           = graph_name
  @nodes          = SortedSet.new
end

Instance Attribute Details

#host_ship_nameObject (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

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/urbit/graph.rb', line 7

def name
  @name
end

#shipObject (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



17
18
19
# File 'lib/urbit/graph.rb', line 17

def add_node(node:)
  @nodes << node unless node.deleted?
end

#creatorObject



21
22
23
24
# File 'lib/urbit/graph.rb', line 21

def creator
  self.fetch_link if @creator.nil?
  @creator
end

#descriptionObject



26
27
28
29
# File 'lib/urbit/graph.rb', line 26

def description
  self.fetch_link if @description.nil?
  @description
end

#groupObject



31
32
33
34
35
36
37
# File 'lib/urbit/graph.rb', line 31

def group
  if @group.nil?
    @link = self.fetch_link
    @group = @link.group unless @link.nil?
  end
  @group
end

#group=(a_group) ⇒ Object



39
40
41
# File 'lib/urbit/graph.rb', line 39

def group=(a_group)
  @group = a_group
end

#host_shipObject



43
44
45
# File 'lib/urbit/graph.rb', line 43

def host_ship
  "~#{@host_ship_name}"
end

#newer_sibling_nodes(node:, count:) ⇒ Object

Answers the count newer sibling nodes relative to the passed #node.



99
100
101
# File 'lib/urbit/graph.rb', line 99

def newer_sibling_nodes(node:, count:)
  self.fetch_sibling_nodes(node, :newer, count)[0..(count - 1)]
end

#newest_nodes(count: 10) ⇒ Object



79
80
81
82
83
84
# File 'lib/urbit/graph.rb', line 79

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).



59
60
61
# File 'lib/urbit/graph.rb', line 59

def node(index:)
  self.fetch_node(index).first
end

#nodesObject

Answers an array with all of this Graph’s currently attached Nodes, recursively inluding all of the Node’s children.



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/urbit/graph.rb', line 67

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.



106
107
108
# File 'lib/urbit/graph.rb', line 106

def older_sibling_nodes(node:, count:)
  self.fetch_sibling_nodes(node, :older, count)[0..(count - 1)]
end

#oldest_nodes(count: 10) ⇒ Object



86
87
88
89
90
# File 'lib/urbit/graph.rb', line 86

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

#resourceObject



92
93
94
# File 'lib/urbit/graph.rb', line 92

def resource
  "#{self.host_ship}/#{self.name}"
end

#titleObject



110
111
112
113
# File 'lib/urbit/graph.rb', line 110

def title
  self.fetch_link if @title.nil?
  @title
end

#to_sObject

the canonical printed representation of a Graph



122
123
124
# File 'lib/urbit/graph.rb', line 122

def to_s
  "a Graph(#{self.resource})"
end

#typeObject



115
116
117
118
# File 'lib/urbit/graph.rb', line 115

def type
  self.fetch_link if @type.nil?
  @type
end