Module: Operandi::Utils

Defined in:
lib/operandi/utils.rb

Overview

Utility module providing helper methods for the Operandi library

Class Method Summary collapse

Class Method Details

.deep_dup(object) ⇒ Object

Creates a deep copy of an object to prevent mutation of shared references.

Examples:

Deep duping a hash

original = { a: { b: 1 } }
copy = Utils.deep_dup(original)
copy[:a][:b] = 2
original[:a][:b] # => 1

Deep duping an array

original = [[1, 2], [3, 4]]
copy = Utils.deep_dup(original)
copy[0] << 5
original[0] # => [1, 2]

Parameters:

  • object (Object)

    the object to duplicate

Returns:

  • (Object)

    a deep copy of the object



25
26
27
28
29
30
31
32
33
34
# File 'lib/operandi/utils.rb', line 25

def deep_dup(object)
  # Use ActiveSupport's deep_dup if available (preferred for Rails apps)
  return object.deep_dup if object.respond_to?(:deep_dup)

  # Fallback to Marshal for objects that support serialization
  Marshal.load(Marshal.dump(object))
rescue TypeError
  # Last resort: use dup if available, otherwise return original
  object.respond_to?(:dup) ? object.dup : object
end