Module: HashUtils
- Defined in:
- lib/buzztools/extend_hash.rb
Instance Method Summary collapse
- #compact ⇒ Object
- #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
-
#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
#compact ⇒ Object
72 73 74 |
# File 'lib/buzztools/extend_hash.rb', line 72 def compact self.reject {|k,v| v==nil} end |
#filter_exclude(aKeys, aHash = nil) ⇒ Object
35 36 37 38 |
# File 'lib/buzztools/extend_hash.rb', line 35 def filter_exclude(aKeys,aHash=nil) aHash ||= self filter_exclude!(aKeys,aHash.dup) end |
#filter_exclude!(aKeys, aHash = nil) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/buzztools/extend_hash.rb', line 23 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
18 19 20 21 |
# File 'lib/buzztools/extend_hash.rb', line 18 def filter_include(aKeys,aHash=nil) aHash ||= self filter_include!(aKeys,aHash.dup) end |
#filter_include!(aKeys, aHash = nil) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/buzztools/extend_hash.rb', line 5 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
40 41 42 43 44 |
# File 'lib/buzztools/extend_hash.rb', line 40 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
66 67 68 69 70 |
# File 'lib/buzztools/extend_hash.rb', line 66 def symbolize_keys result = {} self.each { |k,v| k.is_a?(String) ? result[k.to_sym] = v : result[k] = v } return result 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
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/buzztools/extend_hash.rb', line 52 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 |