Class: DbCharmer::Sharding::Method::HashMap

Inherits:
Object
  • Object
show all
Defined in:
lib/db_charmer/sharding/method/hash_map.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ HashMap

Returns a new instance of HashMap.



7
8
9
# File 'lib/db_charmer/sharding/method/hash_map.rb', line 7

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

Instance Attribute Details

#mapObject

Returns the value of attribute map.



5
6
7
# File 'lib/db_charmer/sharding/method/hash_map.rb', line 5

def map
  @map
end

Instance Method Details

#shard_for_key(key) ⇒ Object

Raises:

  • (ArgumentError)


11
12
13
14
15
# File 'lib/db_charmer/sharding/method/hash_map.rb', line 11

def shard_for_key(key)
  res = map[key] || map[:default]
  raise ArgumentError, "Invalid key value, no shards found for this key!" unless res
  return res
end

#support_default_shard?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/db_charmer/sharding/method/hash_map.rb', line 17

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