diff options
author | Swaathi K <swaathi@skcript.com> | 2015-10-07 17:46:38 +0530 |
---|---|---|
committer | Swaathi K <swaathi@skcript.com> | 2015-11-07 16:57:04 +0530 |
commit | c9143e15a1ba75137d6c9fe844d8b27dbc412835 (patch) | |
tree | ed65aa404707af702d8200ce7a35ee6a9e97b51e /activesupport/lib/active_support/inflector | |
parent | af3ac5022ec252e45c14f460875edadfb7a64d38 (diff) | |
download | rails-c9143e15a1ba75137d6c9fe844d8b27dbc412835.tar.gz rails-c9143e15a1ba75137d6c9fe844d8b27dbc412835.tar.bz2 rails-c9143e15a1ba75137d6c9fe844d8b27dbc412835.zip |
Parameterize with options to preserve case of string
Added test cases
Using kwargs instead of three seperate functions
Updated parameterize in transliterate.rb
Updated parameterize in transliterate.rb
Added deprecation warnings and updating RDoc+Guide
Misspelled separtor. Fixed.
Deprecated test cases and added support to parameterize with keyword parameters
Squashing commits.
Fixed test cases and added deprecated test cases
Small changes to Gemfile.lock and CHANGELOG
Update Gemfile.lock
Diffstat (limited to 'activesupport/lib/active_support/inflector')
-rw-r--r-- | activesupport/lib/active_support/inflector/transliterate.rb | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/activesupport/lib/active_support/inflector/transliterate.rb b/activesupport/lib/active_support/inflector/transliterate.rb index 103207fb63..871cfb8a72 100644 --- a/activesupport/lib/active_support/inflector/transliterate.rb +++ b/activesupport/lib/active_support/inflector/transliterate.rb @@ -68,29 +68,44 @@ module ActiveSupport # # parameterize("Donald E. Knuth") # => "donald-e-knuth" # parameterize("^trés|Jolie-- ") # => "tres-jolie" - def parameterize(string, sep = '-') + # + # To use a custom separator, override the `separator` argument. + # + # parameterize("Donald E. Knuth", separator: '_') # => "donald_e_knuth" + # parameterize("^trés|Jolie-- ", separator: '_') # => "tres_jolie" + # + # To preserve the case of the characters in a string, use the `preserve_case` argument. + # + # parameterize("Donald E. Knuth", preserve_case: true) # => "Donald-E-Knuth" + # parameterize("^trés|Jolie-- ", preserve_case: true) # => "tres-Jolie" + # + def parameterize(string, sep = :unused, separator: '-', preserve_case: false) + unless sep == :unused + ActiveSupport::Deprecation.warn("Passing the separator argument as a positional parameter is deprecated and will soon be removed. Use `separator: '#{sep}'` instead.") + separator = sep + end # Replace accented chars with their ASCII equivalents. parameterized_string = transliterate(string) # Turn unwanted chars into the separator. - parameterized_string.gsub!(/[^a-z0-9\-_]+/i, sep) + parameterized_string.gsub!(/[^a-z0-9\-_]+/i, separator) - unless sep.nil? || sep.empty? - if sep == "-".freeze + unless separator.nil? || separator.empty? + if separator == "-".freeze re_duplicate_separator = /-{2,}/ re_leading_trailing_separator = /^-|-$/i else - re_sep = Regexp.escape(sep) + re_sep = Regexp.escape(separator) re_duplicate_separator = /#{re_sep}{2,}/ re_leading_trailing_separator = /^#{re_sep}|#{re_sep}$/i end # No more than one of the separator in a row. - parameterized_string.gsub!(re_duplicate_separator, sep) + parameterized_string.gsub!(re_duplicate_separator, separator) # Remove leading/trailing separator. parameterized_string.gsub!(re_leading_trailing_separator, ''.freeze) end - - parameterized_string.downcase! + + parameterized_string.downcase! unless preserve_case parameterized_string end end |