Sha256: abf67edd3ceb777be459a3f8d241406291bd5027d23a05d2fcc22c846eb2a700

Contents?: true

Size: 943 Bytes

Versions: 11

Compression:

Stored size: 943 Bytes

Contents

module ActiveRecordShards
  class ShardSelection
    NO_SHARD = :_no_shard
    cattr_accessor :default_shard

    def initialize
      @on_slave = false
    end

    def shard(klass = nil)
      if (@shard || self.class.default_shard) && (klass.nil? || klass.is_sharded?)
        if @shard == NO_SHARD
          nil
        else
          (@shard || self.class.default_shard).to_s
        end
      end
    end

    def shard=(new_shard)
      @shard = (new_shard || NO_SHARD)
    end

    def on_slave?
      @on_slave
    end

    def on_slave=(new_slave)
      @on_slave = (new_slave == true)
    end

    def shard_name(klass = nil, try_slave = true)
      s = "#{RAILS_ENV}"
      if the_shard = shard(klass)
        s << '_shard_'
        s << the_shard
      end
      if @on_slave && try_slave
        s << "_slave" if @on_slave
      end
      s
    end

    def options
      {:shard => @shard, :slave => @on_slave}
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
active_record_shards-2.5.0 lib/active_record_shards/shard_selection.rb
active_record_shards-2.4.5 lib/active_record_shards/shard_selection.rb
active_record_shards-2.4.4 lib/active_record_shards/shard_selection.rb
active_record_shards-2.4.3 lib/active_record_shards/shard_selection.rb
active_record_shards-2.4.2 lib/active_record_shards/shard_selection.rb
active_record_shards-2.4.1 lib/active_record_shards/shard_selection.rb
active_record_shards-2.3.1 lib/active_record_shards/shard_selection.rb
active_record_shards-2.3.0 lib/active_record_shards/shard_selection.rb
active_record_shards-2.2.0 lib/active_record_shards/shard_selection.rb
active_record_shards-2.0.0 lib/active_record_shards/shard_selection.rb
active_record_shards-2.0.0.beta5 lib/active_record_shards/shard_selection.rb