class Society::ObjectGraph

The ObjectGraph class represents a graph of interrelated nodes as an Array of nodes which can be iterated over.

Public Class Methods

new(*nodes) click to toggle source

Public: Override Array#initialize, accepting any number of nodes and lists of nodes including other ObjectGraphs to create a single graph from them.

nodes - Any number of nodes or lists of nodes.

Calls superclass method
# File lib/society/object_graph.rb, line 12
def initialize(*nodes)
  super(nodes.flatten)
end

Public Instance Methods

+(other) click to toggle source

Public: Add two graphs together, returning a new ObjectGraph containing the sum of all nodes contained in both.

other - Another graph.

Returns an ObjectGraph.

# File lib/society/object_graph.rb, line 22
def +(other)
  other.reduce(self) do |graph, node|
    if graph.select { |n| n.intersects?(node) }.any?
      Society::ObjectGraph.new(graph.map { |n| n + node || n  })
    else
      Society::ObjectGraph.new(graph, node)
    end
  end
end
<<(node) click to toggle source

Public: Create a new graph, adding another node.

node - Node to be added to the new graph.

Returns an ObjectGraph.

# File lib/society/object_graph.rb, line 37
def <<(node)
  self + Society::ObjectGraph.new(node)
end
Also aliased as: push
push(node)
Alias for: <<
to_h() click to toggle source

Public: Return the graph represented as a Hash.

Returns a hash.

# File lib/society/object_graph.rb, line 45
def to_h
  self.reduce({}) do |hash, node|
    hash.merge({ node.name => node.edges })
  end
end
to_json() click to toggle source

Public: Return the graph as a JSON string.

Returns a string.

# File lib/society/object_graph.rb, line 54
def to_json
  to_h.reduce({}) do |hash, node|
    name, edges_raw = node
    edges = edges_raw.reduce({}) do |edges, edge|
      edges.merge({ edge.to => edge.weight })
    end
    hash.merge({ name => edges })
  end.to_json
end