Permalink
Browse files

Remove Hash methods from I18n::Backend::Helpers and add them to core …

…extension
  • Loading branch information...
1 parent 524d8e9 commit 4f9592f20d02e1193571540e8865af0b491b47fb @josevalim josevalim committed Apr 30, 2010
View
@@ -12,9 +12,9 @@
module I18n
autoload :Backend, 'i18n/backend'
- autoload :Helpers, 'i18n/helpers'
- autoload :Locale, 'i18n/locale'
autoload :Config, 'i18n/config'
+ autoload :Gettext, 'i18n/gettext'
+ autoload :Locale, 'i18n/locale'
class << self
# Gets I18n configuration object.
View
@@ -10,7 +10,6 @@ module Backend
autoload :Fast, 'i18n/backend/fast'
autoload :Flatten, 'i18n/backend/flatten'
autoload :Gettext, 'i18n/backend/gettext'
- autoload :Helpers, 'i18n/backend/helpers'
autoload :InterpolationCompiler, 'i18n/backend/interpolation_compiler'
autoload :KeyValue, 'i18n/backend/key_value'
autoload :Metadata, 'i18n/backend/metadata'
@@ -37,7 +37,7 @@ def lookup(locale, key, scope = [], options = {})
hash[r.key.slice(chop_range)] = r.value
hash
end
- deep_symbolize_keys(result)
+ result.deep_symbolize_keys
end
end
View
@@ -1,12 +1,11 @@
# encoding: utf-8
require 'yaml'
-require 'i18n/core_ext/hash/except'
+require 'i18n/core_ext/hash'
module I18n
module Backend
module Base
- include I18n::Backend::Helpers
include I18n::Backend::Transliterator
RESERVED_KEYS = [:scope, :default, :separator, :resolve]
@@ -259,8 +258,8 @@ def merge_translations(locale, data, options = {})
locale = locale.to_sym
translations[locale] ||= {}
- data = deep_symbolize_keys(data)
- deep_merge_hash!(translations[locale], data)
+ data = data.deep_symbolize_keys
+ translations[locale].deep_merge!(data)
end
end
end
@@ -16,7 +16,7 @@ module Backend
#
# The implementation assumes that all backends added to the Chain implement
# a lookup method with the same API as Simple backend does.
- class Chain < Simple
+ class Chain
attr_accessor :backends
def initialize(*backends)
@@ -1,25 +0,0 @@
-module I18n
- module Backend
- module Helpers
- # Return a new hash with all keys and nested keys converted to symbols.
- def deep_symbolize_keys(hash)
- hash.inject({}) { |result, (key, value)|
- value = deep_symbolize_keys(value) if value.is_a?(Hash)
- result[(key.to_sym rescue key) || key] = value
- result
- }
- end
-
- # deep_merge_hash! by Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809
- MERGER = proc do |key, v1, v2|
- # TODO should probably be:
- # raise TypeError.new("can't merge #{v1.inspect} and #{v2.inspect}") unless Hash === v1 && Hash === v2
- Hash === v1 && Hash === v2 ? v1.merge(v2, &MERGER) : (v2 || v1)
- end
-
- def deep_merge_hash!(hash, data)
- hash.merge!(data, &MERGER)
- end
- end
- end
-end
@@ -68,7 +68,7 @@ def lookup(locale, key, scope = [], options = {})
key = normalize_keys(locale, key, scope, options[:separator])
value = @store["#{locale}.#{key}"]
value = ActiveSupport::JSON.decode(value) if value
- value.is_a?(Hash) ? deep_symbolize_keys(value) : value
+ value.is_a?(Hash) ? value.deep_symbolize_keys : value
end
def merge_translations(locale, data, options = {})
@@ -79,7 +79,7 @@ def merge_translations(locale, data, options = {})
when Hash
if @subtrees && (old_value = @store[key])
old_value = ActiveSupport::JSON.decode(old_value)
- value = deep_symbolize_keys(old_value).merge(value) if old_value.is_a?(Hash)
+ value = old_value.deep_symbolize_keys.deep_merge!(value) if old_value.is_a?(Hash)
end
when Proc
raise "Key-value stores cannot handle procs"
@@ -27,7 +27,7 @@ def translation_metadata
def translation_metadata=(translation_metadata)
@translation_metadata = translation_metadata
end
- end
+ end unless Object.method_defined?(:translation_metadata)
end
end
View
@@ -0,0 +1,29 @@
+class Hash
+ def slice(*keep_keys)
+ h = {}
+ keep_keys.each { |key| h[key] = fetch(key) }
+ h
+ end unless Hash.method_defined?(:slice)
+
+ def except(*less_keys)
+ slice(*keys - less_keys)
+ end unless Hash.method_defined?(:except)
+
+ def deep_symbolize_keys
+ inject({}) { |result, (key, value)|
+ value = value.deep_symbolize_keys if value.is_a?(Hash)
+ result[(key.to_sym rescue key) || key] = value
+ result
+ }
+ end unless Hash.method_defined?(:deep_symbolize_keys)
+
+ # deep_merge_hash! by Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809
+ MERGER = proc do |key, v1, v2|
+ Hash === v1 && Hash === v2 ? v1.merge(v2, &MERGER) : (v2 || v1)
+ end
+
+ def deep_merge!(data)
+ merge!(data, &MERGER)
+ end unless Hash.method_defined?(:deep_merge!)
+end
+
@@ -1,8 +0,0 @@
-# from facets (http://facets.rubyforge.org)
-require 'i18n/core_ext/hash/slice'
-
-class Hash
- def except(*less_keys)
- slice(*keys - less_keys)
- end
-end unless Hash.method_defined?(:except)
@@ -1,8 +0,0 @@
-# from facets (http://facets.rubyforge.org)
-class Hash
- def slice(*keep_keys)
- h = {}
- keep_keys.each { |key| h[key] = fetch(key) }
- h
- end
-end unless Hash.new.respond_to?(:slice)
@@ -1,15 +0,0 @@
-# encoding: utf-8
-$:.unshift(File.expand_path(File.dirname(__FILE__) + '/../')); $:.uniq!
-require 'test_helper'
-
-class I18nBackendHelpersTest < Test::Unit::TestCase
- def setup
- @backend = I18n::Backend::Simple.new
- end
-
- test "deep_symbolize_keys" do
- result = @backend.deep_symbolize_keys('foo' => { 'bar' => { 'baz' => 'bar' } })
- expected = {:foo => {:bar => {:baz => 'bar'}}}
- assert_equal expected, result
- end
-end
View
@@ -0,0 +1,33 @@
+# encoding: utf-8
+$:.unshift(File.expand_path(File.dirname(__FILE__) + '/../')); $:.uniq!
+
+require 'test_helper'
+require 'i18n/core_ext/hash'
+
+class I18nCoreExtHashInterpolationTest < Test::Unit::TestCase
+ test "#deep_symbolize_keys" do
+ hash = { 'foo' => { 'bar' => { 'baz' => 'bar' } } }
+ expected = { :foo => { :bar => { :baz => 'bar' } } }
+ assert_equal expected, hash.deep_symbolize_keys
+ end
+
+ test "#slice" do
+ hash = { :foo => 'bar', :baz => 'bar' }
+ expected = { :foo => 'bar' }
+ assert_equal expected, hash.slice(:foo)
+ end
+
+ test "#except" do
+ hash = { :foo => 'bar', :baz => 'bar' }
+ expected = { :foo => 'bar' }
+ assert_equal expected, hash.except(:baz)
+ end
+
+ test "#deep_merge!" do
+ hash = { :foo => { :bar => { :baz => 'bar' } }, :baz => 'bar' }
+ hash.deep_merge!(:foo => { :bar => { :baz => 'foo' } })
+
+ expected = { :foo => { :bar => { :baz => 'foo' } }, :baz => 'bar' }
+ assert_equal expected, hash
+ end
+end

0 comments on commit 4f9592f

Please sign in to comment.