Sha256: ca05212807fc5cbfb5b5759e545f88611cf420a1b8101b1f7605be7acf3e863f

Contents?: true

Size: 1.09 KB

Versions: 13

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

class Serega
  #
  # Utilities
  #
  module SeregaUtils
    #
    # Duplicates nested hashes and arrays
    # It does not duplicate any non-Array and non-Hash values
    #
    class EnumDeepDup
      class << self
        #
        # Deeply duplicate provided Array or Hash data
        # It does not duplicate any non-Array and non-Hash values
        #
        # @param data [Hash, Array] Data to duplicate
        #
        # @return [Hash, Array] Duplicated data
        #
        def call(data)
          case data
          when Hash
            # https://github.com/fastruby/fast-ruby#hash-vs-hashdup-code
            data = Hash[data] # rubocop:disable Style/HashConversion
            dup_hash_values(data)
          when Array
            data = data.dup
            dup_array_values(data)
          end

          data
        end

        private

        def dup_hash_values(data)
          data.transform_values! { |value| call(value) }
        end

        def dup_array_values(data)
          data.map! { |value| call(value) }
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
serega-0.21.0 lib/serega/utils/enum_deep_dup.rb
serega-0.20.1 lib/serega/utils/enum_deep_dup.rb
serega-0.20.0 lib/serega/utils/enum_deep_dup.rb
serega-0.19.0 lib/serega/utils/enum_deep_dup.rb
serega-0.18.0 lib/serega/utils/enum_deep_dup.rb
serega-0.17.0 lib/serega/utils/enum_deep_dup.rb
serega-0.16.0 lib/serega/utils/enum_deep_dup.rb
serega-0.15.0 lib/serega/utils/enum_deep_dup.rb
serega-0.14.0 lib/serega/utils/enum_deep_dup.rb
serega-0.12.0 lib/serega/utils/enum_deep_dup.rb
serega-0.11.2 lib/serega/utils/enum_deep_dup.rb
serega-0.11.1 lib/serega/utils/enum_deep_dup.rb
serega-0.11.0 lib/serega/utils/enum_deep_dup.rb