Stringex

Jan 29th — 9:55 AM

A while back I wrote a Ruby port of Sean Burke’s Text::Unidecode for Perl. I was sick of relying on Iconv’s inconsistencies and decided I could do better. (Yes, I can be powerful prideful like that!) I published the whole shebang as a gem but never really did anything else with it. A couple of months later, when looking for a good permalink plugin solution I noticed they all relied on Iconv or naïve Regular Expressions. Yuck! So I did what any good Rails coder would do and stole the good bits from the existing plugin and added my own flavor to it. Enter Stringex! A [hopefully] useful collection of extensions to Ruby’s String class. Included are the Unidecode gem as a lib instead, my acts_as_url permalink plugin and some miscellaneous String methods I personally find kinda useful.

The only thing in Stringex that requires any kind of setup is acts_as_url and it’s dead simple. Call the class method and pass it the attribute you’d like to base the url on.

acts_as_url :foo

Boom! In the spirit of Rails convention over configuration-ness, I decided that the url field should be called url. No muss, no fuss. If anyone wants to use this code and has problems with the name, just let me know and I could whip in some configuration option to let the user define the field but I’m kinda YAGNI about it. url seems like a good name for an url-friendly representation to me. I also threw in some common text equivalent for miscellaneous characters. Here’s some examples of to_url (the String method behind acts_as_url in action:

# A simple prelude
"simple English".to_url => "simple-english" 
"it's nothing at all".to_url => "its-nothing-at-all" 
"rock & roll".to_url => "rock-and-roll" 

# Let's show off
"$12 worth of Ruby power".to_url => "12-dollars-worth-of-ruby-power" 
"10% off if you act now".to_url => "10-percent-off-if-you-act-now" 

# You don't even wanna trust Iconv for this next part
"kick it en Français".to_url => "kick-it-en-francais" 
"rock it Español style".to_url => "rock-it-espanol-style" 
"tell your readers 你好".to_url => "tell-your-readers-ni-hao"

A little snazzier than your average permalink, right? As shown in the code above, I went ahead and added to_url as a method on String itself. You never know when you might need/want to convert params or strings or whatever.

The rest of Stringex is just individual methods for String like to_html (which is really just an alternate form of Rails’ textilize helper), strip_html_tags (thanks Typo!), some methods to massage HTML entities into shape, and a few sundry string formatting shortcuts. Nothing terribly special on their own but all together I think they make a nice package. I hope you feel the same. (Gosh, I sound so nice.) You can check the code out from its GitHub repository. (Yeah, you’re gonna have to be eating off the git buffet to sample these sweet sweeties!) The RDoc is a little sparse and the README is downright laughable but the tests are there and it’s pretty readable code itself. If you ask me and all.

Filed under: rails, plugins

2 comments

  • blj said
    Very useful, I need a to_url equivalent in javascript, for now I can get it from server.
  • Kyrre Nygård said
    Way to go man!

Leave a Comment

Lucky Sneaks is designed and managed by Lucky Sneaks
All content © Lucky Sneaks 2008
Lucky Sneaks is Lucky Sneaks