Class: FPM::Cookery::InheritableAttr::DeepClone

Inherits:
Object
  • Object
show all
Defined in:
lib/fpm/cookery/inheritable_attr.rb

Overview

Provides deep cloning of data structures. Used in InheritableAttr.inherit_for to, among other things, avoid accidentally propagating to the superclass changes made to substructures of an attribute (such as arrays contained in a hash attribute).

Class Method Summary collapse

Class Method Details

.call(obj) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/fpm/cookery/inheritable_attr.rb', line 207

def self.call(obj)
  case obj
    when Hash
      obj.class[obj.map { |k, v| [DeepClone.(k), DeepClone.(v)] }]
    when Array
      obj.map { |v| DeepClone.(v) }
    when Symbol, TrueClass, FalseClass, NilClass, Integer, Float
      obj
    else
      obj.respond_to?(:clone) ? obj.clone : obj
  end
end