Sha256: 6cde9d587d30e3a4e524eb4b4894d3506daea3c8fa86dade585c1438dc63fe68

Contents?: true

Size: 918 Bytes

Versions: 1

Compression:

Stored size: 918 Bytes

Contents

module ActiveRecord
  module ShardFor
    class AbstractShardRepository
      attr_reader :shards

      # @param [Symbol] connection_name
      # @return [Class] A model class for this shard
      def fetch(connection_name)
        shards.fetch(connection_name)
      end

      # @param [Object] key sharding key object for connection
      # @return [Class, nil] A AR model class.
      def fetch_by_key(key)
        shards.values.find do |model|
          case model.assigned_key
          when Range then model.assigned_key.include?(key)
          else model.assigned_key == key
          end
        end
      end

      # @return [Array<Class>]
      def all
        shards.values
      end

      private

      # @param [Symbol] connection_name
      # @return [String]
      def generate_class_name(connection_name)
        "ShardFor#{connection_name.to_s.tr('-', '_').classify}"
      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/abstract_shard_repository.rb