Sha256: 8af3d43006e5d24c84a8db8c564ff719871abacb2c8e45eed1119511557a42d1

Contents?: true

Size: 1.19 KB

Versions: 7

Compression:

Stored size: 1.19 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
      # @raise [KeyError] when key is not registered
      def fetch(key)
        connection_registry.each do |connection_key, connection|
          case connection_key
          when Range then return connection if connection_key.include?(key)
          else return connection if connection_key == key
          end
        end

        raise KeyError.new, "#{key} is not registerd connection"
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
activerecord-shard_for-0.6.1 lib/activerecord/shard_for/cluster_config.rb
activerecord-shard_for-0.6.0 lib/activerecord/shard_for/cluster_config.rb
activerecord-shard_for-0.5.0 lib/activerecord/shard_for/cluster_config.rb
activerecord-shard_for-0.4.1 lib/activerecord/shard_for/cluster_config.rb
activerecord-shard_for-0.4.0 lib/activerecord/shard_for/cluster_config.rb
activerecord-shard_for-0.3.0 lib/activerecord/shard_for/cluster_config.rb
activerecord-shard_for-0.2.1 lib/activerecord/shard_for/cluster_config.rb