Method: Inversion::DataUtilities.deep_copy

Defined in:
lib/inversion/mixins.rb

.deep_copy(obj) ⇒ Object

Recursively copy the specified ‘obj` and return the result.



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/inversion/mixins.rb', line 234

def deep_copy( obj )
  # self.log.debug "Deep copying: %p" % [ 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