module Treequel::HashUtilities
A collection of utilities for working with Hashes.
Public Instance Methods
merge_recursively( key, oldval, newval )
click to toggle source
Recursive hash-merge function
# File lib/treequel/mixins.rb, line 206 def merge_recursively( key, oldval, newval ) case oldval when Hash case newval when Hash oldval.merge( newval, &method(:merge_recursively) ) else newval end when Array case newval when Array oldval | newval else newval end else newval end end
normalize_attributes( hash )
click to toggle source
Normalize the attributes in hash
to be of the form expected by the LDAP
library (i.e., keys as Strings, values as Arrays of Strings)
# File lib/treequel/mixins.rb, line 231 def normalize_attributes( hash ) normhash = {} hash.each do |key,val| val = [ val ] unless val.is_a?( Array ) val.collect! {|obj| obj.to_s } normhash[ key.to_s ] = val end normhash.delete( 'dn' ) return normhash end
stringify_keys( hash )
click to toggle source
Return a version of the given hash
with its keys transformed into Strings from whatever they were before.
# File lib/treequel/mixins.rb, line 170 def stringify_keys( hash ) newhash = {} hash.each do |key,val| if val.is_a?( Hash ) newhash[ key.to_s ] = stringify_keys( val ) else newhash[ key.to_s ] = val end end return newhash end
symbolify_keys( hash )
click to toggle source
Return a duplicate of the given hash
with its identifier-like keys transformed into symbols from whatever they were before.
# File lib/treequel/mixins.rb, line 187 def symbolify_keys( hash ) newhash = {} hash.each do |key,val| keysym = key.to_s.dup.untaint.to_sym if val.is_a?( Hash ) newhash[ keysym ] = symbolify_keys( val ) else newhash[ keysym ] = val end end return newhash end
Also aliased as: internify_keys