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/fixtures.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, Fixtures, NameNotUniqueError, NoActiveShardError, Railtie, Scope, ScopeManager, ShardCollection, ShardDefinition, ShardLookupHandler

Constant Summary collapse

VERSION =
'0.2.5'

Class Method Summary collapse

Class Method Details

.activate_shards(scopes = {}) ⇒ Object

Pushes active shards onto the scope without a block.



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

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

.add_shard(*args) ⇒ Object



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

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



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

def add_shard_observer( observer )
  shard_observers << observer
end

.base_schema_nameObject



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

def base_schema_name
  @base_schema_name
end

.base_schema_name=(val) ⇒ Object



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

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:



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

def config
  @config ||= Config.new

  yield( @config ) if block_given?

  @config
end

.environmentObject



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

def environment
  @environment
end

.environment=(val) ⇒ Object



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

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



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

def logger
  @logger
end

.logger=(val) ⇒ Object



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

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



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

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



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

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

.reload_observer_shards!Object



190
191
192
193
# File 'lib/active_shard.rb', line 190

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

.remove_shard(shard_name) ⇒ Object



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

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

  notify_shard_observers( :remove_shard, shard_name )
end

.schemasObject



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

def schemas
  config.schemas( environment )
end

.scope#push, ...

Returns current scope object

Returns:

  • (#push, #pop, #active_shard_for_schema)

    current scope object



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

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 )


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

def scope=( val )
  @scope = val
end

.shard(shard_name) ⇒ Object



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

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

.shard_configuration=(configuration) ⇒ Object



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

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

  reload_observer_shards!
end

.shard_definitionsObject



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

def shard_definitions
  config.shard_definitions( environment )
end

.shard_observersObject



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

def shard_observers
  @shard_observers ||= []
end

.shards_by_schema(schema_name) ⇒ Object



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

def shards_by_schema( schema_name )
  config.shards_by_schema( environment, 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



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

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



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

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

  yield

  self.environment = previous_environment
end