Module: Arborist::HashUtilities
- Included in:
- Event::NodeDelta, Manager, Node, Node::Ack, Node::Service, Subscription, TreeAPI
- Defined in:
- lib/arborist/mixins.rb
Overview
A collection of utilities for working with Hashes.
Class Method Summary collapse
-
.compact_hash(hash) ⇒ Object
Recursively remove hash pairs in place whose value is nil.
-
.hash_matches(hash, key, val) ⇒ Object
Returns true if the specified
hash
includes the specifiedkey
, and the value associated with thekey
either includesval
if it is a Hash, or equalsval
if it’s anything but a Hash. -
.merge_recursively(key, oldval, newval) ⇒ Object
Recursive hash-merge function.
-
.stringify_keys(hash) ⇒ Object
Return a version of the given
hash
with its keys transformed into Strings from whatever they were before. -
.symbolify_keys(hash) ⇒ Object
(also: internify_keys)
Return a duplicate of the given
hash
with its identifier-like keys transformed into symbols from whatever they were before.
Class Method Details
.compact_hash(hash) ⇒ Object
Recursively remove hash pairs in place whose value is nil.
366 367 368 369 370 371 |
# File 'lib/arborist/mixins.rb', line 366 def compact_hash( hash ) hash.each_key do |k| hash.delete( k ) if hash[ k ].nil? compact_hash( hash[k] ) if hash[k].is_a?( Hash ) end end |
.hash_matches(hash, key, val) ⇒ Object
Returns true if the specified hash
includes the specified key
, and the value associated with the key
either includes val
if it is a Hash, or equals val
if it’s anything but a Hash.
377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/arborist/mixins.rb', line 377 def hash_matches( hash, key, val ) actual = hash[ key ] or return false if actual.is_a?( Hash ) if val.is_a?( Hash ) return val.all? {|subkey, subval| hash_matches(actual, subkey, subval) } else return false end else return actual == val end end |
.merge_recursively(key, oldval, newval) ⇒ Object
Recursive hash-merge function
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/arborist/mixins.rb', line 341 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 |
.stringify_keys(hash) ⇒ Object
Return a version of the given hash
with its keys transformed into Strings from whatever they were before.
305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/arborist/mixins.rb', line 305 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.
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/arborist/mixins.rb', line 322 def symbolify_keys( hash ) newhash = {} hash.each do |key,val| keysym = key.to_s.dup.to_sym if val.is_a?( Hash ) newhash[ keysym ] = symbolify_keys( val ) else newhash[ keysym ] = val end end return newhash end |