Class: UU::OS::Lang::UniformHash

Inherits:
Hash
  • Object
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.

Direct Known Subclasses

HTTP::HTTPHeaders, HTTP::HTTPQuery, VUC::VisualUseCaseContext::ParametersDecorator

Instance Method Summary (collapse)

Constructor Details

- (UniformHash) initialize(obj = nil)

Returns a new instance of UniformHash

See Also:

  • Hash#initialize


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)

See Also:

  • Hash#[]


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)

See Also:

  • Hash#[]=


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)

See Also:

  • Hash#default


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)

See Also:

  • Hash#delete


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)

See Also:

  • Hash#fetch


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)

Returns:

  • (Boolean)

See Also:

  • Hash#has_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)

Returns:

  • (Boolean)

See Also:

  • Hash#has_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)

Returns:

  • (Boolean)

See Also:

  • Hash#include?


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)

See Also:

  • Hash#key


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)

Returns:

  • (Boolean)

See Also:

  • Hash#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)

Returns:

  • (Boolean)

See Also:

  • Hash#member?


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)

See Also:

  • Hash#merge


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)

See Also:

  • Hash#merge!


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)

See Also:

  • Hash#replace


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)

See Also:

  • Hash#store


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

- (Object) transform_key(key)

Note:

Not intended to be directly used, but for overriding of default behavior by custom implementation!

Transforms given key to desired form.

Parameters:

  • key (Object)

    Key to be transformed.

Returns:

  • (Object)

    Transformed key.



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

- (Object) transform_value(value)

Note:

Not intended to be directly used, but for overriding of default behavior by custom implementation!

Transforms given value to desired form.

Parameters:

  • value (Object)

    Value to be transformed.

Returns:

  • (Object)

    Transformed value.



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)

See Also:

  • Hash#update


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)

Returns:

  • (Boolean)

See Also:

  • Hash#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)

See Also:

  • Hash#values_at


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