Sha256: 197e6e08d48d227446a3b837eb465dc9875be8aafddad5cf49797fda4bbd293b

Contents?: true

Size: 1.02 KB

Versions: 24

Compression:

Stored size: 1.02 KB

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

      refine ::Module do
        def deep_dup
          self
        end
      end

      using self
    end
  end
end

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
anyway_config-2.6.4 lib/anyway/ext/deep_dup.rb
anyway_config-2.6.3 lib/anyway/ext/deep_dup.rb
anyway_config-2.6.2 lib/anyway/ext/deep_dup.rb
anyway_config-2.6.1 lib/anyway/ext/deep_dup.rb
anyway_config-2.6.0 lib/anyway/ext/deep_dup.rb
runger_config-3.0.1 lib/anyway/ext/deep_dup.rb
runger_config-3.0.0 lib/anyway/ext/deep_dup.rb
runger_config-2.7.0 lib/anyway/ext/deep_dup.rb
runger_config-2.6.1 lib/anyway/ext/deep_dup.rb
runger_config-2.6.0 lib/anyway/ext/deep_dup.rb
anyway_config-2.5.4 lib/anyway/ext/deep_dup.rb
anyway_config-2.5.3 lib/anyway/ext/deep_dup.rb
anyway_config-2.5.2 lib/anyway/ext/deep_dup.rb
anyway_config-2.5.1 lib/anyway/ext/deep_dup.rb
anyway_config-2.5.0 lib/anyway/ext/deep_dup.rb
anyway_config-2.4.2 lib/anyway/ext/deep_dup.rb
anyway_config-2.4.1 lib/anyway/ext/deep_dup.rb
anyway_config-2.4.0 lib/anyway/ext/deep_dup.rb
anyway_config-2.3.1 lib/anyway/ext/deep_dup.rb
anyway_config-2.3.0 lib/anyway/ext/deep_dup.rb