Sha256: a28ec600eaa5f734ad891607c79f551c53b5538edca7c22105fdf9d5162367c9

Contents?: true

Size: 948 Bytes

Versions: 6

Compression:

Stored size: 948 Bytes

Contents

# frozen_string_literal: true

module WSDirector
  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
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
wsdirector-core-1.1.0 lib/wsdirector/ext/deep_dup.rb
wsdirector-core-1.0.3 lib/wsdirector/ext/deep_dup.rb
wsdirector-core-1.0.2 lib/wsdirector/ext/deep_dup.rb
wsdirector-core-1.0.1 lib/wsdirector/ext/deep_dup.rb
wsdirector-core-1.0.0 lib/wsdirector/ext/deep_dup.rb
wsdirector-cli-0.5.0 lib/wsdirector/ext/deep_dup.rb