Sha256: 5e6b74888b0a06aa07c9e90c20bf042bb238f8dd3b5b84b952f7b0db1078d813

Contents?: true

Size: 742 Bytes

Versions: 15

Compression:

Stored size: 742 Bytes

Contents

module DbCharmer
  module Sharding
    module Method
      class Range
        attr_accessor :ranges

        def initialize(config)
          @ranges = config[:ranges] ? config[:ranges].clone : raise(ArgumentError, "No :ranges defined!")
        end

        def shard_for_key(key)
          return ranges[:default] if key == :default

          ranges.each do |range, shard|
            next if range == :default
            return shard if range.member?(key.to_i)
          end

          return ranges[:default] if ranges[:default]
          raise ArgumentError, "Invalid key value, no shards found for this key!"
        end

        def support_default_shard?
          ranges.has_key?(:default)
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
db-charmer-1.6.14 lib/db_charmer/sharding/method/range.rb
db-charmer-1.6.13 lib/db_charmer/sharding/method/range.rb
db-charmer-1.6.12 lib/db_charmer/sharding/method/range.rb
db-charmer-1.6.11 lib/db_charmer/sharding/method/range.rb
db-charmer-1.6.10 lib/db_charmer/sharding/method/range.rb
db-charmer-1.6.9 lib/db_charmer/sharding/method/range.rb
db-charmer-1.6.8 lib/db_charmer/sharding/method/range.rb
db-charmer-1.6.7 lib/db_charmer/sharding/method/range.rb
db-charmer-1.6.6 lib/db_charmer/sharding/method/range.rb
db-charmer-1.6.5 lib/db_charmer/sharding/method/range.rb
db-charmer-1.6.4 lib/db_charmer/sharding/method/range.rb
db-charmer-1.6.3 lib/db_charmer/sharding/method/range.rb
db-charmer-1.6.2 lib/db_charmer/sharding/method/range.rb
db-charmer-1.6.1 lib/db_charmer/sharding/method/range.rb
db-charmer-1.6.0 lib/db_charmer/sharding/method/range.rb