Module: Kantox::Split::Graph::Vertex::InstanceMethods

Defined in:
lib/kantox/split/graph.rb

Instance Method Summary collapse

Instance Method Details

#graph_node_idObject



53
54
55
56
57
# File 'lib/kantox/split/graph.rb', line 53

def graph_node_id
  label = schild if respond_to?(:schild)
  label = label.empty? ? self.class.name : "\"#{label}\": #{self.class.name}"
  "#{label} «#{id rescue nil}»"
end

#leaf?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/kantox/split/graph.rb', line 42

def leaf?
  edges.empty?
end

#to_graph(levels = 0, g = RGL::DirectedAdjacencyGraph.new, root = self.graph_node_id) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/kantox/split/graph.rb', line 68

def to_graph levels = 0, g = RGL::DirectedAdjacencyGraph.new, root = self.graph_node_id
  vertices.each do |k, v|
    next if v.nil? || v.respond_to?(:empty?) && v.empty?
    next unless v.is_a? Vertex # FIXME ARRAY
    g.add_edge(root, vtx = v.graph_node_id)
    v.to_graph(levels - 1, g, vtx) if levels > 0 && v.respond_to?(:to_graph)
  end
  g
end

#to_h(levels = 0, collected = []) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/kantox/split/graph.rb', line 59

def to_h levels = 0, collected = []
  (respond_to?(:embedded) && embedded || {}).merge(
    vertices.map do |k, v|
      next if v.nil? || v.respond_to?(:empty?) && v.empty?
      [k[:name], Kantox::Split::Utils.omnivorous_to_h(v, levels, collected, 'obj.respond_to?(:graph_node_id) ? obj.graph_node_id : obj.__id__')]
    end.compact.to_h
  )
end

#verticesObject



46
47
48
49
50
51
# File 'lib/kantox/split/graph.rb', line 46

def vertices
  return [] unless respond_to? :edges
  edges.map do |edge|
    edge.vertex_getter.call self rescue nil
  end.compact
end