Archive for March, 2009
Displaying A Default Image
Posted by Anthony in JavaScript, Technology on March 13, 2009
Have you ever wanted to display a default image if the img tag source references a file that cannot be found? You might think that you could use the onerror event on the img tag, but don’t be fooled into thinking it’s that easy! No, it isn’t, because most browsers don’t support that event in the way you expect.
Here’s a little bit of JavaScript that I have used to successfully replace a broken image with a default (assumes prototype is used). Also note that this has been extracted somewhat from my implementation and thus may have gathered bugs during the extraction, but the concept should still be clear.
$$('img.my_class_name').each(function(e) {
var i = new Image();
i.onerror = function() {
e.src = "error.png";
e.onerror = "";
return true;
};
i.src = e.src;
});
RDDB on Github
Posted by Anthony in Uncategorized on March 6, 2009
I just imported the source code for RDDB from RubyForge into Github. First let me say that the SVN import from Github is hot – they’ve done a really good job. Second, please do not interpret this as an indication that I am going to resume work on RDDB – I’m not (I don’t think). At this point I’m just trying to get all of my projects on Github because it rocks and if I do want to work on any of these projects I can easily and others can fork them, which is really the big win.
Anyhow, if you want RDDB go to github: http://github.com/aeden/rddb/tree/master
Unrelated to RDDB, I also put support into the Moneta S3 implementation for passing :multi_thread as an option to the Moneta::S3.new method.
Have fun!
S3 in Moneta
Posted by Anthony in Uncategorized on March 2, 2009
Sometimes you just have to shave a yak, mostly because it’s fun.
I am working on a project where I need access to a distributed key/value store, in my case S3. I could have just gone to S3 directly but since Yehuda Katz recently announced Moneta, which is a generic key/value store interface I figured I’d give a shot at building an S3 implementation. The result is in my fork on github.
Some points of interest. I refactored the specs so that specs which had dependencies (such as the memcache implementation) are in their own spec files. This makes it much easier to run only a single spec for one implementation at a time, highly useful since some of the dependencies are not things I really want or need on my dev machine, nor do I really want to run all of the specs every time.
The Moneta interface includes the ability to specify :expires_in with a number of seconds as a common means for setting expiration of the key in the store. This is not something S3 supports out of the box. Initially I tried to make it work with HTTP Expires header to no avail. I settled on using a meta header (non-standard HTTP headers used by S3 for client info) to store the expiration date and then I check for the expiration when retrieving the S3 key object. This actually worked quite well even though it did require jumping through some implementation hoops and resulted in more code than I was originally hoping for.
I also had to raise the expiration times just a smidge in the specs since it was quite possible that the test operation would not complete before 1 second would pass and it was causing random behavior. Once I got the expiration working it was golden and all of the specs were passing.
Side note: I don’t usually use RSpec, but the ability to useĀ it_should_behave_like “some spec” is quite elegant.
That’s all for now. Enjoy!



