Module: Treequel::HashUtilities
- Included in:
- Branch, Directory, Model::Errors, Model::ObjectClass
- Defined in:
- lib/treequel/mixins.rb
Overview
A collection of utilities for working with Hashes.
Class Method Summary collapse
-
.merge_recursively(key, oldval, newval) ⇒ Object
Recursive hash-merge function.
-
.normalize_attributes(hash) ⇒ Object
Normalize the attributes in
hashto be of the form expected by the LDAP library (i.e., keys as Strings, values as Arrays of Strings). -
.stringify_keys(hash) ⇒ Object
Return a version of the given
hashwith its keys transformed into Strings from whatever they were before. -
.symbolify_keys(hash) ⇒ Object
(also: internify_keys)
Return a duplicate of the given
hashwith its identifier-like keys transformed into symbols from whatever they were before.
Class Method Details
.merge_recursively(key, oldval, newval) ⇒ Object
Recursive hash-merge function
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/treequel/mixins.rb', line 207 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) ⇒ Object
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)
232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/treequel/mixins.rb', line 232 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) ⇒ Object
Return a version of the given hash with its keys transformed into Strings from whatever they were before.
171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/treequel/mixins.rb', line 171 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) ⇒ Object Also known as: internify_keys
Return a duplicate of the given hash with its identifier-like keys transformed into symbols from whatever they were before.
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/treequel/mixins.rb', line 188 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 |