notCalle/ruby-tangle

View on GitHub
lib/tangle/directed/edge.rb

Summary

Maintainability
A
0 mins
Test Coverage
# frozen_string_literal: true

require_relative '../edge'

module Tangle
  module Directed
    #
    # An edge in a directed graph
    #
    class Edge < Tangle::Edge
      attr_reader :head, :tail

      def head?(vertex)
        @head == vertex
      end

      def tail?(vertex)
        @tail == vertex
      end

      def each_vertex(&block)
        [@tail, @head].each(&block)
      end

      def to_s
        "{#{@tail}-->#{@head}}"
      end
      alias inspect to_s

      private

      def initialize_vertices(tail, head = tail)
        super
        @tail = tail
        @head = head
        @vertices = { tail => head }.freeze
      end
    end
  end
end