Strelka::

DataUtilities

module

A collection of miscellaneous functions that are useful for manipulating complex data structures.

include Strelka::DataUtilities
newhash = deep_copy( oldhash )

Public Instance Methods

anchor
autovivify( hash, key )

Create and return a Hash that will auto-vivify any values it is missing with another auto-vivifying Hash.

# File lib/strelka/mixins.rb, line 218
def autovivify( hash, key )
        hash[ key ] = Hash.new( &Strelka::DataUtilities.method(:autovivify) )
end
anchor
deep_copy( obj )

Recursively copy the specified obj and return the result.

# File lib/strelka/mixins.rb, line 189
def deep_copy( obj )

        # Handle mocks during testing
        return obj if obj.class.name == 'RSpec::Mocks::Mock'

        return case obj
                when NilClass, Numeric, TrueClass, FalseClass, Symbol,
                     Module, Encoding, IO, Tempfile
                        obj

                when Array
                        obj.map {|o| deep_copy(o) }

                when Hash
                        newhash = {}
                        newhash.default_proc = obj.default_proc if obj.default_proc
                        obj.each do |k,v|
                                newhash[ deep_copy(k) ] = deep_copy( v )
                        end
                        newhash

                else
                        obj.clone
                end
end
anchor
internify_keys( hash )
Alias for: symbolify_keys
anchor
stringify_keys( hash )

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

# File lib/strelka/mixins.rb, line 225
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
anchor
symbolify_keys( hash )

Return a duplicate of the given hash with its identifier-like keys transformed into symbols from whatever they were before.

# File lib/strelka/mixins.rb, line 242
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