aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/inflector
Commit message (Collapse)AuthorAgeFilesLines
* Missing require AS/core_ext/object/blankAkira Matsuda2019-08-021-0/+1
|
* Prevent error on transliterate with frozen strings.Cliff Pruitt2019-07-311-0/+1
| | | | ActiveSupport::Inflector.transliterate mutates strings by changing encodings. Prior to this commit passing a frozen string would raise a `FrozenError`. This change duplicates the internal string, if frozen, before transliterating.
* Address to rubocop offencesRyuta Kamizono2019-07-311-1/+1
|
* Reduce Array allocationsAkira Matsuda2019-07-311-1/+1
|
* Speedup and reduce Array creation when constantizing a non-namespaced stringAkira Matsuda2019-07-311-25/+29
|
* [ci skip] Fix unclosed tags in `Inflector` docsvzvu3k6k2019-07-301-1/+1
|
* Handle GB18030 strings with invalid characters in transliterateCliff Pruitt2019-07-261-6/+14
| | | | | | GB18030 is Unicode compatible and covers all Unicode code points so we can temporarily convert GB18030 strings to UTF-8 to perform the transliteration. After transliterating we want to convert back to GB18030. In all cases of transcoding, we replace invalid or undefined characters with the default replacement character ("?"). This is in line with the behavior of tidy_bytes which is used on the UTF-8 string before transliterating.
* Handle US-ASCII strings with invalid characters in transliterateCliff Pruitt2019-07-261-2/+15
| | | | US-ASCII is a subset of UTF-8 so we can temporarily convert US-ASCII strings to UTF-8 to perform the transliteration. After we've converted characters to ASCII representations, we can set the encoding back to US-ASCII to return the same encoding we accepted.
* Raise errors for ASCII-8BIT encoding in ActiveSupport::Inflector::transliterateCliff Pruitt2019-07-261-0/+4
| | | | Adds ArgumentErrors to `ActiveSupport::Inflector::transliterate` if a string is with ASCII-8BIT which will raise an error in `unicode_normalize`.
* Revert "Make UTF-8 string requirement explicit for ↵Cliff Pruitt2019-07-171-7/+2
| | | | `ActiveSupport::Inflector.transliterate`"
* Make UTF-8 string requirement explicit for `transliterate`Cliff Pruitt2019-07-161-2/+7
| | | | | | It's noted in #34062 that String#parameterize will raise an `Encoding::CompatibilityError` if the string is not UTF-8 encoded. The error is raised as a result of passing the string to `.unicode_normalize`. This PR raises a higher level `ArgumentError` if the provided string is not UTF-8 and updates documentation to note the encoding requirement.
* active_support/deprecation is not in use hereAkira Matsuda2019-07-121-1/+0
|
* Enable `Layout/EmptyLinesAroundAccessModifier` copRyuta Kamizono2019-06-132-2/+0
| | | | | | | | | | | We sometimes say "✂️ newline after `private`" in a code review (e.g. https://github.com/rails/rails/pull/18546#discussion_r23188776, https://github.com/rails/rails/pull/34832#discussion_r244847195). Now `Layout/EmptyLinesAroundAccessModifier` cop have new enforced style `EnforcedStyle: only_before` (https://github.com/rubocop-hq/rubocop/pull/7059). That cop and enforced style will reduce the our code review cost.
* Update docs for 'parameterize()' [ci skip]Sharang Dashputre2019-03-161-5/+9
|
* Fix bug with parametrize when `locale` is passedSharang Dashputre2019-03-121-1/+1
| | | | Also add tests for parametrize and transliterate
* Add locale option to parameterizeKaan Ozkan2019-03-111-8/+7
| | | | | | Parameterize is triggering I18n#transliterate. This method already accepts a locale. It would be cleaner if similar to other string inflection methods #parameterize also accepted 'locale' as a parameter.
* Remove deprecated `#acronym_regex` method from `Inflections`Rafael Mendonça França2019-01-171-2/+1
|
* Fix safe_constantize to not raise a LoadError.Keenan Brock2019-01-091-0/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | ### Summary There was an issues when using `safe_constantize` on a string that has the wrong case. File `em.rb` defines `EM`. `"Em".safe_constantize` causes a little confusion with the autoloader. The autoloader finds file "em.rb", expecting it to define `Em`, but `Em` is not defined. The autoloader raises a `LoadError`, which is good, But `safe_constantize` is defined to return `nil` when a class is not found. ### Before ``` "Em".safe_constantize LoadError: Unable to autoload constant Em, \ expected rails/activesupport/test/autoloading_fixtures/em.rb to define it ``` ### After ``` "Em".safe_constantize # => nil ```
* Use native `Array#append`, `Array#prepend`, `Hash#transform_keys`, and ↵Ryuta Kamizono2018-12-201-1/+0
| | | | | | | | | | `Hash#transform_keys!` Since Rails 6 requires Ruby 2.5. https://github.com/ruby/ruby/blob/ruby_2_5/NEWS Follow up #34754.
* Deprecate Unicode#normalize and Chars#normalize (#34202)Francesco Rodríguez2018-10-121-3/+3
|
* Add `Style/RedundantFreeze` to remove redudant `.freeze`Yasuo Honda2018-09-292-16/+16
| | | | | | | | | | | | | | | | | | | | | Since Rails 6.0 will support Ruby 2.4.1 or higher `# frozen_string_literal: true` magic comment is enough to make string object frozen. This magic comment is enabled by `Style/FrozenStringLiteralComment` cop. * Exclude these files not to auto correct false positive `Regexp#freeze` - 'actionpack/lib/action_dispatch/journey/router/utils.rb' - 'activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb' It has been fixed by https://github.com/rubocop-hq/rubocop/pull/6333 Once the newer version of RuboCop released and available at Code Climate these exclude entries should be removed. * Replace `String#freeze` with `String#-@` manually if explicit frozen string objects are required - 'actionpack/test/controller/test_case_test.rb' - 'activemodel/test/cases/type/string_test.rb' - 'activesupport/lib/active_support/core_ext/string/strip.rb' - 'activesupport/test/core_ext/string_ext_test.rb' - 'railties/test/generators/actions_test.rb'
* Remove unused `require "active_support/core_ext/regexp"`Ryuta Kamizono2018-07-292-2/+0
| | | | | | | | Ruby 2.4 has native `Regexp#match?`. https://ruby-doc.org/core-2.4.0/Regexp.html#method-i-match-3F Related #32034.
* `ActiveSupport::Inflector#ordinal` and `ActiveSupport::Inflector#ordinalize`Christian Blais2018-03-051-13/+2
| | | | | | | | | | | | | | | | | | | | | | | | now support translations through I18n. { fr: { number: { nth: { ordinals: lambda do |_key, number:, **_options| if number.to_i.abs == 1 'er' else 'e' end end, ordinalized: lambda do |_key, number:, **_options| "#{number}#{ActiveSupport::Inflector.ordinal(number)}" end } } } }
* Enable autocorrect for `Lint/EndAlignment` copKoichi ITO2018-01-182-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ### Summary This PR changes .rubocop.yml. Regarding the code using `if ... else ... end`, I think the coding style that Rails expects is as follows. ```ruby var = if cond a else b end ``` However, the current .rubocop.yml setting does not offense for the following code. ```ruby var = if cond a else b end ``` I think that the above code expects offense to be warned. Moreover, the layout by autocorrect is unnatural. ```ruby var = if cond a else b end ``` This PR adds a setting to .rubocop.yml to make an offense warning and autocorrect as expected by the coding style. And this change also fixes `case ... when ... end` together. Also this PR itself is an example that arranges the layout using `rubocop -a`. ### Other Information Autocorrect of `Lint/EndAlignment` cop is `false` by default. https://github.com/bbatsov/rubocop/blob/v0.51.0/config/default.yml#L1443 This PR changes this value to `true`. Also this PR has changed it together as it is necessary to enable `Layout/ElseAlignment` cop to make this behavior.
* Update incorrect backtick usage in RDoc to teletypeT.J. Schuck2017-11-221-2/+2
| | | [ci skip]
* Merge pull request #30782 from NickLaMuro/improve_performance_of_inflectionsMatthew Draper2017-11-142-5/+19
|\ | | | | Cache regexps generated from acronym_regex
| * Deprecate ActiveSupport::Inflector#acronym_regexNick LaMuro2017-10-281-2/+4
| | | | | | | | | | | | | | | | | | To be removed in Rails 6.0 (default for the deprecate helper). Code moved around as well for the ActiveSupport::Deprecation modules, since it was dependent on ActiveSupport::Inflector being loaded for it to work. By "lazy loading" the Inflector code from within the Deprecation code, we can require ActiveSupport::Deprecation from ActiveSupport::Inflector and not get a circular dependency issue.
| * Cache regexps generated from acronym_regexNick LaMuro2017-10-232-6/+18
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The Problem ----------- The following line from `String#camelize`: string = string.sub(/^(?:#{inflections.acronym_regex}(?=\b|[A-Z_])|\w)/) { |match| match.downcase } and the following line from `String#camelize`: word.gsub!(/(?:(?<=([A-Za-z\d]))|\b)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1 && '_'.freeze }#{$2.downcase}" }#{$2.downcase}" } Both generate the same regexep in the first part of the `.sub`/`.gsub` method calls every time the function is called, creating an extra object allocation each time. The value of `acronym_regex` only changes if the user decides add an acronym to the current set of inflections and apends another string on the the regexp generated here, but beyond that it remains relatively static. This has been around since acronym support was introduced back in 2011 in PR#1648. Proposed Solution ----------------- To avoid re-generating these strings every time these methods are called, cache the values of these regular expressions in the `ActiveSupport::Inflector::Inflections` instance, making it so these regular expressions are only generated once, or when the acronym's are added to. Other notable changes is the attr_readers are nodoc'd, as they shouldn't really be public APIs for users. Also, the new method, define_acronym_regex_patterns, is the only method in charge of manipulating @acronym_regex, and initialize_dup also makes use of that new change. ** Note about fix for non-deterministic actionpack test ** With the introduction of `@acronym_underscore_regex` and `@acronym_camelize_regex`, tests that manipulated these for a short time, then reset them could caused test failures to happen. This happened because the previous way we reset the `@acronyms` and `@acronym_regex` was the set them using #instance_variable_set, which wouldn't run the #define_acronym_regex_patterns method. This has now been introduced into the actionpack tests to avoid this failure.
* | Fix acronym support in `humanize`Andrew White2017-11-061-1/+1
| | | | | | | | | | | | | | | | | | Acronym inflections are stored with lowercase keys in the hash but the match wasn't being lowercased before being looked up in the hash. This shouldn't have any performance impact because before it would fail to find the acronym and perform the `downcase` operation anyway. Fixes #31052.
* | Fix french spelling mistake Skander2017-11-061-6/+6
| | | | | | | | Trés -> Très https://fr.wiktionary.org/wiki/tr%C3%A8s
* | [ci skip] show the correct example to demonstrate inflections.Aditya Kapoor2017-11-011-1/+7
|/
* [Active Support] require_relative => requireAkira Matsuda2017-10-213-7/+7
| | | | This basically reverts 8da30ad6be34339124ba4cb4e36aea260dda12bc
* Enable `Layout/FirstParameterIndentation` copRyuta Kamizono2017-07-171-3/+4
| | | | | | | We have some indentation cops. But now there is a little inconsistent params indentations. Enable `Layout/FirstParameterIndentation` cop to prevent newly inconsistent indentation added and auto-correct to existing violations.
* [Active Support] `rubocop -a --only Layout/EmptyLineAfterMagicComment`Koichi ITO2017-07-113-0/+3
|
* Use frozen-string-literal in ActiveSupportKir Shatrov2017-07-093-0/+3
|
* [Active Support] require => require_relativeAkira Matsuda2017-07-013-7/+7
|
* Fix pluralization of uncountables when given a localeEilis Hamilton2017-05-191-6/+9
| | | | | | | | | Previously apply_inflections would only use the :en uncountables rather then the ones for the locale that was passed to pluralize or singularize. This changes apply_inflections to take a locale which it will use to find the uncountables.
* Use keyword arguments instead of hashRafael Mendonça França2017-03-281-5/+7
|
* Merge pull request #28480 from ↵Rafael Mendonça França2017-03-281-12/+22
|\ | | | | | | | | | | mubashirhanif/add_keep_id_suffix_option_to_humanize_new Add keep id suffix option to humanize new
| * Added options hash to titleize method and keep_id_suffix option to humanizeMubashir Hanif2017-03-211-12/+22
| | | | | | | | | | | | | | | | | | | | | | | | some documentation remove extra whitespace. Added id in the middle test case and corrected some testcases. Some Coding standard guidelines corrections as suggested by codeclimate. Some more corrections suggested by codeclimate.
* | Update `titlelize` regex to allow apostrophesAndrew White2017-03-061-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In 4b685aa the regex in `titlelize` was updated to not match apostrophes to better reflect the nature of the transformation. Unfortunately this had the side effect of breaking capitalization on the first word of a sub-string, e.g: >> "This was 'fake news'".titleize => "This Was 'fake News'" This is fixed by extending the look-behind to also check for a word character on the other side of the apostrophe. Fixes #28312.
* | Raise ArgumentError if attempting to transliterate anything that is not a stringKevin McPhillips2017-01-161-0/+2
| |
* | There's no such moduleAkira Matsuda2017-01-051-4/+4
| |
* | Prefer Regexp#match? over Regexp#===Matthew Draper2016-12-311-1/+2
| |
* | Only add regexes for the new wordsMatthew Draper2016-12-311-2/+3
| |
* | Privatize unneededly protected methods in Active SupportAkira Matsuda2016-12-241-1/+1
| |
* | Fix constantize edge case involving prepend, autoloading and name conflictsJean Boussier2016-12-141-1/+1
|/ | | | | | | | | | | | | | | | | | | | | | | | | | | In the following situation: ```ruby class Bar end module Baz end class Foo prepend Baz end class Foo::Bar end ``` Running `Inflector.constantize('Foo::Bar')` would blow up with a NameError. What is happening is that `constatize` was written before the introduction of prepend, and wrongly assume that `klass.ancestors.first == klass`. So it uses `klass.ancestors.inject` without arguments, as a result a prepended module is used in place of the actual class.
* Remove deprecated separator argument from parameterizeAndrew White2016-11-141-5/+1
|
* Add more rubocop rules about whitespacesRafael Mendonça França2016-10-291-2/+2
|
* Fix broken comments indentation caused by rubocop auto-correct [ci skip]Ryuta Kamizono2016-09-141-9/+9
| | | | | | All indentation was normalized by rubocop auto-correct at 80e66cc4d90bf8c15d1a5f6e3152e90147f00772. But comments was still kept absolute position. This commit aligns comments with method definitions for consistency.