Module: HashUtils
- Defined in:
- lib/buzzcorej/extend_base_classes.rb
Instance Method Summary collapse
- #filter_exclude(aKeys, aHash = nil) ⇒ Object
- #filter_exclude!(aKeys, aHash = nil) ⇒ Object
- #filter_include(aKeys, aHash = nil) ⇒ Object
- #filter_include!(aKeys, aHash = nil) ⇒ Object
- #has_values_for?(aKeys, aHash = nil) ⇒ Boolean
- #symbolize_keys ⇒ Object
- #to_nil ⇒ Object
-
#without_key(aKey) ⇒ Object
give a block to execute without the given key in this hash It will be replaced after the block (guaranteed by ensure) eg.
Instance Method Details
#filter_exclude(aKeys, aHash = nil) ⇒ Object
282 283 284 285 |
# File 'lib/buzzcorej/extend_base_classes.rb', line 282 def filter_exclude(aKeys,aHash=nil) aHash ||= self filter_exclude!(aKeys,aHash.clone) end |
#filter_exclude!(aKeys, aHash = nil) ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/buzzcorej/extend_base_classes.rb', line 270 def filter_exclude!(aKeys,aHash=nil) aHash ||= self if aKeys.is_a? Regexp return aHash.delete_if {|k,v| k =~ aKeys } else aKeys = [aKeys] unless aKeys.is_a? Array return aHash if aKeys.empty? return aHash.delete_if {|key, value| ((aKeys.include?(key)) || (key.is_a?(Symbol) and aKeys.include?(key.to_s)) || (key.is_a?(String) and aKeys.include?(key.to_sym)))} end end |
#filter_include(aKeys, aHash = nil) ⇒ Object
265 266 267 268 |
# File 'lib/buzzcorej/extend_base_classes.rb', line 265 def filter_include(aKeys,aHash=nil) aHash ||= self filter_include!(aKeys,aHash.clone) end |
#filter_include!(aKeys, aHash = nil) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/buzzcorej/extend_base_classes.rb', line 252 def filter_include!(aKeys,aHash=nil) aHash ||= self if aKeys.is_a? Regexp return aHash.delete_if {|k,v| not k =~ aKeys } else aKeys = [aKeys] unless aKeys.is_a? Array return aHash.clear if aKeys.empty? return aHash.delete_if {|key, value| !((aKeys.include?(key)) || (key.is_a?(Symbol) and aKeys.include?(key.to_s)) || (key.is_a?(String) and aKeys.include?(key.to_sym)))} return aHash # last resort end end |
#has_values_for?(aKeys, aHash = nil) ⇒ Boolean
287 288 289 290 291 |
# File 'lib/buzzcorej/extend_base_classes.rb', line 287 def has_values_for?(aKeys,aHash=nil) aHash ||= self # check all keys exist in aHash and their values are not nil aKeys.all? { |k,v| aHash[k] } end |
#symbolize_keys ⇒ Object
313 314 315 316 317 |
# File 'lib/buzzcorej/extend_base_classes.rb', line 313 def symbolize_keys result = {} self.each { |k,v| k.is_a?(String) ? result[k.to_sym] = v : result[k] = v } return result end |
#to_nil ⇒ Object
319 320 321 |
# File 'lib/buzzcorej/extend_base_classes.rb', line 319 def to_nil self.empty? ? nil : self end |
#without_key(aKey) ⇒ Object
give a block to execute without the given key in this hash It will be replaced after the block (guaranteed by ensure) eg. hash.without_key(:blah) do |aHash| puts aHash.inspect end
299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/buzzcorej/extend_base_classes.rb', line 299 def without_key(aKey) temp = nil h = self begin if h.include?(aKey) temp = [aKey,h.delete(aKey)] end result = yield(h) ensure h[temp[0]] = temp[1] if temp end return result end |