Class: Urbit::Graph

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

Direct Known Subclasses

PublishGraph

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

#deleteObject



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

def delete
  resp = self.ship.spider(mark_in: 'graph-view-action', mark_out: 'json', thread: 'graph-delete', data: self.delete_graph_json, args: ["NO_RESPONSE"])
  @persistent = !(200 == resp[:status])
end

#descriptionObject



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

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

#groupObject



36
37
38
39
40
41
42
# File 'lib/urbit/graph.rb', line 36

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

#group=(a_group) ⇒ Object



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

def group=(a_group)
  @group = a_group
end

#host_shipObject



48
49
50
# File 'lib/urbit/graph.rb', line 48

def host_ship
  "~#{@host_ship_name}"
end

#moltObject

Transform this Graph into a PublishGraph.

TODO: This is a very crude way to do this since we don’t get the type of graph back from

our initial call to retrieve the graphs, only later with the metadata.

This will need some more thought.


68
69
70
71
# File 'lib/urbit/graph.rb', line 68

def molt
  return PublishGraph.new(ship: self.ship, graph_name: self.name, title: self.title, description: self.description, persistent: true) if 'publish' == self.type
  self
end

#newer_sibling_nodes(node:, count:) ⇒ Object

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



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

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

#newest_nodes(count: 10) ⇒ Object



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

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



77
78
79
# File 'lib/urbit/graph.rb', line 77

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.



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/urbit/graph.rb', line 85

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.



124
125
126
# File 'lib/urbit/graph.rb', line 124

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

#oldest_nodes(count: 10) ⇒ Object



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

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



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

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

#titleObject



128
129
130
131
# File 'lib/urbit/graph.rb', line 128

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

#to_sObject

the canonical printed representation of a Graph



140
141
142
# File 'lib/urbit/graph.rb', line 140

def to_s
  "a #{self.class.name.split('::').last}(#{self.resource})"
end

#typeObject



133
134
135
136
# File 'lib/urbit/graph.rb', line 133

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