Class: UU::OS::Lang::UniformHash
- Inherits:
-
Hash
- Object
- Hash
- UU::OS::Lang::UniformHash
show all
- Defined in:
- uu_os_commons-1.5.0/lib/uu/os/lang/uniform_hash.rb
Overview
Implementation of Hash providing methods for unification of stored keys
and values on both, persistence level (all values are stored in same form)
and API level (any user input is transformed to match internal
representation). By default, all keys are translated to symbols, values are
left without any change.
Instance Method Summary
(collapse)
Constructor Details
- (UniformHash) initialize(obj = nil)
Returns a new instance of UniformHash
17
18
19
|
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/uniform_hash.rb', line 17
def initialize(obj = nil)
super(obj)
end
|
Instance Method Details
- (Object) [](key)
22
23
24
|
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/uniform_hash.rb', line 22
def [](key)
super(transform_key(key))
end
|
- (Object) []=(key, value)
27
28
29
|
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/uniform_hash.rb', line 27
def []=(key, value)
super(transform_key(key), transform_value(value))
end
|
- (Object) default(key = nil)
32
33
34
|
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/uniform_hash.rb', line 32
def default(key = nil)
super(transform_key(key))
end
|
- (Object) delete(key, &block)
37
38
39
|
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/uniform_hash.rb', line 37
def delete(key, &block)
super(transform_key(key), &block)
end
|
- (Object) fetch(key, default = nil, &block)
42
43
44
|
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/uniform_hash.rb', line 42
def fetch(key, default = nil, &block)
super(transform_key(key), default, &block)
end
|
- (Boolean) has_key?(key)
47
48
49
|
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/uniform_hash.rb', line 47
def has_key?(key)
super(transform_key(key))
end
|
- (Boolean) has_value?(value)
52
53
54
|
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/uniform_hash.rb', line 52
def has_value?(value)
super(transform_value(value))
end
|
- (Boolean) include?(key)
57
58
59
|
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/uniform_hash.rb', line 57
def include?(key)
super(transform_key(key))
end
|
- (Object) key(value)
67
68
69
|
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/uniform_hash.rb', line 67
def key(value)
super(transform_value(value))
end
|
- (Boolean) key?(key)
62
63
64
|
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/uniform_hash.rb', line 62
def key?(key)
super(transform_key(key))
end
|
- (Boolean) member?(key)
72
73
74
|
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/uniform_hash.rb', line 72
def member?(key)
super(transform_key(key))
end
|
- (Object) merge(other_hash, &block)
77
78
79
80
81
82
|
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/uniform_hash.rb', line 77
def merge(other_hash, &block)
result = self.class.new
result.replace(self)
result.merge!(other_hash, &block) if other_hash
return result
end
|
- (Object) merge!(other_hash, &block)
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/uniform_hash.rb', line 85
def merge!(other_hash, &block)
if other_hash
raise TypeError.new("no implicit conversion of #{other_hash.class} into Hash") if !other_hash.respond_to?:each
other_hash.each do |key, value|
tkey = transform_key(key)
if !_has_key?(tkey) || block.nil?
_put(tkey, transform_value(value))
else
_put(tkey, transform_value(block.yield(tkey, _get(tkey), value)))
end
end
end
end
|
- (Object) replace(other_hash)
100
101
102
103
104
105
106
107
108
109
110
|
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/uniform_hash.rb', line 100
def replace(other_hash)
if other_hash
raise TypeError.new("no implicit conversion of #{other_hash.class} into Hash") if !other_hash.respond_to?:each
clear
other_hash.each do |key, value|
self[key] = value
end
else
clear
end
end
|
- (Object) store(key, value)
113
114
115
|
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/uniform_hash.rb', line 113
def store(key, value)
super(transform_key(key), transform_value(value))
end
|
Note:
Not intended to be directly used, but for overriding of default behavior by
custom implementation!
Transforms given key to desired form.
145
146
147
148
|
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/uniform_hash.rb', line 145
def transform_key(key)
return nil if key.nil?
return key.to_sym
end
|
Note:
Not intended to be directly used, but for overriding of default behavior by
custom implementation!
Transforms given value to desired form.
155
156
157
|
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/uniform_hash.rb', line 155
def transform_value(value)
return value
end
|
- (Object) update(other_hash, &block)
118
119
120
|
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/uniform_hash.rb', line 118
def update(other_hash, &block)
merge!(other_hash, &block) if other_hash
end
|
- (Boolean) value?(value)
123
124
125
|
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/uniform_hash.rb', line 123
def value?(value)
super(transform_value(value))
end
|
- (Object) values_at(*key)
128
129
130
131
132
133
134
135
136
137
138
|
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/uniform_hash.rb', line 128
def values_at(*key)
if (key.size == 0)
super
else
keys = []
key.each do |k|
keys << transform_key(k)
end
super(keys)
end
end
|