Sha256: 0c1409e33d461fd89182f33b095e64afeb9d59ff90e46b0b6ecaced50d39b3c5

Contents?: true

Size: 962 Bytes

Versions: 1

Compression:

Stored size: 962 Bytes

Contents

# frozen_string_literal: true

module Anyway
  module Ext
    # Extend Object through refinements
    module DeepDup
      refine ::Hash do
        # Based on ActiveSupport http://api.rubyonrails.org/classes/Hash.html#method-i-deep_dup
        def deep_dup
          each_with_object(dup) do |(key, value), hash|
            hash[key] = if value.is_a?(::Hash) || value.is_a?(::Array)
              value.deep_dup
            else
              value
            end
          end
        end
      end

      refine ::Array do
        # From ActiveSupport http://api.rubyonrails.org/classes/Array.html#method-i-deep_dup
        def deep_dup
          map do |value|
            if value.is_a?(::Hash) || value.is_a?(::Array)
              value.deep_dup
            else
              value
            end
          end
        end
      end

      refine ::Object do
        def deep_dup
          dup
        end
      end

      using self
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
anyway_config-2.1.0 lib/anyway/ext/deep_dup.rb