Sha256: 41dde2472d7d7b196bfe02606706227d323cfb49621319f029bc8c618acbc913

Contents?: true

Size: 1.79 KB

Versions: 7

Compression:

Stored size: 1.79 KB

Contents

module SoberSwag
  module Reporting
    module Output
      ##
      # Defer loading of an output for mutual recursion and/or loading time speed.
      # Probably just do this for mutual recursion though.
      #
      # Note: this *does not* save you from infinite schema generation.
      # This type *must* return some sort of {Referenced} type in order to do that!
      #
      # The common use case for this is mutual recursion.
      # Something like...
      #
      # ```ruby
      # class PersonOutput < SoberSwag::Reporting::Output::Struct
      #   field :first_name, SoberSwag::Reporting::Output.text
      #   view :detail do
      #     field :classes, SoberSwag::Reporting::Output::Defer.new { ClassroomOutput.view(:base).array }
      #   end
      # end
      #
      # class ClassroomOutut < SoberSwag::Reporting::Output::Struct
      #   field :class_name, SoberSwag::Reporting::Output.text
      #
      #   view :detail do
      #     field :students, SoberSwag::Reporting::Output::Defer.new { PersonOutput.view(:base).array }
      #   end
      # end
      # ```
      class Defer < Base
        ##
        # Nicer initialization: uses a block.
        #
        # @yieldreturn [Interface] serializer to use.
        def self.defer(&block)
          new(block)
        end

        def initialize(other_lazy)
          @other_lazy = other_lazy
        end

        attr_reader :other_lazy

        ##
        # @return [Interface]
        def other
          @other ||= other_lazy.call
        end

        def call(input)
          other.call(input)
        end

        def serialize_report(input)
          other.serialize_report(input)
        end

        def view(view)
          other.view(view)
        end

        def swagger_schema
          other.swagger_schema
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sober_swag-0.25.2 lib/sober_swag/reporting/output/defer.rb
sober_swag-0.25.1 lib/sober_swag/reporting/output/defer.rb
sober_swag-0.25.0 lib/sober_swag/reporting/output/defer.rb
sober_swag-0.24.1 lib/sober_swag/reporting/output/defer.rb
sober_swag-0.24.0 lib/sober_swag/reporting/output/defer.rb
sober_swag-0.23.0 lib/sober_swag/reporting/output/defer.rb
sober_swag-0.22.0 lib/sober_swag/reporting/output/defer.rb