Class: Gritty::NodeBuilder

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

Instance Method Summary collapse

Constructor Details

#initialize(graph) ⇒ NodeBuilder

Returns a new instance of NodeBuilder.



14
15
16
# File 'lib/gritty.rb', line 14

def initialize(graph)
  @graph = graph
end

Instance Method Details

#build(obj, parent) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gritty.rb', line 18

def build(obj, parent)
  case obj
  when Hash then
    obj.each do |k, v|
      node = @graph.autonode k.inspect
      @graph.edge parent, node
      build v, node
    end
  when Array then
    obj.each do |o|
      build o, parent
    end
  when Struct then
    container = @graph.autonode obj.class.to_s
    @graph.edge parent, container

    obj.members.each do |k|
      v = obj.send k
      node = @graph.autonode k.inspect
      @graph.edge container, node
      build v, node
    end
  else
    node = obj.inspect
    @graph.edge parent, node
  end
end