Module: ActiveRecord::Sharding::Model::ClassMethods
- Defined in:
- lib/active_record/sharding/model.rb
Instance Method Summary collapse
- #all_shards ⇒ Object
- #before_put(&block) ⇒ Object
- #define_parent_methods(&block) ⇒ Object
- #define_sharding_key(column) ⇒ Object
- #put!(attributes) ⇒ Object
- #shard_for(key) ⇒ Object
- #use_sharding(name, algorithm = :modulo) ⇒ Object
Instance Method Details
#all_shards ⇒ Object
49 50 51 |
# File 'lib/active_record/sharding/model.rb', line 49 def all_shards shard_repository.all end |
#before_put(&block) ⇒ Object
28 29 30 |
# File 'lib/active_record/sharding/model.rb', line 28 def before_put(&block) @before_put_callback = block end |
#define_parent_methods(&block) ⇒ Object
53 54 55 |
# File 'lib/active_record/sharding/model.rb', line 53 def define_parent_methods(&block) instance_eval(&block) end |
#define_sharding_key(column) ⇒ Object
24 25 26 |
# File 'lib/active_record/sharding/model.rb', line 24 def define_sharding_key(column) self.sharding_key = column.to_sym end |
#put!(attributes) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/active_record/sharding/model.rb', line 32 def put!(attributes) fail "`sharding_key` is not defined. Use `define_sharding_key`." unless sharding_key @before_put_callback.call(attributes) if @before_put_callback if key = attributes[sharding_key] || attributes[sharding_key.to_s] shard_for(key).create!(attributes) else fail ActiveRecord::Sharding::MissingShardingKeyAttribute end end |
#shard_for(key) ⇒ Object
44 45 46 47 |
# File 'lib/active_record/sharding/model.rb', line 44 def shard_for(key) connection_name = cluster_router.route key shard_repository.fetch connection_name end |
#use_sharding(name, algorithm = :modulo) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/active_record/sharding/model.rb', line 15 def use_sharding(name, algorithm = :modulo) config = ActiveRecord::Sharding.config.fetch_cluster_config name if algorithm == :modulo self.cluster_router = ActiveRecord::Sharding::ModuloRouter.new config end self.shard_repository = ActiveRecord::Sharding::ShardRepository.new config, self self.abstract_class = true end |