Sha256: 7f1c37fe9c19ed8600b80ef07e1648911e1249965f90caff99230215a25ee773

Contents?: true

Size: 651 Bytes

Versions: 17

Compression:

Stored size: 651 Bytes

Contents

# frozen_string_literal: true

Hash.class_eval do
  # Do a deep merge, but don't override the key-value pairs on the left.
  #
  # @param other_hash [Hash]
  #
  # @return [Hash]
  #
  def left_deep_merge!(other_hash, &block)
    merge!(other_hash) do |key, this_val, other_val|
      if this_val.is_a?(Hash) && other_hash.is_a?(Hash)
        this_val.left_deep_merge(other_val, &block)
      elsif block_given?
        yield(key, this_val, other_val)
      else
        this_val.presence || other_val
      end
    end
  end

  # @see #left_deep_merge!
  def left_deep_merge(other_hash, &block)
    dup.left_deep_merge!(other_hash, &block)
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
ruby-rails-extensions-2.1.2 lib/ruby-rails-extensions/extensions/left_deep_merge.rb
ruby-rails-extensions-2.1.2.pre.pre.pre.1 lib/ruby-rails-extensions/extensions/left_deep_merge.rb
ruby-rails-extensions-2.1.1 lib/ruby-rails-extensions/extensions/left_deep_merge.rb
ruby-rails-extensions-2.1.0 lib/ruby-rails-extensions/extensions/left_deep_merge.rb
ruby-rails-extensions-2.1.0.pre.rc.14 lib/ruby-rails-extensions/extensions/left_deep_merge.rb
ruby-rails-extensions-2.1.0.pre.rc.13 lib/ruby-rails-extensions/extensions/left_deep_merge.rb
ruby-rails-extensions-2.1.0.pre.rc.12 lib/ruby-rails-extensions/extensions/left_deep_merge.rb
ruby-rails-extensions-2.1.0.pre.rc.11 lib/ruby-rails-extensions/extensions/left_deep_merge.rb
ruby-rails-extensions-2.1.0.pre.rc.10 lib/ruby-rails-extensions/extensions/left_deep_merge.rb
ruby-rails-extensions-2.1.0.pre.rc.9 lib/ruby-rails-extensions/extensions/left_deep_merge.rb
ruby-rails-extensions-2.1.0.pre.rc.8 lib/ruby-rails-extensions/extensions/left_deep_merge.rb
ruby-rails-extensions-2.1.0.pre.rc.7 lib/ruby-rails-extensions/extensions/left_deep_merge.rb
ruby-rails-extensions-2.1.0.pre.rc.6 lib/ruby-rails-extensions/extensions/left_deep_merge.rb
ruby-rails-extensions-2.1.0.pre.rc.5 lib/ruby-rails-extensions/extensions/left_deep_merge.rb
ruby-rails-extensions-2.1.0.pre.rc.4 lib/ruby-rails-extensions/extensions/left_deep_merge.rb
ruby-rails-extensions-2.1.0.pre.rc.3 lib/ruby-rails-extensions/extensions/left_deep_merge.rb
ruby-rails-extensions-2.1.0.pre.rc.2 lib/ruby-rails-extensions/extensions/left_deep_merge.rb