Rob's Tips and TricksSaturday, July 10, 2010I have been working on a back-end management UI for a client of mine, and ran into an old, old problem in web development - plural nouns. I found my code filled with lots of conditional code to determine when to add an "s" to things, eg: "You have 1 active listing" vs "You have 2 active listings"
In Rails, I have a pluralize method to help (though it only goes so far), and I was looking for something similar in PHP, which led me to this excellent post over at kuwamoto.org. It was the perfect thing - a port of Rails' pluralize/singularize methods in PHP, with a few improvements to boot. Once I had the code installed on our system, however, I found that it wasn't enough. I still had conditionals. I still had blocks of ugly code to make things work. So I came up with something better... The Clever BitI wrote the following function to take not a single word, but a phrase. It pulls out the last word in the phrase, and pluralizes it. The clever bit is, if there's a number in the phrase, it conditionally pluralizes the last word. This was exactly what I needed in my code, and allows me to concisely and elegantly solve one of those niggling problems all web developers deal with. Check out the function here:
ExamplesHere are some sample strings, and the result of running them through smart_pluralize():
Integrating it with SmartyNow that I had a smart phrase pluralizer, I needed to integrate it into Smarty, my template system of, er, choice. :-) I wrote a Smarty block helper named "pluralize" that simply passed the contained text into smart_pluralize() and voila! Clean templates:
Just write your text in singular, include the count in the phrase somewhere, and you're off and running. It's been a real blessing, hope you find it helpful as well!
Posted
by Rob
at 11:11 PM
Add a comment
Sunday, September 13, 2009I do a lot of procedural content generation - thumbnailing, gradients, css, you name it. With luck, many of those pieces of code will make it up onto this site at some point. But today, I'd like to introduce a nice helper tool that I use across the board - Color.rb.
What's it do?The Color class represents a web color (rgb + a) and allows manipulating it. You can create a color by values (255,0,0) or by string ('#FF0000'). You can lighten, darken, and blend colors together by percentage amounts. Let's start with a few examples:
Real-world UsageSo that's nice, but where would you actually use this? A simple example is parametric CSS rule generation. Here's a sample CSS file:
Obviously simplified, but it will suffice. You hard-code your values, and manually set gray to be halfway between your text and your background. Simple and clean. However, when you decide you'd like to modify your site's color scheme, it's a major pain going through and changing all the color values. A better way to set things up would be to generate your CSS using ERB, like this:
Now you can change the colors at the top, and re-use them throughout your rule set. But you can quickly see the problem. The .grayed rule really means "half way between the foreground and background colors." With simple strings for your color values, you can't achieve this. But, with the magic of the Color class, you can write your rules as you mean them and have it work automatically when you adjust your color scheme.
This is a very small sample, but I think you'll find as I have that when you have hundreds of CSS rules referencing various values, blending between colors, and so on, having the ability to manage the colors is very valuable. Download the file here: Color.rb
Posted
by Rob
at 4:04 PM
Add a comment
Sunday, August 9, 2009String#extract is an extension to the core String class that simplifies extracting values from a string using regular expressions.
The codeFirst, let me show you the method:
So, what does it do?I always find using String#match and its Regex equivalents to be tedious and hard to follow. Here's an example of trying to pull the various phone fields out of a user-entered phone number:
Instead, check out using the extract method:
Basically, #extract allows you to use a regex to pull a value out of a string instance, cleanly. Here are a few more examples:
So there you have it. Extract a single value with a plain regular expression, or one or more values using regexen with capture groups in them. And the syntax is clean and elegant. Hope you like it!
Posted
by Rob
at 9:09 PM
1 comment
Sunday, August 2, 2009Thanks for stopping by. My name is Rob Morris, and I'm a web developer/software architect/code dude located in North Carolina. I've set up this blog as a home for tips and techniques that I run across or develop in the line of duty.
I primarily work in Ruby/Rails these days, with a heavy helping of jQuery and PHP on the side, so that's what you'll mostly find in these posts. I hope you find the information useful, and please feel free to join the discussion. Cheers! -Rob
Posted
by Rob
at 9:09 PM
Add a comment
|
AboutWeb home of Rob Morris, software guru and all-around great guy. A place to post and discuss code and the software development lifestyle. ArchiveJuly 2010 (1)September 2009 (1)August 2009 (2) |