Module: EventedSpec::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/evented-spec/util.rb

Overview

Miscellanous utility methods used throughout the code.

Instance Method Summary collapse

Instance Method Details

#deep_clone(value) ⇒ Object

Creates a deep clone of an object. Different from normal Object#clone method which is shallow clone (doesn’t traverse hashes and arrays, cloning their contents).

Parameters:

  • Object

    to clone

Returns:

  • Deep clone of the given object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/evented-spec/util.rb', line 12

def deep_clone(value)
  case value
  when Hash
    value.inject({}) do |result, kv|
      result[kv[0]] = deep_clone(kv[1])
      result
    end
  when Array
    value.inject([]) do |result, item|
      result << deep_clone(item)
    end
  else
    begin
      value.clone
    rescue TypeError
      value
    end
  end
end