Porting the Rails Inflector to Lisp
trapm.com
trapm.com
Damian Conway (Lingua::EN::Inflect's author) also wrote a paper about algorithmic English pluralization that actually takes a linguistic approach to all of this: http://www.csse.monash.edu.au/~damian/papers/HTML/Plurals.ht...
The hard part about an inflector as part of something like a framework is that it's hard to add special cases later when you need corrections, because you risk breaking the existing incorrectly pluralized or singularized names people have for things. It's much easier to get it as right as possible as soon as possible.
It's not even that most of the code is hard to read (although a few parts did leave me scratching my head), it's just that without the lingustic explanations I'd be lost.
I wouldn't blame you if you held off porting it for the time being. A stopgap measure is definitely better than nothing. I just wanted to make sure you knew about it.
For example, let's say you have a Big Giant App with fifty database tables, each with a corresponding model object. You can use the Rails inflector, which is God-knows-how-many lines of code by now. Or, you can have a 50-line file that looks like this:
model_table_map = {
User => 'users',
Shark => 'sharks',
Lazer => 'lazers',
Antenna => 'antennae',
...
Locus => 'loci',
Species => 'species'
}
(Don't ask me what kind of screwed up, terrifying app you could possibly be writing that requires these classes, 'cause I don't know!)This has numerous advantages over using the Inflector:
- The conversion from singular class name to plural table name is unambiguous and self-documenting, so if you come back to the code in 6 months, or if you are working with someone for whom English is not their primary language, you don't have to wonder, "Hmmm... is 'locus' one of those words with an irregular plural inflection? If so, does the Inflector know about it?"
- It's one less dependency you need to trust.
- Memoization makes this already-minuscule point nearly moot, but calling model_table_map[obj.class] is faster than inflecting it.
Like I said: It's really not a big deal, and probably counts more towards personal preference than anything else. I simply prefer to be as explicit as possible until it would simply hurt less to use magic.
"Design is the successive application of constraints until only a unique product is left." - Donald Norman, The Design of Everyday Things
1. Can something non-trivial be trivially exemplified? I dunno. :-/