Class: DbCharmer::Sharding::Method::Range
- Defined in:
- lib/db_charmer/sharding/method/range.rb
Instance Attribute Summary collapse
-
#ranges ⇒ Object
Returns the value of attribute ranges.
Instance Method Summary collapse
-
#initialize(config) ⇒ Range
constructor
A new instance of Range.
- #shard_connections ⇒ Object
- #shard_for_key(key) ⇒ Object
- #support_default_shard? ⇒ Boolean
Constructor Details
#initialize(config) ⇒ Range
Returns a new instance of Range.
7 8 9 |
# File 'lib/db_charmer/sharding/method/range.rb', line 7 def initialize(config) @ranges = config[:ranges] ? config[:ranges].clone : raise(ArgumentError, "No :ranges defined!") end |
Instance Attribute Details
#ranges ⇒ Object
Returns the value of attribute ranges.
5 6 7 |
# File 'lib/db_charmer/sharding/method/range.rb', line 5 def ranges @ranges end |
Instance Method Details
#shard_connections ⇒ Object
27 28 29 |
# File 'lib/db_charmer/sharding/method/range.rb', line 27 def shard_connections ranges.values.uniq end |
#shard_for_key(key) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/db_charmer/sharding/method/range.rb', line 11 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 |
#support_default_shard? ⇒ Boolean
23 24 25 |
# File 'lib/db_charmer/sharding/method/range.rb', line 23 def support_default_shard? ranges.has_key?(:default) end |