Class: Urbit::Fact::MetadataUpdateFact

Inherits:
BaseFact
  • Object
show all
Defined in:
lib/urbit/fact/metadata_fact.rb

Instance Attribute Summary

Attributes inherited from BaseFact

#ack, #channel, #data, #type

Instance Method Summary collapse

Methods inherited from BaseFact

#add_ack, #contents, #create_parser, #for_this_ship?, #graph_update?, #is_acknowledged?, #raw_json, #ship, #to_s

Constructor Details

#initialize(channel:, event:) ⇒ MetadataUpdateFact

Returns a new instance of MetadataUpdateFact.



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

def initialize(channel:, event:)
  super channel: channel, event: event
  unless self.is_remove
    self.accept if self.for_this_ship?
  end
  # TODO: Remove this debugging once Facts are finalized. DJR 2/3/2022
  # puts "Received a #{self.class.name.split('::').last} for [#{channel}] -- [#{@type}] -- [#{@data}]"
end

Instance Method Details

#acceptObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/urbit/fact/metadata_fact.rb', line 17

def accept
  links = self.channel.ship.links
  if self.add_root.nil?
    # This is an new incoming Link, add it. If its a dupe, the Links Set will filter it.
    self.associations.each do |k, v|
      links << Link.new(chain: links, path: k, data: v)
    end
  else
    g = self.add_root["group"]
    links << Link.new(chain: links, path: "#{g}/groups#{g}", data: self.add_root)
  end
  nil
end

#add_rootObject



31
32
33
# File 'lib/urbit/fact/metadata_fact.rb', line 31

def add_root
  self.root_h["add"]
end

#associationsObject



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

def associations
  # An ugly hacks around the fact that when you first join a group there is another
  # useless "initial-update" enclosing hash.
  unless (a = self.root_h["associations"])
    return self.root_h["initial-group"]["associations"]
  end
  a
end

#is_removeObject



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

def is_remove
  self.root_h["remove"]
end

#root_hObject



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

def root_h
  self.contents["json"]["metadata-update"]
end

#to_hObject



52
53
54
# File 'lib/urbit/fact/metadata_fact.rb', line 52

def to_h
  super.merge!(resource: self.resource)
end