Sha256: b9557b345b144463f9f3147eccddd6bc9c9e30d3aed57b653dc7ce8c9496154c

Contents?: true

Size: 826 Bytes

Versions: 4

Compression:

Stored size: 826 Bytes

Contents

module EventedSpec
  # Miscellanous utility methods used throughout the code.
  module Util
    extend self

    # 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).
    #
    # @param Object to clone
    # @return Deep clone of the given object
    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 # deep_clone
  end # module Util
end # module EventedSpec

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
evented-spec-1.0.0.beta1 lib/evented-spec/util.rb
evented-spec-0.9.0 lib/evented-spec/util.rb
evented-spec-0.4.1 lib/evented-spec/util.rb
evented-spec-0.4.0 lib/evented-spec/util.rb