Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
Signed-off-by: David Celis <david@davidcelis.com>
  • Loading branch information
David Celis committed Jul 19, 2012
0 parents commit b96b355
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
@@ -0,0 +1,17 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in inflections.gemspec
gemspec
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
Copyright (c) 2012 David Celis

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 changes: 41 additions & 0 deletions README.markdown
@@ -0,0 +1,41 @@
# Inflections

This gem is to remove the cruft from ActiveSupport's inflections and provide a more sane set of defaults for Ruby/Rails applications.

At the time of this gem's publication, the list of inflections in ActiveSupport is a mess. It is riddled with special cases such as a special pluralization rule for "octopus" and "virus", even though they follow a regular rule (as octopi and viri are disputed terms). Similar pluralization rules exist for "ox", "quiz", "mouse", "louse", etc.

Many of the special cases that ActiveSupport defines will not see the light of day in an application. Other rules exist that are actually gramatical exceptions, such as changing "f" to a "v" when encountered at the end of the word (which then requires even more rules to fix special words such as "drive", "safe", "hive", etc.). And, of course, who can forget the special pluralization of "cow" to the archaic term of Scottish origin, "kine" (the plural of "kye")?

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'inflections'
```

And then execute:

```bash
$ bundle
```

## Usage

If you're using Rails, you're done. The default inflections defined in ActiveSupport will be overwritten, and you can continue to define your own special cases in `config/intializers/inflections.rb`.

Otherwise, wherever you need 'em:

```ruby
require 'inflections'
```

## Contributing

Please note that pull requests will only be accepted for rules that are in error or a potentially missed rule. If your change is an exception to an existing rule, that exception must occur _frequently_ and must involved words used more frequently than the regular plurals. If your change is an irregularity, it must be a word that is arguably _frequently_ encountered in applications that would use ActiveSupport.

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`) with tests
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
2 changes: 2 additions & 0 deletions Rakefile
@@ -0,0 +1,2 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
22 changes: 22 additions & 0 deletions inflections.gemspec
@@ -0,0 +1,22 @@
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/inflections/version', __FILE__)

Gem::Specification.new do |gem|
gem.authors = ['David Celis']
gem.email = ['david@davidcelis.com']
gem.description = %q{A better set of singularization and pluralization rules for Ruby/Rails applications using ActiveSupport.}
gem.summary = %q{Sane inflection rules for ActiveSupport.}
gem.homepage = 'https://github.com/davidcelis/inflections'

gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = 'inflections'
gem.require_paths = ['lib']
gem.version = Inflections::VERSION

gem.add_development_dependency 'bundler'
gem.add_development_dependency 'minitest'

gem.add_dependency 'activesupport', '>= 2.2.1'
end
74 changes: 74 additions & 0 deletions inflections_test.rb
@@ -0,0 +1,74 @@
require 'inflections'
require 'minitest/autorun'

class TestInflections < MiniTest::Unit::TestCase
def test_regular_plurals
assert_equal 'dogs', 'dog'.pluralize
assert_equal 'dog', 'dogs'.singularize

assert_equal 'days', 'day'.pluralize
assert_equal 'day', 'days'.singularize

assert_equal 'tests', 'test'.pluralize
assert_equal 'test', 'tests'.singularize
end

def test_sibilant_sounds
assert_equal 'addresses', 'address'.pluralize
assert_equal 'address', 'addresses'.singularize

assert_equal 'phases', 'phase'.pluralize
# Can't assume whether words that end with 'es' remove the 'es' or just 's'
# So I went with the case that seemed more reasonable given sibilant sounds
#
# assert_equal 'phase', 'phases'.singularize

assert_equal 'buzzes', 'buzz'.pluralize
assert_equal 'buzz', 'buzzes'.singularize

assert_equal 'boxes', 'box'.pluralize
assert_equal 'box', 'boxes'.singularize

assert_equal 'benches', 'bench'.pluralize
assert_equal 'bench', 'benches'.singularize

assert_equal 'dishes', 'dish'.pluralize
assert_equal 'dish', 'dishes'.singularize
end

def test_oes_rule
assert_equal 'heroes', 'hero'.pluralize
assert_equal 'hero', 'heroes'.singularize

assert_equal 'igloos', 'igloo'.pluralize
assert_equal 'igloo', 'igloos'.singularize
end

def test_ies_rule
assert_equal 'berries', 'berry'.pluralize
assert_equal 'berry', 'berries'.singularize

assert_equal 'days', 'day'.pluralize
assert_equal 'day', 'days'.singularize
end

def test_irregulars
assert_equal 'children', 'child'.pluralize
assert_equal 'child', 'children'.singularize

assert_equal 'people', 'person'.pluralize
assert_equal 'person', 'people'.singularize

assert_equal 'selves', 'self'.pluralize
assert_equal 'self', 'selves'.singularize
end

def test_uncountables
assert_equal 'series', 'series'.pluralize
assert_equal 'series', 'series'.singularize
end

def test_stupid_inflections_removed
assert_equal 'cows', 'cow'.pluralize
end
end
25 changes: 25 additions & 0 deletions lib/inflections.rb
@@ -0,0 +1,25 @@
require 'active_support/inflector'

module Inflections
end

ActiveSupport::Inflector.inflections do |inflect|
inflect.clear

inflect.plural(/$/, 's')
inflect.plural(/([sxz]|[cs]h)$/i, '\1es')
inflect.plural(/([^aeiouy]o)$/i, '\1es')
inflect.plural(/([^aeiouy])y$/i, '\1ies')

inflect.singular(/s$/i, '')
inflect.singular(/(ss)$/i, '\1')
inflect.singular(/([sxz]|[cs]h)es/, '\1')
inflect.singular(/([^aeiouy]o)es$/, '\1')
inflect.singular(/([^aeiouy])ies$/i, '\1y')

inflect.irregular('child', 'children')
inflect.irregular('person', 'people')
inflect.irregular('self', 'selves')

inflect.uncountable(%w(series))
end
3 changes: 3 additions & 0 deletions lib/inflections/version.rb
@@ -0,0 +1,3 @@
module Inflections
VERSION = "0.0.1"
end

0 comments on commit b96b355

Please sign in to comment.