Sha256: 97284f5e6f4c24dfadfb7a6f923295fcf30006bedd1fc5599a7f2d96c0800651

Contents?: true

Size: 752 Bytes

Versions: 1

Compression:

Stored size: 752 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

1 entries across 1 versions & 1 rubygems

Version Path
hierarchical_graph-1.0.0 lib/hierarchical_graph/node.rb