Module: Strelka::DataUtilities

Included in:
HTTPRequest, HTTPResponse, ParamValidator, Session::Default
Defined in:
lib/strelka/mixins.rb

Overview

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

include Strelka::DataUtilities
newhash = deep_copy( oldhash )

Class Method Summary collapse

Class Method Details

.autovivify(hash, key) ⇒ Object

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



219
220
221
# File 'lib/strelka/mixins.rb', line 219

def autovivify( hash, key )
	hash[ key ] = Hash.new( &Strelka::DataUtilities.method(:autovivify) )
end

.deep_copy(obj) ⇒ Object

Recursively copy the specified obj and return the result.



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/strelka/mixins.rb', line 190

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

.stringify_keys(hash) ⇒ Object

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



226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/strelka/mixins.rb', line 226

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.



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/strelka/mixins.rb', line 243

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