Sha256: dec7ba30e1625c8cc13113b149c48f65ea9a8d930810fd2c8a3f18314a1ed475

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

# typed: strict
# frozen_string_literal: true

require 'tsort'
module Yogurt
  class CodeGenerator
    class DefinedClassSorter
      extend T::Sig
      include TSort

      sig {params(classes: T::Array[DefinedClass]).void}
      def initialize(classes)
        @classes = T.let(
          classes.map {|k| [k.name, k]}.to_h,
          T::Hash[String, DefinedClass],
        )
      end

      sig {returns(T::Array[DefinedClass])}
      def sorted_classes
        tsort
      end

      sig {params(block: T.proc.params(arg0: DefinedClass).void).void}
      private def tsort_each_node(&block)
        @classes.keys.sort.each do |key|
          yield(@classes.fetch(key))
        end
      end

      sig do
        params(
          input_class: DefinedClass,
          block: T.proc.params(arg0: DefinedClass).void,
        ).void
      end
      private def tsort_each_child(input_class, &block)
        input_class.dependencies.sort.each do |name|
          yield(@classes.fetch(name))
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
yogurt-0.2.0 lib/yogurt/code_generator/defined_class_sorter.rb
yogurt-0.1.1 lib/yogurt/code_generator/defined_class_sorter.rb