module ActiveRecord::Sharding::Model::ClassMethods

Public Instance Methods

all_shards() click to toggle source
# File lib/active_record/sharding/model.rb, line 58
def all_shards
  shard_repository.all
end
all_shards_in_parallel() click to toggle source
# File lib/active_record/sharding/model.rb, line 62
def all_shards_in_parallel
  AllShardsInParallel.new(all_shards)
end
Also aliased as: parallel
before_put(&block) click to toggle source
# File lib/active_record/sharding/model.rb, line 30
def before_put(&block)
  @before_put_callback = block
end
define_parent_methods(&block) click to toggle source
# File lib/active_record/sharding/model.rb, line 67
def define_parent_methods(&block)
  instance_eval(&block)
end
define_sharding_key(column) click to toggle source
# File lib/active_record/sharding/model.rb, line 26
def define_sharding_key(column)
  self.sharding_key = column.to_sym
end
parallel()
put!(attributes) { |object| ... } click to toggle source
# File lib/active_record/sharding/model.rb, line 34
def put!(attributes)
  raise "`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]
    if block_given?
      shard_for(key).transaction do
        object = shard_for(key).create!(attributes)
        yield(object)
      end
    else
      shard_for(key).create!(attributes)
    end
  else
    raise ActiveRecord::Sharding::MissingShardingKeyAttribute
  end
end
shard_for(key) click to toggle source
# File lib/active_record/sharding/model.rb, line 53
def shard_for(key)
  connection_name = cluster_router.route key
  shard_repository.fetch connection_name
end
use_sharding(name, algorithm = :modulo) click to toggle source
# File lib/active_record/sharding/model.rb, line 17
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