Sha256: 558ddedc2d73c663247724a9160e436f25a9f7918b45967fcf8076c99217a587

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

module SoberSwag
  class Compiler
    ##
    # Compile multiple routes into a paths set.
    class Paths
      def initialize
        @routes = []
      end

      def add_route(route)
        @routes << route
      end

      def grouped_paths
        routes.group_by(&:path)
      end

      ##
      # Slightly weird method that gives you a compiled
      # paths list. Since this is only a compiler for paths,
      # it has *no idea* how to handle types. So, it takes a compiler
      # which it will use to do that for it.
      def paths_list(compiler)
        grouped_paths.transform_values do |values|
          values.map { |route|
            [route.method, compile_route(route, compiler)]
          }.to_h
        end
      end

      ##
      # Get a list of all types we discovered when compiling
      # the paths.
      def found_types
        return enum_for(:found_types) unless block_given?

        routes.each do |route|
          %i[body_class query_class path_params_class].each do |k|
            yield route.public_send(k) if route.public_send(k)
          end
        end
      end

      attr_reader :routes

      private

      def compile_route(route, compiler)
        SoberSwag::Compiler::Path.new(route, compiler).schema
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sober_swag-0.1.0 lib/sober_swag/compiler/paths.rb