class Urbit::Node

Attributes

node_json[RW]

Public Class Methods

new(graph:, node_json:) click to toggle source
# File lib/urbit/node.rb, line 6
def initialize(graph:, node_json:)
  @graph      = graph
  @post_h     = node_json['post']
  @children_h = node_json['children']
  @persistent = false
  @index      = nil
end

Public Instance Methods

<=>(another_node) click to toggle source
# File lib/urbit/node.rb, line 18
def <=>(another_node)
  self.time_sent <=> another_node.time_sent
end
==(another_node) click to toggle source
# File lib/urbit/node.rb, line 14
def ==(another_node)
  another_node.raw_index == self.raw_index
end
author() click to toggle source
# File lib/urbit/node.rb, line 30
def author
  @post_h["author"]
end
children() click to toggle source
# File lib/urbit/node.rb, line 34
def children
  @children = SortedSet.new
  if @children_h
    @children_h.each do |k, v|
      @children << (n = Urbit::Node.new(graph: @graph, node_json: v))
      # Recursively fetch all the children's children until we reach the bottom...
      n.children.each {|c| @children << c} if !n.children.empty?
    end
  end
  @children
end
contents() click to toggle source
# File lib/urbit/node.rb, line 46
def contents
  @post_h['contents']
end
eql?(another_node) click to toggle source
# File lib/urbit/node.rb, line 22
def eql?(another_node)
  another_node.raw_index == self.raw_index
end
hash() click to toggle source
# File lib/urbit/node.rb, line 26
def hash
  self.raw_index.hash
end
index() click to toggle source

Answers the memoized @index or calculates it from the raw_index.

# File lib/urbit/node.rb, line 57
def index
  return @index if @index
  @index = self.index_to_atom
end
next(count: 1) click to toggle source

Answers the next {count} Nodes relative to this Node. Defaults to the next Node if no {count} is passed.

# File lib/urbit/node.rb, line 66
def next(count: 1)
  @graph.newer_sibling_nodes(node: self, count: count)
end
persistent?() click to toggle source
# File lib/urbit/node.rb, line 50
def persistent?
  @persistent
end
previous(count: 1) click to toggle source

Answers the previous {count} Nodes relative to this Node. Defaults to the next Node if no {count} is passed.

# File lib/urbit/node.rb, line 74
def previous(count: 1)
  @graph.older_sibling_nodes(node: self, count: count)
end
raw_index() click to toggle source
# File lib/urbit/node.rb, line 78
def raw_index
  @post_h["index"].delete_prefix('/')
end
time_sent() click to toggle source
# File lib/urbit/node.rb, line 82
def time_sent
  @post_h['time-sent']
end
to_h() click to toggle source
# File lib/urbit/node.rb, line 86
def to_h
  {
    index: self.index,
    author: self.author,
    contents: self.contents,
    time_sent: self.time_sent,
    is_parent: !self.children.empty?,
    child_count: self.children.count
  }
end
to_s() click to toggle source
# File lib/urbit/node.rb, line 97
def to_s
  "a Node(#{self.to_h})"
end

Private Instance Methods

index_to_atom() click to toggle source
# File lib/urbit/node.rb, line 103
def index_to_atom
  subkeys = self.raw_index.split("/")
  subatoms = []
  subkeys.each do |s|
    subatoms << s.reverse.scan(/.{1,3}/).join('.').reverse
  end
  subatoms.join('/')
end