Module: HashUtils
- Defined in:
- lib/buzzcore/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
-
#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
198 199 200 201 |
# File 'lib/buzzcore/extend_base_classes.rb', line 198 def filter_exclude(aKeys,aHash=nil) aHash ||= self filter_exclude!(aKeys,aHash.clone) end |
#filter_exclude!(aKeys, aHash = nil) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/buzzcore/extend_base_classes.rb', line 186 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
181 182 183 184 |
# File 'lib/buzzcore/extend_base_classes.rb', line 181 def filter_include(aKeys,aHash=nil) aHash ||= self filter_include!(aKeys,aHash.clone) end |
#filter_include!(aKeys, aHash = nil) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/buzzcore/extend_base_classes.rb', line 168 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
203 204 205 206 207 |
# File 'lib/buzzcore/extend_base_classes.rb', line 203 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
229 230 231 232 233 |
# File 'lib/buzzcore/extend_base_classes.rb', line 229 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
215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/buzzcore/extend_base_classes.rb', line 215 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 |