Class: ActiveRecord::ShardFor::Config

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



6
7
8
9
# File 'lib/activerecord/shard_for/config.rb', line 6

def initialize
  @cluster_configs = {}
  @connection_routers = {}
end

Instance Attribute Details

#cluster_configsObject (readonly)

Returns the value of attribute cluster_configs.



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

def cluster_configs
  @cluster_configs
end

#connection_routersObject (readonly)

Returns the value of attribute connection_routers.



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

def connection_routers
  @connection_routers
end

Instance Method Details

#define_cluster(cluster_name) {|ActiveRecord::ShardFor::ClusterConfig| ... } ⇒ ActiveRecord::ShardFor::ClusterConfig

Define config for specific cluster. See README.md for example. raise [RuntimeError] when this cluster config is invalid.

Parameters:

  • cluster_name (String)

Yields:

Returns:



17
18
19
20
21
# File 'lib/activerecord/shard_for/config.rb', line 17

def define_cluster(cluster_name, &block)
  cluster_config = ClusterConfig.new(cluster_name)
  cluster_config.instance_eval(&block)
  cluster_configs[cluster_name] = cluster_config
end

#fetch_cluster_config(cluster_name) ⇒ ActiveRecord::ShardFor::ClusterConfig

Parameters:

  • cluster_name (Symbol)

Returns:

Raises:

  • (KeyError)

    when not registered key given



26
27
28
# File 'lib/activerecord/shard_for/config.rb', line 26

def fetch_cluster_config(cluster_name)
  cluster_configs.fetch(cluster_name)
end

#fetch_connection_router(connection_router_name) ⇒ Class

Returns registered class by [#register_router].

Parameters:

  • connection_router_name (Symbol)

Returns:

  • (Class)

    registered class by [#register_router]

Raises:

  • (KeyError)

    when not registerd router_name given



41
42
43
# File 'lib/activerecord/shard_for/config.rb', line 41

def fetch_connection_router(connection_router_name)
  connection_routers.fetch(connection_router_name)
end

#register_connection_router(router_name, router_class) ⇒ Object

Register connection router for ActiveRecord::ShardFor See README.md for example.

Parameters:

  • router_name (Symbol)


34
35
36
# File 'lib/activerecord/shard_for/config.rb', line 34

def register_connection_router(router_name, router_class)
  connection_routers[router_name] = router_class
end