Module: Arrow::HashUtilities

Included in:
Config, Config::ConfigStruct, Template, Template::Parser
Defined in:
lib/arrow/mixins.rb

Overview

A collection of utilities for working with Hashes.

Constant Summary collapse

HashMergeFunction =

Recursive hash-merge function

Proc.new {|key, oldval, newval|
  #debugMsg "Merging '%s': %s -> %s" %
  #  [ key.inspect, oldval.inspect, newval.inspect ]
  case oldval
  when Hash
    case newval
    when Hash
      #debugMsg "Hash/Hash merge"
      oldval.merge( newval, &HashMergeFunction )
    else
      newval
    end

  when Array
    case newval
    when Array
      #debugMsg "Array/Array union"
      oldval | newval
    else
      newval
    end

  when Arrow::Path
    if newval.is_a?( Arrow::Path )
      newval
    else
      Arrow::Path.new( newval )
    end

  else
    newval
  end
}

Class Method Summary collapse

Class Method Details

.stringify_keys(hash) ⇒ Object

Return a version of the given hash with its keys transformed into Strings from whatever they were before.



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/arrow/mixins.rb', line 50

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.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/arrow/mixins.rb', line 67

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