Class: MarsBase10::Ship

Inherits:
Object
  • Object
show all
Defined in:
lib/mars_base_10/ship.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection:) ⇒ Ship

Returns a new instance of Ship.



7
8
9
# File 'lib/mars_base_10/ship.rb', line 7

def initialize(connection:)
  @ship = connection
end

Instance Method Details

#empty_nodeObject



11
12
13
# File 'lib/mars_base_10/ship.rb', line 11

def empty_node
  Subject.new title: 'Node', contents: []
end

#empty_node_listObject



15
16
17
# File 'lib/mars_base_10/ship.rb', line 15

def empty_node_list
  Subject.new title: 'Node List', contents: []
end

#fetch_channel(group_title:, channel_title:) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/mars_base_10/ship.rb', line 27

def fetch_channel(group_title:, channel_title:)
  if (group = @ship.groups[title: group_title])
    if (channel = group.graphs.select {|c| channel_title == c.title unless c.nil?}.first)
      return channel
    end
  end
  nil
end

#fetch_channel_props(group_title:, channel_title:) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mars_base_10/ship.rb', line 36

def fetch_channel_props(group_title:, channel_title:)
  if (channel = self.fetch_channel(group_title: group_title, channel_title: channel_title))
    # What we are calling a channel here is really a graph in the urbit-ruby bridge.
    # This is the equivalent of node.to_pretty_array
    props = {
      title:       channel.title,
      description: channel.description,
      creator:     channel.creator,
      host_ship:   channel.host_ship,
      resource:    channel.resource,
      type:        channel.type
    }
    return props.each.map {|k, v| "#{k}#{(' ' * [(18 - k.length), 0].max)}#{v}"}
  end
  ["Channel not found."]
end

#fetch_group(group_title:) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/mars_base_10/ship.rb', line 53

def fetch_group(group_title:)
  if (group = @ship.groups[title: group_title])
    # This is the equivalent of node.to_pretty_array
    return group.to_h.each.map {|k, v| "#{k}#{(' ' * [(18 - k.length), 0].max)}#{v}"}
  end
  ["Group not found."]
end

#fetch_group_channels(group_title:) ⇒ Object



61
62
63
64
65
66
# File 'lib/mars_base_10/ship.rb', line 61

def fetch_group_channels(group_title:)
  if (group = @ship.groups[title: group_title])
    return group.graphs.map {|g| g.nil? ? "Unnamed" : g.title}
  end
  ["No Channels Available."]
end

#fetch_node(resource:, index:) ⇒ Object



68
69
70
# File 'lib/mars_base_10/ship.rb', line 68

def fetch_node(resource:, index:)
  @ship.graph(resource: resource).node(index: index)
end

#fetch_node_children(resource:, index:) ⇒ Object



72
73
74
# File 'lib/mars_base_10/ship.rb', line 72

def fetch_node_children(resource:, index:)
  self.fetch_node(resource: resource, index: index).children.map {|node| node.index}.sort
end

#fetch_node_contents(resource:, index:) ⇒ Object



76
77
78
79
# File 'lib/mars_base_10/ship.rb', line 76

def fetch_node_contents(resource:, index:)
  return [] unless (n = self.fetch_node(resource: resource, index: index))
  n.to_pretty_array
end

#fetch_node_list(resource:, count: 60) ⇒ Object



81
82
83
# File 'lib/mars_base_10/ship.rb', line 81

def fetch_node_list(resource:, count: 60)
  @ship.graph(resource: resource).newest_nodes(count: count).map {|node| node.index}.sort
end

#fetch_older_nodes(resource:, node:, count: 60) ⇒ Object



85
86
87
# File 'lib/mars_base_10/ship.rb', line 85

def fetch_older_nodes(resource:, node:, count: 60)
  @ship.graph(resource: resource).older_sibling_nodes(node: node, count: count).map {|node| node.index}.sort
end

#graph_namesObject



19
20
21
# File 'lib/mars_base_10/ship.rb', line 19

def graph_names
  Subject.new title: 'Graphs', contents: @ship.graph_names
end

#group_namesObject



23
24
25
# File 'lib/mars_base_10/ship.rb', line 23

def group_names
  Subject.new title: 'Groups', contents: (@ship.groups.map {|g| g.to_list})
end