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
154 def autovivify( hash, key )
155     hash[ key ] = Hash.new( &Chione::DataUtilities.method(:autovivify) )
156 end
deep_copy( obj )

Recursively copy the specified obj and return the result.

    # File lib/chione/mixins.rb
125 def deep_copy( obj )
126 
127     # Handle mocks during testing
128     return obj if obj.class.name == 'RSpec::Mocks::Mock'
129 
130     return case obj
131         when NilClass, Numeric, TrueClass, FalseClass, Symbol,
132              Module, Encoding, IO, Tempfile
133             obj
134 
135         when Array
136             obj.map {|o| deep_copy(o) }
137 
138         when Hash
139             newhash = {}
140             newhash.default_proc = obj.default_proc if obj.default_proc
141             obj.each do |k,v|
142                 newhash[ deep_copy(k) ] = deep_copy( v )
143             end
144             newhash
145 
146         else
147             obj.clone
148         end
149 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
161 def stringify_keys( hash )
162     newhash = {}
163 
164     hash.each do |key,val|
165         if val.is_a?( Hash )
166             newhash[ key.to_s ] = stringify_keys( val )
167         else
168             newhash[ key.to_s ] = val
169         end
170     end
171 
172     return newhash
173 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
178 def symbolify_keys( hash )
179     newhash = {}
180 
181     hash.each do |key,val|
182         keysym = key.to_s.dup.to_sym
183 
184         if val.is_a?( Hash )
185             newhash[ keysym ] = symbolify_keys( val )
186         else
187             newhash[ keysym ] = val
188         end
189     end
190 
191     return newhash
192 end
Also aliased as: internify_keys