Chione::

DataUtilities module

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

include Chione::DataUtilities newhash = deep_copy( oldhash )

Public Instance Methods

autovivify( hash, key )

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

    # File lib/chione/mixins.rb
155 def autovivify( hash, key )
156     hash[ key ] = Hash.new( &Chione::DataUtilities.method(:autovivify) )
157 end
deep_copy( obj )

Recursively copy the specified obj and return the result.

    # File lib/chione/mixins.rb
126 def deep_copy( obj )
127 
128     # Handle mocks during testing
129     return obj if obj.class.name == 'RSpec::Mocks::Mock'
130 
131     return case obj
132         when NilClass, Numeric, TrueClass, FalseClass, Symbol,
133              Module, Encoding, IO, Tempfile
134             obj
135 
136         when Array
137             obj.map {|o| deep_copy(o) }
138 
139         when Hash
140             newhash = {}
141             newhash.default_proc = obj.default_proc if obj.default_proc
142             obj.each do |k,v|
143                 newhash[ deep_copy(k) ] = deep_copy( v )
144             end
145             newhash
146 
147         else
148             obj.clone
149         end
150 end
internify_keys( hash )
Alias for: symbolify_keys
stringify_keys( hash )

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

    # File lib/chione/mixins.rb
162 def stringify_keys( hash )
163     newhash = {}
164 
165     hash.each do |key,val|
166         if val.is_a?( Hash )
167             newhash[ key.to_s ] = stringify_keys( val )
168         else
169             newhash[ key.to_s ] = val
170         end
171     end
172 
173     return newhash
174 end
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/chione/mixins.rb
179 def symbolify_keys( hash )
180     newhash = {}
181 
182     hash.each do |key,val|
183         keysym = key.to_s.dup.to_sym
184 
185         if val.is_a?( Hash )
186             newhash[ keysym ] = symbolify_keys( val )
187         else
188             newhash[ keysym ] = val
189         end
190     end
191 
192     return newhash
193 end
Also aliased as: internify_keys