Class: ActiveRecord::ShardFor::ClusterConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord/shard_for/cluster_config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ ClusterConfig

Returns a new instance of ClusterConfig.

Parameters:

  • name (Symbol)


7
8
9
10
# File 'lib/activerecord/shard_for/cluster_config.rb', line 7

def initialize(name)
  @name = name
  @connection_registry = {}
end

Instance Attribute Details

#connection_registryObject (readonly)

Returns the value of attribute connection_registry.



4
5
6
# File 'lib/activerecord/shard_for/cluster_config.rb', line 4

def connection_registry
  @connection_registry
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/activerecord/shard_for/cluster_config.rb', line 4

def name
  @name
end

Instance Method Details

#connectionsArray<Symbol>

Returns An array of connection name.

Returns:

  • (Array<Symbol>)

    An array of connection name



23
24
25
# File 'lib/activerecord/shard_for/cluster_config.rb', line 23

def connections
  connection_registry.values
end

#fetch(key) ⇒ Symbol

Returns registered connection name.

Parameters:

  • key (Object)

Returns:

  • (Symbol)

    registered connection name

Raises:

  • (KeyError)

    when key is not registered



30
31
32
33
34
35
36
37
38
39
# File 'lib/activerecord/shard_for/cluster_config.rb', line 30

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

#register(key, connection_name) ⇒ Object

Parameters:

  • key (Object)

    sharding key object for connection

  • connection_name (Symbol)

Raises:

  • (RuntimeError)

    when duplicate entry of key



15
16
17
18
19
20
# File 'lib/activerecord/shard_for/cluster_config.rb', line 15

def register(key, connection_name)
  raise RuntimeError.new, "#{key} is registered" if connection_registry.key?(key)

  establish_connection(connection_name)
  connection_registry[key] = connection_name
end