Sha256: bda81979afbb6e1c32aef4ea0653ea60bca09b839005a4ef63d359d2ce8932e8

Contents?: true

Size: 1.95 KB

Versions: 2

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

module PG
  module AWS_RDS_IAM
    # Registers {AuthTokenGenerator}s to be used to generate authentication tokens for `PG::Connection`s that have the
    # `aws_rds_iam_auth_token_generator` connection parameter set to the registered name.
    class AuthTokenGeneratorRegistry
      # Creates a new `AuthTokenRegistry`.
      #
      # @param default_auth_token_generator_class [Class] the class to register as the default {AuthTokenGenerator}
      def initialize(default_auth_token_generator_class: AuthTokenGenerator)
        @default_auth_token_generator_class = default_auth_token_generator_class
        reset
      end

      # Registers an {AuthTokenGenerator}.
      #
      # @param name [String, Symbol]
      # @return [void]
      # @yieldreturn [AuthTokenGenerator]
      def add(name, &block)
        @registry[name.to_s] = Memoizer.new(&block)
      end

      # Looks up an {AuthTokenGenerator} by name.
      #
      # @param name [String, Symbol]
      # @return [AuthTokenGenerator]
      def fetch(name)
        @registry.fetch(name.to_s).call
      end

      # Unregisters an {AuthTokenGenerator}.
      #
      # @param name [String, Symbol]
      # @return [void]
      def remove(name)
        @registry.delete name.to_s
      end

      # Unregisters all {AuthTokenGenerator}s and re-registers the default {AuthTokenGenerator}.
      #
      # @return [void]
      def reset
        @registry = {}

        add :default do
          config = Aws::RDS::Client.new.config
          @default_auth_token_generator_class.new(credentials: config.credentials, region: config.region)
        end
      end

      class Memoizer # rubocop:disable Style/Documentation
        def initialize(&block)
          @block = block
        end

        def call
          return @auth_token_generator if defined?(@auth_token_generator)

          @auth_token_generator = @block.call
        end
      end

      private_constant :Memoizer
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pg-aws_rds_iam-0.1.1 lib/pg/aws_rds_iam/auth_token_generator_registry.rb
pg-aws_rds_iam-0.1.0 lib/pg/aws_rds_iam/auth_token_generator_registry.rb