Sha256: 78764f3f5978f48d99f98a04e94ba68703fe0e10f312d7d0807c74f042f89d1a

Contents?: true

Size: 1.24 KB

Versions: 1

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_class_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)
        class_name = generate_class_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}::#{class_name}"
            end
          RUBY
        end
        model
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activerecord-shard_for-0.5.0 lib/activerecord/shard_for/sti_shard_repository.rb