Sha256: 539ea0cd245f4bd4830315f50939fd6667022b5cb5b879adb5563ab7f9ac2958

Contents?: true

Size: 830 Bytes

Versions: 2

Compression:

Stored size: 830 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 root?
      parents.empty?
    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 descendants_subgraph
      graph.descendants_subgraph_from id
    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.1.1 lib/hierarchical_graph/node.rb
hierarchical_graph-1.1.0 lib/hierarchical_graph/node.rb