Module: Inversion::DataUtilities

Included in:
RenderState, Template
Defined in:
lib/inversion/mixins.rb

Overview

A collection of data-manipulation functions.

Class Method Summary collapse

Class Method Details

.deep_copy(obj) ⇒ Object

Recursively copy the specified obj and return the result.



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
260
# File 'lib/inversion/mixins.rb', line 235

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