Sha256: 87c50aac947acf0ab783b10887515e42aa713c2d9384f5103c6f4a7b5de8be64

Contents?: true

Size: 1.47 KB

Versions: 15

Compression:

Stored size: 1.47 KB

Contents

module I18n
  module HashRefinements
    refine Hash do
      using I18n::HashRefinements
      def except(*keys)
        dup.except!(*keys)
      end unless method_defined?(:except)

      def except!(*keys)
        keys.each { |key| delete(key) }
        self
      end

      def deep_symbolize_keys
        each_with_object({}) do |(key, value), result|
          result[symbolize_key(key)] = deep_symbolize_keys_in_object(value)
          result
        end
      end

      # deep_merge from activesupport 5
      # Copyright (c) 2005-2019 David Heinemeier Hansson
      def deep_merge(other_hash, &block)
        dup.deep_merge!(other_hash, &block)
      end

      # deep_merge! from activesupport 5
      # Copyright (c) 2005-2019 David Heinemeier Hansson
      def deep_merge!(other_hash, &block)
        merge!(other_hash) do |key, this_val, other_val|
          if this_val.is_a?(Hash) && other_val.is_a?(Hash)
            this_val.deep_merge(other_val, &block)
          elsif block_given?
            block.call(key, this_val, other_val)
          else
            other_val
          end
        end
      end

      def symbolize_key(key)
        key.respond_to?(:to_sym) ? key.to_sym : key
      end

      private

      def deep_symbolize_keys_in_object(value)
        case value
        when Hash
          value.deep_symbolize_keys
        when Array
          value.map { |e| deep_symbolize_keys_in_object(e) }
        else
          value
        end
      end
    end
  end
end

Version data entries

15 entries across 14 versions & 7 rubygems

Version Path
ric-0.14.2 vendor/bundle/ruby/2.7.0/gems/i18n-1.8.11/lib/i18n/core_ext/hash.rb
ric-0.14.1 vendor/bundle/ruby/2.7.0/gems/i18n-1.8.11/lib/i18n/core_ext/hash.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/i18n-1.8.11/lib/i18n/core_ext/hash.rb
ric-0.14.0 vendor/bundle/ruby/2.7.0/gems/i18n-1.8.11/lib/i18n/core_ext/hash.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/3.0.0/gems/i18n-1.8.10/lib/i18n/core_ext/hash.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/3.0.0/gems/i18n-1.8.11/lib/i18n/core_ext/hash.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/i18n-1.8.11/lib/i18n/core_ext/hash.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/i18n-1.8.11/lib/i18n/core_ext/hash.rb
i18n-1.8.11 lib/i18n/core_ext/hash.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/3.0.0/gems/i18n-1.8.10/lib/i18n/core_ext/hash.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/i18n-1.8.10/lib/i18n/core_ext/hash.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/i18n-1.8.10/lib/i18n/core_ext/hash.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/3.0.0/gems/i18n-1.8.10/lib/i18n/core_ext/hash.rb
i18n-1.8.10 lib/i18n/core_ext/hash.rb
i18n-1.8.9 lib/i18n/core_ext/hash.rb