Sha256: 2ba1da17e655d55a81c35ad456dd140f90cdd6209e4a290df949176e8b0d6b9b

Contents?: true

Size: 1.24 KB

Versions: 3

Compression:

Stored size: 1.24 KB

Contents

module ActiveRecord
  module ShardFor
    class STIShardRepository < AbstractShardRepository
      attr_reader :inherited_class

      # @param [Class] A sub class of AR model.
      # @base_shards [Array<Class>] An array of shard models.
      def initialize(inherited_class, base_shards)
        @inherited_class = inherited_class

        @shards = base_shards.each_with_object({}) do |(connection_name, base_model), hash|
          model = generate_model_from_shard(connection_name, base_model)
          inherited_class.const_set(:"#{generate_shard_name(connection_name)}", model)
          hash[connection_name] = model
        end
      end

      private

      # @param [Symbol] connection_name
      # @param [Class] A class of shard model.
      # @return [Class] A sub class of given model.
      def generate_model_from_shard(connection_name, base_model)
        shard_name = generate_shard_name(connection_name)
        module_name = inherited_class.name

        model = Class.new(base_model) do
          @assigned_key = base_model.assigned_key

          module_eval <<-RUBY, __FILE__, __LINE__ + 1
            def self.name
              "#{module_name}::#{shard_name}"
            end
          RUBY
        end
        model
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
activerecord-shard_for-0.7.0 lib/activerecord/shard_for/sti_shard_repository.rb
activerecord-shard_for-0.6.1 lib/activerecord/shard_for/sti_shard_repository.rb
activerecord-shard_for-0.6.0 lib/activerecord/shard_for/sti_shard_repository.rb