Sha256: 496001efb050ec4bffbc79418e43b8212f92108e0c9b464efd7073cd87626edf

Contents?: true

Size: 751 Bytes

Versions: 2

Compression:

Stored size: 751 Bytes

Contents

class HierarchicalGraph
  class Node

    attr_reader :id, :data

    def initialize(graph, id, data={})
      @graph = graph
      @id = id
      @data = data
    end

    def [](key)
      data[key]
    end

    def []=(key, value)
      data[key] = value
    end

    def parents
      graph.parents_of id
    end

    def children
      graph.children_of id
    end

    def ancestors
      graph.ancestors_of id
    end

    def descendants
      graph.descendants_of id
    end

    def root?
      parents.empty?
    end

    def to_s
      "<#{self.class.name} #{id} parents:[#{parents.map(&:id).join(', ')}] children:[#{children.map(&:id).join(', ')}]>"
    end
    alias_method :inspect, :to_s

    private

    attr_reader :graph

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hierarchical_graph-1.0.2 lib/hierarchical_graph/node.rb
hierarchical_graph-1.0.1 lib/hierarchical_graph/node.rb