forked from ruby-i18n/i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
No longer rely on refinements for Hash utility methods.
Refinements have a very significant performance overhead. The simple fact of refining a method, even if the refinement is never used will make calls to that method 40% slower. On rarely called methods in doesn't matter that much, but I18n refine some popular Active Support methods, so any project including the I18n gem suffer from a significant performance penalty. See this benchmark for more details: https://gist.github.com/casperisfine/1c46f05cccfa945cd156f445f0d3d6fa In the end these refinements are easily replace by simple module functions taking one extra argument.
- Loading branch information
Showing
11 changed files
with
96 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# frozen_string_literal: true | ||
|
||
module I18n | ||
module Utils | ||
class << self | ||
if Hash.method_defined?(:except) | ||
def except(hash, *keys) | ||
hash.except(*keys) | ||
end | ||
else | ||
def except(hash, *keys) | ||
hash = hash.dup | ||
keys.each { |k| hash.delete(k) } | ||
hash | ||
end | ||
end | ||
|
||
def deep_merge(hash, other_hash, &block) | ||
deep_merge!(hash.dup, other_hash, &block) | ||
end | ||
|
||
def deep_merge!(hash, other_hash, &block) | ||
hash.merge!(other_hash) do |key, this_val, other_val| | ||
if this_val.is_a?(Hash) && other_val.is_a?(Hash) | ||
deep_merge(this_val, other_val, &block) | ||
elsif block_given? | ||
yield key, this_val, other_val | ||
else | ||
other_val | ||
end | ||
end | ||
end | ||
|
||
def deep_symbolize_keys(hash) | ||
hash.each_with_object({}) do |(key, value), result| | ||
result[key.respond_to?(:to_sym) ? key.to_sym : key] = deep_symbolize_keys_in_object(value) | ||
result | ||
end | ||
end | ||
|
||
private | ||
|
||
def deep_symbolize_keys_in_object(value) | ||
case value | ||
when Hash | ||
deep_symbolize_keys(value) | ||
when Array | ||
value.map { |e| deep_symbolize_keys_in_object(e) } | ||
else | ||
value | ||
end | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require 'test_helper' | ||
|
||
class I18nUtilsTest < I18n::TestCase | ||
|
||
test ".deep_symbolize_keys" do | ||
hash = { 'foo' => { 'bar' => { 'baz' => 'bar' } } } | ||
expected = { :foo => { :bar => { :baz => 'bar' } } } | ||
assert_equal expected, I18n::Utils.deep_symbolize_keys(hash) | ||
end | ||
|
||
test "#deep_symbolize_keys with numeric keys" do | ||
hash = { 1 => { 2 => { 3 => 'bar' } } } | ||
expected = { 1 => { 2 => { 3 => 'bar' } } } | ||
assert_equal expected, I18n::Utils.deep_symbolize_keys(hash) | ||
end | ||
|
||
test "#except" do | ||
hash = { :foo => 'bar', :baz => 'bar' } | ||
expected = { :foo => 'bar' } | ||
assert_equal expected, I18n::Utils.except(hash, :baz) | ||
end | ||
|
||
test "#deep_merge!" do | ||
hash = { :foo => { :bar => { :baz => 'bar' } }, :baz => 'bar' } | ||
I18n::Utils.deep_merge!(hash, :foo => { :bar => { :baz => 'foo' } }) | ||
|
||
expected = { :foo => { :bar => { :baz => 'foo' } }, :baz => 'bar' } | ||
assert_equal expected, hash | ||
end | ||
end |