Class: Pandoru::ClientBuilders::TranslatingHash
- Inherits:
-
Hash
- Object
- Hash
- Pandoru::ClientBuilders::TranslatingHash
show all
- Defined in:
- lib/pandoru/client_builder.rb
Overview
Abstract Key/Value Translating Hash
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of TranslatingHash.
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/pandoru/client_builder.rb', line 28
def initialize(initial = nil)
super()
if initial
if initial.respond_to?(:each_pair)
initial.each_pair { |k, v| put(k, v) }
else
initial.each { |k, v| put(k, v) }
end
end
end
|
Class Method Details
.key_translations ⇒ Object
12
13
14
|
# File 'lib/pandoru/client_builder.rb', line 12
def self.key_translations
@key_translations ||= {}
end
|
.translate_key(key, value_translations) ⇒ Object
20
21
22
|
# File 'lib/pandoru/client_builder.rb', line 20
def self.translate_key(key, value_translations)
@key_translations = key_translations
end
|
.translate_value(value_translations) ⇒ Object
24
25
26
|
# File 'lib/pandoru/client_builder.rb', line 24
def self.translate_value(value_translations)
@value_translations = value_translations
end
|
.value_translations ⇒ Object
16
17
18
|
# File 'lib/pandoru/client_builder.rb', line 16
def self.value_translations
@value_translations ||= {}
end
|
Instance Method Details
#[]=(key, value) ⇒ Object
68
69
70
|
# File 'lib/pandoru/client_builder.rb', line 68
def []=(key, value)
put(key, value)
end
|
#put(key, value) ⇒ Object
62
63
64
65
66
|
# File 'lib/pandoru/client_builder.rb', line 62
def put(key, value)
translated_key = translate_key(key)
translated_value = translate_value(translated_key, value)
store(translated_key, translated_value)
end
|
#translate_key(key) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/pandoru/client_builder.rb', line 44
def translate_key(key)
key_str = key.to_s.strip.upcase
to_key = self.class.key_translations[key_str]
if to_key
was_translated(key_str, to_key)
to_key
else
key_str
end
end
|
#translate_value(key, value) ⇒ Object
56
57
58
59
60
|
# File 'lib/pandoru/client_builder.rb', line 56
def translate_value(key, value)
value = value.strip if value.respond_to?(:strip)
translator = self.class.value_translations[key]
translator ? translator.call(value) : value
end
|
#was_translated(from_key, to_key) ⇒ Object
40
41
42
|
# File 'lib/pandoru/client_builder.rb', line 40
def was_translated(from_key, to_key)
end
|