diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2008-08-23 21:41:18 -0700 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2008-08-23 21:41:18 -0700 |
commit | 53e2e8ef2bebb7fb444fe472c657ddb11fae1c89 (patch) | |
tree | 7b90f6e92d318a2112091a2448e2c1d5507a4149 /activesupport | |
parent | 23fb26a0dcbd46d36bdbc2caa267aa3192f77a24 (diff) | |
parent | c471f13db63844fe290615ed6e1ddca32b26570d (diff) | |
download | rails-53e2e8ef2bebb7fb444fe472c657ddb11fae1c89.tar.gz rails-53e2e8ef2bebb7fb444fe472c657ddb11fae1c89.tar.bz2 rails-53e2e8ef2bebb7fb444fe472c657ddb11fae1c89.zip |
Merge branch 'master' into i18n
Diffstat (limited to 'activesupport')
4 files changed, 59 insertions, 31 deletions
diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index 1e189465bd..7ae9e0c6ab 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -39,12 +39,16 @@ module ActiveSupport # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression. # The replacement should always be a string that may include references to the matched data from the rule. def plural(rule, replacement) + @uncountables.delete(rule) if rule.is_a?(String) + @uncountables.delete(replacement) @plurals.insert(0, [rule, replacement]) end # Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression. # The replacement should always be a string that may include references to the matched data from the rule. def singular(rule, replacement) + @uncountables.delete(rule) if rule.is_a?(String) + @uncountables.delete(replacement) @singulars.insert(0, [rule, replacement]) end @@ -55,6 +59,8 @@ module ActiveSupport # irregular 'octopus', 'octopi' # irregular 'person', 'people' def irregular(singular, plural) + @uncountables.delete(singular) + @uncountables.delete(plural) if singular[0,1].upcase == plural[0,1].upcase plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1]) singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1]) @@ -273,32 +279,47 @@ module ActiveSupport underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id") end - # Tries to find a constant with the name specified in the argument string: - # - # "Module".constantize # => Module - # "Test::Unit".constantize # => Test::Unit - # - # The name is assumed to be the one of a top-level constant, no matter whether - # it starts with "::" or not. No lexical context is taken into account: - # - # C = 'outside' - # module M - # C = 'inside' - # C # => 'inside' - # "C".constantize # => 'outside', same as ::C - # end - # - # NameError is raised when the name is not in CamelCase or the constant is - # unknown. - def constantize(camel_cased_word) - names = camel_cased_word.split('::') - names.shift if names.empty? || names.first.empty? + # Ruby 1.9 introduces an inherit argument for Module#const_get and + # #const_defined? and changes their default behavior. + if Module.method(:const_get).arity == 1 + # Tries to find a constant with the name specified in the argument string: + # + # "Module".constantize # => Module + # "Test::Unit".constantize # => Test::Unit + # + # The name is assumed to be the one of a top-level constant, no matter whether + # it starts with "::" or not. No lexical context is taken into account: + # + # C = 'outside' + # module M + # C = 'inside' + # C # => 'inside' + # "C".constantize # => 'outside', same as ::C + # end + # + # NameError is raised when the name is not in CamelCase or the constant is + # unknown. + def constantize(camel_cased_word) + names = camel_cased_word.split('::') + names.shift if names.empty? || names.first.empty? + + constant = Object + names.each do |name| + constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name) + end + constant + end + else + def constantize(camel_cased_word) #:nodoc: + names = camel_cased_word.split('::') + names.shift if names.empty? || names.first.empty? - constant = Object - names.each do |name| - constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name) + constant = Object + names.each do |name| + constant = constant.const_get(name, false) || constant.const_missing(name) + end + constant end - constant end # Turns a number into an ordinal string used to denote the position in an diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb index 0e2a2d7051..9e347d94e9 100755 --- a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb +++ b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb @@ -183,8 +183,8 @@ module I18n # keys are Symbols. def normalize_translation_keys(locale, key, scope) keys = [locale] + Array(scope) + [key] - keys = keys.map{|key| key.to_s.split(/\./) } - keys.flatten.map{|key| key.to_sym} + keys = keys.map{|k| k.to_s.split(/\./) } + keys.flatten.map{|k| k.to_sym} end end end diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb index 6c7c54777c..43ff686514 100644 --- a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb +++ b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb @@ -27,7 +27,7 @@ module I18n def translate(locale, key, options = {}) raise InvalidLocale.new(locale) if locale.nil? - return key.map{|key| translate locale, key, options } if key.is_a? Array + return key.map{|k| translate locale, k, options } if key.is_a? Array reserved = :scope, :default count, scope, default = options.values_at(:count, *reserved) @@ -74,7 +74,7 @@ module I18n def lookup(locale, key, scope = []) return unless key keys = I18n.send :normalize_translation_keys, locale, key, scope - keys.inject(translations){|result, key| result[key.to_sym] or return nil } + keys.inject(translations){|result, k| result[k.to_sym] or return nil } end # Evaluates a default translation. @@ -147,9 +147,9 @@ module I18n type = File.extname(filename).tr('.', '').downcase raise UnknownFileType.new(type, filename) unless respond_to? :"load_#{type}" data = send :"load_#{type}", filename # TODO raise a meaningful exception if this does not yield a Hash - data.each do |locale, data| - merge_translations locale, data - end + data.each do |locale, d| + merge_translations locale, d + end end # Loads a plain Ruby translations file. eval'ing the file must yield diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb index b2fad4a9bc..8eebe1be25 100644 --- a/activesupport/test/inflector_test.rb +++ b/activesupport/test/inflector_test.rb @@ -34,6 +34,13 @@ class InflectorTest < Test::Unit::TestCase end end + def test_overwrite_previous_inflectors + assert_equal("series", ActiveSupport::Inflector.singularize("series")) + ActiveSupport::Inflector.inflections.singular "series", "serie" + assert_equal("serie", ActiveSupport::Inflector.singularize("series")) + ActiveSupport::Inflector.inflections.uncountable "series" # Return to normal + end + MixtureToTitleCase.each do |before, titleized| define_method "test_titleize_#{before}" do assert_equal(titleized, ActiveSupport::Inflector.titleize(before)) |