Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plural? and singular? methods for String #6802

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions activesupport/lib/active_support/core_ext/string/inflections.rb
Expand Up @@ -55,6 +55,24 @@ def singularize(locale = :en)
ActiveSupport::Inflector.singularize(self, locale)
end

# Returns true if the string is singular
#
# 'post'.singular? # => true
# 'posts'.singular? # => false
# 'sheep'.singular? # => true
def singular?
ActiveSupport::Inflector.singular?(self)
end

# Returns true if the string is plural
#
# 'posts'.plural? # => true
# 'post'.plural? # => false
# 'sheep'.plural? # => true
def plural?
ActiveSupport::Inflector.plural?(self)
end

# +constantize+ tries to find a declared constant with the name specified
# in the string. It raises a NameError when the name is not in CamelCase
# or is not initialized. See ActiveSupport::Inflector.constantize
Expand Down
18 changes: 18 additions & 0 deletions activesupport/lib/active_support/inflector/methods.rb
Expand Up @@ -50,6 +50,24 @@ def singularize(word, locale = :en)
apply_inflections(word, inflections(locale).singulars)
end

# Returns true if the string is plural
#
# 'posts'.plural? # => true
# 'post'.plural? # => false
# 'sheep'.plural? # => true
def plural?(word)
pluralize(word) == word
end

# Returns true if the string is singular
#
# 'post'.singular? # => true
# 'posts'.singular? # => false
# 'sheep'.singular? # => true
def singular?(word)
singularize(word) == word
end

# By default, +camelize+ converts strings to UpperCamelCase. If the argument
# to +camelize+ is set to <tt>:lower</tt> then +camelize+ produces
# lowerCamelCase.
Expand Down
16 changes: 16 additions & 0 deletions activesupport/test/core_ext/string_ext_test.rb
Expand Up @@ -71,6 +71,22 @@ def test_singularize
end
end

def test_plural
SingularToPlural.values.each do |plural|
assert plural.plural?
end

assert !"search".plural?
end

def test_singular
SingularToPlural.keys.each do |singular|
assert singular.singular?
end

assert !"searches".singular?
end

def test_titleize
MixtureToTitleCase.each do |before, titleized|
assert_equal(titleized, before.titleize)
Expand Down
15 changes: 15 additions & 0 deletions activesupport/test/inflector_test.rb
Expand Up @@ -70,6 +70,21 @@ def test_uncountable_word_is_not_greedy
end
end

def test_singular
SingularToPlural.each do |singular, plural|
assert ActiveSupport::Inflector.singular?(singular)
end

assert !ActiveSupport::Inflector.singular?('searches')
end

def test_plural
SingularToPlural.each do |singular, plural|
assert ActiveSupport::Inflector.plural?(plural)
end

assert !ActiveSupport::Inflector.plural?('search')
end

def test_overwrite_previous_inflectors
assert_equal("series", ActiveSupport::Inflector.singularize("series"))
Expand Down
2 changes: 2 additions & 0 deletions guides/source/4_0_release_notes.md
Expand Up @@ -868,6 +868,8 @@ Active Support

* Adds `encode_big_decimal_as_string` option to force JSON serialization of BigDecimals as numeric instead of wrapping them in strings for safety.

* Adds `String#plural?` to determine if a string is plural and `String#singular?` to determine if a string is singular.

### Deprecations

* `ActiveSupport::Callbacks`: deprecate usage of filter object with `#before` and `#after` methods as `around` callback.
Expand Down
28 changes: 28 additions & 0 deletions guides/source/active_support_core_extensions.md
Expand Up @@ -1454,6 +1454,20 @@ end

NOTE: Defined in `active_support/core_ext/string/inflections.rb`.

#### `plural?`

Returns whether the string is plural or not:

```ruby
"tables".plural? # => true
"ruby".plural? # => false
"equipment".plural? # => true
```

`plural?` will call the `pluralize` method to determine if a word is plural or not, and abides by the same rules for irregular plural words as defined in Acitve Support and `config/initializers/inflections.rb`.

NOTE: Defined in `active_support/core_ext/string/inflections.rb`.

#### `singularize`

The inverse of `pluralize`:
Expand All @@ -1477,6 +1491,20 @@ end

NOTE: Defined in `active_support/core_ext/string/inflections.rb`.

#### `singular?`

Returns whether the string is singular or not:

```ruby
"table".singular? # => true
"rubies".singular? # => false
"equipment".singular? # => true
```

`singular?` will call the `singularize` method to determine if a word is singular or not, and abides by the same rules for irregular plural words as defined in Acitve Support and `config/initializers/inflections.rb`.

NOTE: Defined in `active_support/core_ext/string/inflections.rb`.

#### `camelize`

The method `camelize` returns its receiver in camel case:
Expand Down