Sha256: d38f48e308823049cb9a6ad8212f75fa9c5a38ee456c3e9f3a779cc07bd3aa77

Contents?: true

Size: 1.82 KB

Versions: 43

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

module Karafka
  module Routing
    module Features
      class Base
        # Routing builder expander that injects feature related drawing operations into it
        class Expander < Module
          # @param scope [Module] feature scope in which contract and other things should be
          # @return [Expander] builder expander instance
          def initialize(scope)
            super()
            @scope = scope
          end

          # Builds anonymous module that alters how `#draw` behaves allowing the feature contracts
          # to run.
          # @param mod [::Karafka::Routing::Builder] builder we will prepend to
          def prepended(mod)
            super

            mod.prepend(prepended_module)
          end

          private

          # @return [Module] builds an anonymous module with `#draw` that will alter the builder
          #   `#draw` allowing to run feature context aware code.
          def prepended_module
            scope = @scope

            Module.new do
              # Runs validations related to this feature on a routing resources
              #
              # @param block [Proc] routing defining block
              define_method :draw do |&block|
                result = super(&block)

                each do |consumer_group|
                  if scope::Contracts.const_defined?('ConsumerGroup', false)
                    scope::Contracts::ConsumerGroup.new.validate!(consumer_group.to_h)
                  end

                  next unless scope::Contracts.const_defined?('Topic', false)

                  consumer_group.topics.each do |topic|
                    scope::Contracts::Topic.new.validate!(topic.to_h)
                  end
                end

                result
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
karafka-2.4.17 lib/karafka/routing/features/base/expander.rb
karafka-2.4.16 lib/karafka/routing/features/base/expander.rb
karafka-2.4.15 lib/karafka/routing/features/base/expander.rb
karafka-2.4.14 lib/karafka/routing/features/base/expander.rb
karafka-2.4.13 lib/karafka/routing/features/base/expander.rb
karafka-2.4.12 lib/karafka/routing/features/base/expander.rb
karafka-2.4.11 lib/karafka/routing/features/base/expander.rb
karafka-2.4.10 lib/karafka/routing/features/base/expander.rb
karafka-2.4.9 lib/karafka/routing/features/base/expander.rb
karafka-2.4.8 lib/karafka/routing/features/base/expander.rb
karafka-2.4.7 lib/karafka/routing/features/base/expander.rb
karafka-2.4.6 lib/karafka/routing/features/base/expander.rb
karafka-2.4.5 lib/karafka/routing/features/base/expander.rb
karafka-2.4.4 lib/karafka/routing/features/base/expander.rb
karafka-2.4.3 lib/karafka/routing/features/base/expander.rb
karafka-2.4.0 lib/karafka/routing/features/base/expander.rb
karafka-2.4.0.rc1 lib/karafka/routing/features/base/expander.rb
karafka-2.3.4 lib/karafka/routing/features/base/expander.rb
karafka-2.4.0.beta2 lib/karafka/routing/features/base/expander.rb
karafka-2.4.0.beta1 lib/karafka/routing/features/base/expander.rb