Class: ActiveRecordShards::ShardSelection

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_shards/shard_selection.rb

Constant Summary collapse

NO_SHARD =
:_no_shard

Instance Method Summary collapse

Constructor Details

#initializeShardSelection

Returns a new instance of ShardSelection.



7
8
9
# File 'lib/active_record_shards/shard_selection.rb', line 7

def initialize
  @on_slave = false
end

Instance Method Details

#on_slave=(new_slave) ⇒ Object



29
30
31
# File 'lib/active_record_shards/shard_selection.rb', line 29

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

#on_slave?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/active_record_shards/shard_selection.rb', line 25

def on_slave?
  @on_slave
end

#optionsObject



48
49
50
# File 'lib/active_record_shards/shard_selection.rb', line 48

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

#shard(klass = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/active_record_shards/shard_selection.rb', line 11

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
    end
  end
end

#shard=(new_shard) ⇒ Object



21
22
23
# File 'lib/active_record_shards/shard_selection.rb', line 21

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

#shard_name(klass = nil, try_slave = true) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/active_record_shards/shard_selection.rb', line 33

def shard_name(klass = nil, try_slave = true)
  the_shard = shard(klass)

  @shard_names ||= {}
  @shard_names[ActiveRecordShards.rails_env] ||= {}
  @shard_names[ActiveRecordShards.rails_env][the_shard] ||= {}
  @shard_names[ActiveRecordShards.rails_env][the_shard][try_slave] ||= {}
  @shard_names[ActiveRecordShards.rails_env][the_shard][try_slave][@on_slave] ||= begin
    s = ActiveRecordShards.rails_env.dup
    s << "_shard_#{the_shard}" if the_shard
    s << "_slave"              if @on_slave && try_slave
    s
  end
end