Module: ActiveShard

Extended by:
ActiveSupport::Autoload
Defined in:
lib/active_shard.rb,
lib/active_shard/scope.rb,
lib/active_shard/config.rb,
lib/active_shard/railtie.rb,
lib/active_shard/version.rb,
lib/active_shard/exceptions.rb,
lib/active_shard/active_record.rb,
lib/active_shard/scope_manager.rb,
lib/active_shard/shard_collection.rb,
lib/active_shard/shard_definition.rb,
lib/active_shard/shard_lookup_handler.rb,
lib/active_shard/active_record/sharded_base.rb,
lib/active_shard/active_record/shard_support.rb,
lib/active_shard/active_record/connection_handler.rb,
lib/active_shard/active_record/connection_proxy_pool.rb,
lib/active_shard/active_record/schema_connection_proxy.rb,
lib/active_shard/active_record/connection_specification_adapter.rb

Defined Under Namespace

Modules: ActiveRecord Classes: ActiveShardError, Config, DefinitionError, NameNotUniqueError, NoActiveShardError, Railtie, Scope, ScopeManager, ShardCollection, ShardDefinition, ShardLookupHandler

Constant Summary collapse

VERSION =
'0.2.3'

Class Method Summary collapse

Class Method Details

.activate_shards(scopes = {}) ⇒ Object

Pushes active shards onto the scope without a block.



165
166
167
# File 'lib/active_shard.rb', line 165

def activate_shards( scopes={} )
  scope.push( scopes )
end

.add_shard(*args) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/active_shard.rb', line 88

def add_shard( *args )
  definition = args.first.is_a?( ShardDefinition ) ? args.first : ShardDefinition.new( *args )

  config.add_shard( environment, definition )

  notify_shard_observers( :add_shard, definition )

  definition
end

.add_shard_observer(observer) ⇒ Object



80
81
82
# File 'lib/active_shard.rb', line 80

def add_shard_observer( observer )
  shard_observers << observer
end

.base_schema_nameObject



54
55
56
# File 'lib/active_shard.rb', line 54

def base_schema_name
  @base_schema_name
end

.base_schema_name=(val) ⇒ Object



50
51
52
# File 'lib/active_shard.rb', line 50

def base_schema_name=(val)
  @base_schema_name = val.nil? ? nil : val.to_sym
end

.config {|c| ... } ⇒ Config

Returns the current Config object for ActiveShard.

Yields:

  • (c)

    yields the current config object to the block for setup

Returns:



42
43
44
45
46
47
48
# File 'lib/active_shard.rb', line 42

def config
  @config ||= Config.new

  yield( @config ) if block_given?

  @config
end

.environmentObject



32
33
34
# File 'lib/active_shard.rb', line 32

def environment
  @environment
end

.environment=(val) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/active_shard.rb', line 24

def environment=( val )
  env_changed = !( @environment.to_s == val.to_s )

  @environment = val.nil? ? nil : val.to_sym

  reload_observer_shards! if env_changed
end

.loggerObject



177
178
179
# File 'lib/active_shard.rb', line 177

def logger
  @logger
end

.logger=(val) ⇒ Object



181
182
183
# File 'lib/active_shard.rb', line 181

def logger=(val)
  @logger = val
end

.notify_shard_observers(message, *args) ⇒ Object

Doesn’t yet support anything other than ActiveRecord::Base

def base_class=(val)

@base_class = val

end

def base_class

@base_class

end



74
75
76
77
78
# File 'lib/active_shard.rb', line 74

def notify_shard_observers( message, *args )
  shard_observers.each do |observer|
    observer.public_send( message, *args ) if observer.respond_to?( message )
  end
end

.pop_to(scopes) ⇒ Object



169
170
171
# File 'lib/active_shard.rb', line 169

def pop_to( scopes )
  scope.pop( scopes )
end

.reload_observer_shards!Object



185
186
187
188
# File 'lib/active_shard.rb', line 185

def reload_observer_shards!
  notify_shard_observers( :remove_all_shards! )
  notify_shard_observers( :add_shards, shard_definitions )
end

.remove_shard(shard_name) ⇒ Object



98
99
100
101
102
# File 'lib/active_shard.rb', line 98

def remove_shard( shard_name )
  config.remove_shard( environment, shard_name )

  notify_shard_observers( :remove_shard, shard_name )
end

.scope#push, ...

Returns current scope object

Returns:

  • (#push, #pop, #active_shard_for_schema)

    current scope object



125
126
127
# File 'lib/active_shard.rb', line 125

def scope
  @scope ||= ScopeManager.new
end

.scope=(val) ⇒ Object

Sets the current scope handling object.

Scope handler must respond to the following methods:

#push( scope ), #pop( scopes ), #active_shard_for_schema( schema )


117
118
119
# File 'lib/active_shard.rb', line 117

def scope=( val )
  @scope = val
end

.shard(shard_name) ⇒ Object



108
109
110
# File 'lib/active_shard.rb', line 108

def shard( shard_name )
  config.shard( environment, shard_name )
end

.shard_configuration=(configuration) ⇒ Object



58
59
60
61
62
# File 'lib/active_shard.rb', line 58

def shard_configuration=( configuration )
  config.shard_configuration=( configuration )

  reload_observer_shards!
end

.shard_definitionsObject



104
105
106
# File 'lib/active_shard.rb', line 104

def shard_definitions
  config.shard_definitions( environment )
end

.shard_observersObject



84
85
86
# File 'lib/active_shard.rb', line 84

def shard_observers
  @shard_observers ||= []
end

.shards_by_schema(schema_name) ⇒ Object



173
174
175
# File 'lib/active_shard.rb', line 173

def shards_by_schema( schema_name )
  config.shards_by_schema( schema_name )
end

.with(scopes = {}, &block) ⇒ Object

Sets the active shards before yielding, and reverts them before returning.

This method will also pop off any additional scopes that were added by the provided block if they were not already popped.

Examples:


ActiveShard.with( :users => :user_db1 ) do
  ActiveShard.with( :users => :user_db2 ) do
    ActiveShard.activate_shards( :users => :user_db3 )
    # 3 shard entries on scope stack
  end
  # 1 shard entry on scope stack
end

Parameters:

  • scopes (Hash) (defaults to: {})

    schemas (keys) and active shards (values)

Returns:

  • the return value from the provided block



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/active_shard.rb', line 148

def with( scopes={}, &block )
  ret     = nil
  memento = nil

  begin
    memento = activate_shards( scopes )

    ret = block.call()
  ensure
    pop_to( memento )
  end

  ret
end

.with_environment(val) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/active_shard.rb', line 15

def with_environment( val )
  previous_environment = self.environment
  self.environment = val

  yield

  self.environment = previous_environment
end