Created
August 5, 2011 17:42
-
-
Save nickh/1128071 to your computer and use it in GitHub Desktop.
Dynashard class generation cleanup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/lib/dynashard.rb b/lib/dynashard.rb | |
| index ef63584..220a47b 100644 | |
| --- a/lib/dynashard.rb | |
| +++ b/lib/dynashard.rb | |
| @@ -122,6 +122,53 @@ | |
| # - uniqueness validations should be scoped by whatever is sharding | |
| module Dynashard | |
| + module GeneratedModelClass | |
| + extend ActiveSupport::Concern | |
| + | |
| + included do | |
| + alias_method_chain :initialize, :dynashard | |
| + | |
| + class << self | |
| + attr_accessor :dynashard_klass | |
| + end | |
| + end | |
| + | |
| + module ClassMethods | |
| + def dynashard_model?() | |
| + true | |
| + end | |
| + | |
| + def connection | |
| + dynashard_klass.connection | |
| + end | |
| + | |
| + def dynashard_context | |
| + superclass.dynashard_context | |
| + end | |
| + | |
| + def dynashard_per_table_lookup | |
| + superclass.dynashard_per_table_lookup | |
| + end | |
| + | |
| + def model_name | |
| + superclass.model_name | |
| + end | |
| + end | |
| + | |
| + module InstanceMethods | |
| + def initialize_with_dynashard(*args, &block) | |
| + set_dynashard_context | |
| + initialize_without_dynashard(*args, &block) | |
| + end | |
| + | |
| + private | |
| + | |
| + def set_dynashard_context | |
| + @dynashard_context = Dynashard.shard_context.dup | |
| + end | |
| + end | |
| + end | |
| + | |
| # Enable sharding for all models configured to do so | |
| def self.enable | |
| @enabled = true | |
| @@ -157,9 +204,7 @@ module Dynashard | |
| # Return a class with an established connection to a database shard | |
| def self.class_for(spec) | |
| - @class_cache ||= {} | |
| - @class_cache[spec] ||= new_shard_class(spec) | |
| - @class_cache[spec] | |
| + class_cache[spec] ||= new_shard_class(spec) | |
| end | |
| # Return a reflection with a sharded class | |
| @@ -224,46 +269,11 @@ module Dynashard | |
| unless shard_klass.constants.include?(subclass_name.to_sym) | |
| class_eval <<-EOE, __FILE__, __LINE__ | |
| class #{class_name} < #{base_klass.name} | |
| - @@dynashard_klass = #{shard_klass.name} | |
| - | |
| - def self.dynashard_model?() | |
| - true | |
| - end | |
| - | |
| - def self.dynashard_klass() | |
| - @@dynashard_klass | |
| - end | |
| - | |
| - def self.connection | |
| - dynashard_klass.connection | |
| - end | |
| - | |
| - def self.dynashard_context | |
| - superclass.dynashard_context | |
| - end | |
| - | |
| - def self.dynashard_per_table_lookup | |
| - superclass.dynashard_per_table_lookup | |
| - end | |
| - | |
| - def self.model_name | |
| - superclass.model_name | |
| - end | |
| - | |
| - def initialize_with_dynashard(*args, &block) | |
| - set_dynashard_context | |
| - initialize_without_dynashard(*args, &block) | |
| - end | |
| - alias_method_chain :initialize, :dynashard | |
| - | |
| - private | |
| - | |
| - def set_dynashard_context | |
| - @dynashard_context = Dynashard.shard_context.dup | |
| - end | |
| + include Dynashard::GeneratedModelClass | |
| end | |
| EOE | |
| klass = class_name.constantize | |
| + klass.dynashard_klass = shard_klass | |
| klass.connection_handler.dynashard_pool_alias(klass.name, shard_klass.name) | |
| if sharded_table_name = base_klass.dynashard_table_name | |
| klass.set_table_name sharded_table_name | |
| @@ -290,13 +300,17 @@ EOE | |
| end | |
| def self.shard_constants | |
| - (0...@class_cache.size).map{|i| const_get "Shard#{i}" } | |
| + class_cache.values | |
| end | |
| private | |
| + def self.class_cache | |
| + @class_cache ||= {} | |
| + end | |
| + | |
| def self.new_shard_class(spec) | |
| - shard_number = @class_cache.size | |
| + shard_number = class_cache.size | |
| klass_name = "Shard#{shard_number}" | |
| unless const_defined?(klass_name) | |
| module_eval("class #{klass_name} < ActiveRecord::Base ; end") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment