Class: Jat::EnumDeepDup

Inherits:
Object
  • Object
show all
Defined in:
lib/jat/utils/enum_deep_dup.rb

Overview

Duplicates nested enumerable data

Constant Summary collapse

DUP =
{
  Hash => ->(data) { dup_hash_values(data) },
  Array => ->(data) { dup_array_values(data) }
}.freeze

Class Method Summary collapse

Class Method Details

.call(data) ⇒ Object



11
12
13
14
15
# File 'lib/jat/utils/enum_deep_dup.rb', line 11

def self.call(data)
  duplicate_data = data.dup
  DUP.fetch(duplicate_data.class).call(duplicate_data)
  duplicate_data
end

.dup_array_values(duplicate_data) ⇒ Object



23
24
25
26
27
# File 'lib/jat/utils/enum_deep_dup.rb', line 23

def self.dup_array_values(duplicate_data)
  duplicate_data.each_with_index do |value, index|
    duplicate_data[index] = call(value) if value.is_a?(Enumerable)
  end
end

.dup_hash_values(duplicate_data) ⇒ Object



17
18
19
20
21
# File 'lib/jat/utils/enum_deep_dup.rb', line 17

def self.dup_hash_values(duplicate_data)
  duplicate_data.each do |key, value|
    duplicate_data[key] = call(value) if value.is_a?(Enumerable)
  end
end