3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/redis_key_manager/manager.rb', line 3
def self.included(base)
def base.key(key, pattern)
class_name = self.name
placeholders = pattern.scan(/(?<=\[)[^\]]+(?=\])/).uniq.map(&:to_sym)
define_singleton_method(key) do |options = {}|
passed_option_keys = options.keys
required_option_keys = placeholders
if passed_option_keys.sort != required_option_keys.sort
raise ArgumentError, "Invalid arguments for #{class_name}.#{key}"
end
placeholders.inject(pattern) do |revised_pattern, placeholder|
value = options[placeholder]
if value.nil?
raise RedisKeyManager::InvalidKeyComponentError, "Invalid Redis key component passed to #{class_name}.#{key}"
end
revised_pattern.gsub("[#{placeholder}]", value.to_s)
end
end
end
end
|