Class: Hash

Inherits:
Object show all
Defined in:
lib/tdriver/util/common/hash.rb

Overview

extend Ruby Hash class functionality

Direct Known Subclasses

TDriver::ParameterHash

Instance Method Summary collapse

Instance Method Details

#collect_keys(*keys) ⇒ Object

collect given keypairs from hash



113
114
115
116
117
118
119
120
# File 'lib/tdriver/util/common/hash.rb', line 113

def collect_keys( *keys )

  #Hash[ self.select{ | key, value | true if keys.include?( key ) } ]

  # optimized version, approx 47.9% faster
  keys.inject( {} ){ | hash, key | hash[ key ] = self[ key ] if has_key?( key ); hash }
  
end

#default_value(key, value) ⇒ Object

store key and value to hash if not already defined



153
154
155
156
157
158
159
# File 'lib/tdriver/util/common/hash.rb', line 153

def default_value( key, value )

  self[ key ] = value unless has_key?( key )

  self

end

#default_values(hash) ⇒ Object

store keys and values to hash if not already defined



144
145
146
147
148
149
150
# File 'lib/tdriver/util/common/hash.rb', line 144

def default_values( hash )

  hash.each_pair{ | key, value | self[ key ] = value unless has_key?( key ) }

  self

end

#delete_keys(*keys) ⇒ Object

delete multiple keys from hash, does not modify original hash



133
134
135
136
137
138
139
140
141
# File 'lib/tdriver/util/common/hash.rb', line 133

def delete_keys( *keys )

  # create a duplicate of current hash
  result = dup; keys.flatten.each{ | key | result.delete( key ) }; result

  # optimized version, approx 5% faster
  #keys.inject( dup ){ | hash, key | hash.delete( key ); hash }

end

#delete_keys!(*keys) ⇒ Object

remove keys from hash, return hash of deleted keys as result



123
124
125
126
127
128
129
130
# File 'lib/tdriver/util/common/hash.rb', line 123

def delete_keys!( *keys )

  #Hash[ keys.flatten.collect{ | key | [ key, delete( key ) ] if has_key?( key ) }.compact ]

  # optimized version, approx 23.4% faster
  keys.inject( {} ){ | hash, key | hash[ key ] = delete( key ) if has_key?( key ); hash }
  
end

#if_found(key) ⇒ Object

TODO: document me



255
256
257
258
259
260
261
262
263
# File 'lib/tdriver/util/common/hash.rb', line 255

def if_found( key )

  if has_key?( key )
  
    yield( key, self[ key ] )
  
  end

end

#not_empty(message = "Hash must not be empty", exception = ArgumentError) ⇒ Object

Raises:

  • (exception)


23
24
25
26
27
28
29
# File 'lib/tdriver/util/common/hash.rb', line 23

def not_empty( message = "Hash must not be empty", exception = ArgumentError )

  raise exception, message, caller if empty? 

  self

end

#recursive_merge(other) ⇒ Object

TODO: document me



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/tdriver/util/common/hash.rb', line 216

def recursive_merge( other )

  merge( other ){ | key, old_value, new_value |

    new_value

    if old_value.kind_of?( Hash ) && new_value.kind_of?( Hash )
    
      # merge hashes, call self recursively
      old_value.recursive_merge( new_value )

    elsif old_value.kind_of?( Array ) && new_value.kind_of?( Array )

      # concatenate arrays
      old_value.clone.concat( new_value ).uniq
    
    else

      # return new value as is
      new_value
    
    end

  }

end

#recursive_merge!(other) ⇒ Object

TODO: document me



244
245
246
247
248
249
250
251
252
# File 'lib/tdriver/util/common/hash.rb', line 244

def recursive_merge!( other )

  replace( 
  
    recursive_merge( other )
    
  )

end

#rename_key!(key, new) ⇒ Object

TODO: document me



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/tdriver/util/common/hash.rb', line 266

def rename_key!( key, new )

  if has_key?( key )
  
    self[ new ] = delete( key )
  
  else
  
    if block_given?
    
      yield( key, new )
      
    else
      
      raise IndexError, "key #{ key.inspect } not found", caller
    
    end
        
  end # has_key?

end

#require_key(key, message = 'required key $1 not found from hash') ⇒ Object

verify that receiver object contains given key. Raises exception is key not found.



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/tdriver/util/common/hash.rb', line 67

def require_key( key, message = 'required key $1 not found from hash' )

  # store caller backtrace before calling fetch
  backtrace = caller

  fetch( key ){
  
    raise ArgumentError, message.gsub( "$1", key.inspect ), backtrace
  
  }

end

#require_keys(keys, message = "Required key(s) $1 not found from hash") ⇒ Object

verify that receiver object contains all of given keys. Raises exception is key not found.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/tdriver/util/common/hash.rb', line 81

def require_keys( keys, message = "Required key(s) $1 not found from hash" )

  # create array of types
  keys_array = Array( keys )

  found = true

  verbose_keys_list = keys_array.each_with_index.collect{ | key, index | 

    found = false unless has_key?( key )

    # result string, separate types if multiple types given
    "#{ ( ( index > 0 ) ? ( index + 1 < keys_array.count ? ", " : " and " ) : "" ) }#{ key.inspect }"
        
  }

  # raise exception if type did not match
  unless found

    # convert macros
    [ verbose_keys_list.join ].each_with_index{ | param, index | message.gsub!( "$#{ index + 1 }", param.to_s ) }

    # raise the exception
    raise ArgumentError, message, caller

  end

  self

end

#require_one(keys, message = "None of key(s) $1 found from hash") ⇒ Object

verify that receiver object contains one of given keys. Raises exception is key not found.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tdriver/util/common/hash.rb', line 32

def require_one( keys, message = "None of key(s) $1 found from hash" )

  # create array of types
  keys_array = Array( keys )

  found = false

  verbose_keys_list = keys_array.each_with_index.collect{ | key, index | 

    if has_key?( key )
      found = true
      break 
    end

    # result string, separate types if multiple types given
    "#{ ( ( index > 0 ) ? ( index + 1 < keys_array.count ? ", " : " or " ) : "" ) }#{ key.inspect }"
        
  }

  # raise exception if type did not match
  unless found

    # convert macros
    [ verbose_keys_list.join ].each_with_index{ | param, index | message.gsub!( "$#{ index + 1 }", param.to_s ) }

    # raise the exception
    raise ArgumentError, message, caller

  end

  self

end

#strip_dynamic_attributes!Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/tdriver/util/common/hash.rb', line 161

def strip_dynamic_attributes!

=begin
  # remove dynamic attributes from hash and return as result     
  Hash[ 

    # iterate through each hash key
    select{ | key, value | 

      # dynamic attribute name has "__" prefix
      if key.to_s =~ /^__/ 

        # remove dynamic attribute key from hash
        delete( key )

        # add to hash
        true

      else

        # do not add to hash
        false

      end

    } 

  ]
=end

  # optimized version, approx 3.2% faster     
  prefix = '__'

  keys.inject( {} ){ | hash, key | 

    hash[ key ] = delete( key ) if key.to_s[0..1] == prefix
  
    hash
  
  }

end

#to_attributesObject

TODO: document me



205
206
207
208
209
210
211
212
213
# File 'lib/tdriver/util/common/hash.rb', line 205

def to_attributes

  collect{ | key, value |

    "#{ key.to_s }=\"#{ value.to_s.encode_to_xml }\"" 

  }.join(" ")

end