Sha256: f7427cd87902ffec3131e7b34328ec084b622c7d2c2cb77c58c174757f2fa08c

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

module ActiveRecord
  module ShardFor
    class ClusterConfig
      attr_reader :name, :connection_registry

      # @param [Symbol] name
      def initialize(name)
        @name = name
        @connection_registry = {}
      end

      # @param [Object] key sharding key object for connection
      # @param [Symbol] connection_name
      # @raise [RuntimeError] when duplicate entry of  key
      def register(key, connection_name)
        raise RuntimeError.new, "#{key} is registered" if connection_registry.key?(key)
        connection_registry[key] = connection_name
      end

      # @return [Array<Symbol>] An array of connection name
      def connections
        connection_registry.values
      end

      # @param [Object] key
      # @return [Symbol] registered connection name
      def fetch(key)
        connection_registry.find do |connection_key, _connection|
          case connection_key
          when Range then connection_key.include?(key)
          else connection_key == key
          end
        end.second
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activerecord-shard_for-0.2.0 lib/activerecord/shard_for/cluster_config.rb