Module: HashUtils

Defined in:
lib/buzzcore/extend_base_classes.rb

Instance Method Summary collapse

Instance Method Details

#filter_exclude(aKeys, aHash = nil) ⇒ Object



302
303
304
305
# File 'lib/buzzcore/extend_base_classes.rb', line 302

def filter_exclude(aKeys,aHash=nil)
	aHash ||= self
	filter_exclude!(aKeys,aHash.clone)
end

#filter_exclude!(aKeys, aHash = nil) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
# File 'lib/buzzcore/extend_base_classes.rb', line 290

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



285
286
287
288
# File 'lib/buzzcore/extend_base_classes.rb', line 285

def filter_include(aKeys,aHash=nil)
	aHash ||= self
	filter_include!(aKeys,aHash.clone)
end

#filter_include!(aKeys, aHash = nil) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/buzzcore/extend_base_classes.rb', line 272

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

Returns:

  • (Boolean)


307
308
309
310
311
# File 'lib/buzzcore/extend_base_classes.rb', line 307

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_keysObject



333
334
335
336
337
# File 'lib/buzzcore/extend_base_classes.rb', line 333

def symbolize_keys
	result = {}
	self.each { |k,v| k.is_a?(String) ? result[k.to_sym] = v : result[k] = v  }
	return result
end

#to_nilObject



339
340
341
# File 'lib/buzzcore/extend_base_classes.rb', line 339

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



319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/buzzcore/extend_base_classes.rb', line 319

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