Archives
Tags
Multiple before/after model filters with Merb
This evening I found myself in a position in which I wanted to execute several "before" methods hooks/filters on a DataMapper model in a Merb application. I had a look around in the source and API docs, but couldn't really find a nice way of doing it (even though having every hook on a seperate line is not such a bad thing in terms of readability, I suppose).
Anyway, I came up with the following mixin for Extlib (which houses DataMapper's before/after hook functionality):
module Extlib
module Hook
module ClassMethods
alias_method :singular_before, :before
alias_method :singular_after, :after
def before(target_methods, filter_methods = nil, &block)
insert_multiple_hooks "before", target_methods, filter_methods, &block
end
def after(target_methods, filter_methods = nil, &block)
insert_multiple_hooks "after", target_methods, filter_methods, &block
end
private
def insert_multiple_hooks(context, target_methods, filter_methods, &block)
targets = [target_methods].flatten
filters = [filter_methods].flatten
targets.each do |target|
filters.each do |filter|
send("singular_#{context}", target.to_sym, filter.to_sym, &block)
end
end
end
end
end
end Hopefully it is fairly straight-forward. Basically, it will allow you to pass an array to the before and after methods. For example, you can do this:
before [ :create, :update ], [ :set_filename, :set_filesize, :generate_thumbnail ]
Which is equivalent to:
before :create, :set_filename before :create, :set_filesize before :create, :generate_thumbnail before :update, :set_filename before :update, :set_filesize before :update, :generate_thumbnail
I got it loaded into my Merb app by creating an app/lib directory and requireing the contents of it from the config/init.rb file.
Witchhammer 1.3, available now!
After I released Witchhammer 1.0, my bandwidth usage went up 3,000%. My logs are telling me that over 500 people have already downloaded the extension!
In any case, I decided that I could make an enhancement or two. Namely:
- Full support for both band and album/release searching
- Better handling of AJAX requests (as metal-archives is often down or slow)
It still works in the same way as before, although there is now a submenu available that allows you to choose to search for either bands or albums. The results are aggregated and displayed efficiently (instead of simply redirecting you to a webpage).
You can download the extension here (it will overwrite v1.0): http://www.andrewbuntine.com/firefox/witchhammer-1.3.xpi
It's also available at Firefox Addons and Softpedia (thanks to whoever submitted it there!).
Note, you will need Firefox 3.0 or above -- so make sure you upgrade if you need to.
Please let me know if you find any bugs or areas for improvement. Thanks!
An improved shell script for Svn resetting
A few weeks ago I posted a simple script for resetting an Svn repository to an earlier revision (and recording it as a new revision, so the reset is resettable!).
I decided it was totally shit, and so, I have simplified it a little bit:
- You no longer have to pass in the URL for the central repository. The script will figure it out for you.
- The working copy (second) argument is optional. It defaults to the current directory.
Anyway, here it is:
if [ $# = 0 ]
then
echo "Resets an Svn working copy to an earlier revision."
echo "svnreset <revision> [PATH...]"
else
dir=$2
: ${dir:="."}
# Find repository URL using AWK.
repo_url=`svn info $dir | awk '/^URL: (.+)$/ { print $2 }'`
# Update working copy, merge into older revision, commit with message.
svn update $dir
svn merge -rHEAD:$1 $repo_url
svn commit -m "Reverted back to r$1"
fi
exit 0Call it like this (with an appropriate revision number): $ svnreset 1242
Witchhammer, a Firefox extension for metal warriors
NOTE: My latest version is available here: http://andrewbuntine.com/articles/2008/11/27/witchhammer-13-available-now
Record Collecting is a hobby of mine. Well, perhaps an obsession. Infact, I feel somewhat comfortable in claiming that I have the best collection of obscure Czechoslovakian Heavy Metal LPs in Australia!
One of the best general resources for confirming information about bands (or discovering new ones) is undoubtedly The Encyclopaedia Metallum (or simply "Metal Archives" to most of us). When browsing a tradelist, distro listing, EBay, etc I find myself continuously jumping to said website. So I thought:
Wouldn't it be cool if there was a Firefox Extension that allowed you to simply highlight a bands name (or keyword) on a webpage, right-click, select "Find at Metal Archives", and be presented with a nice listing of the results?! You could then select the results you wanted to see, and click "Ok". A new tab, linking to the bands profile, would be opened for each.
So this is exactly what I did. I decided to name the extension Witchhammer; a reference to the cult Czech band, Törr.
Witchhammer is currently in sandbox at the official Mozilla Add-Ons website, and should be available for download there soon. In the meantime, you can download it here: http://www.andrewbuntine.com/firefox/witchhammer.xpi
You will need Firefox 3.0 or above. I developed it on Linux, but have tested on Windows XP and found no problems. It also works fine on Mac OSX.
If you find any issues or areas for improvement, please feel free to email me!
Enjoy, thrashers!!
Freyja, available now!
My first Firefox extension, Freyja, is now available for download! See my first post for an introduction to the functionality and inspiration of this work.
Freyja is currently in an experimental state, and being my first attempt, may not be 100% stable on all operating systems (tested on Ubuntu 8.04, Fedora 8, Windows XP).
To use it properly, you will need Firefox 2.0 or above (FF3 is recommended), access to an SMTP server (Gmail is supported!) and the Python interpreter (comes bundled on most Linux distro's and Mac OS X).
MS Windows users -- the extension will work for you if you have Python installed on your machine.
Direct download: http://www.andrewbuntine.com/firefox/freyja.xpi
I would really appreciate it if someone could download it, test it out with their own email address(es), and let me know if you run into any problems. Any feedback at all is appreciated!
General usage is as follows:
- Go to Tools > Freyja
- Add a few URLs to the Websites list
- Add one or more email addresses to the Email list
- Go to Preferences and set your email information (example: Gmail password, email address, full name)
- Browse to a disallowed URL (that you just set)
- Look in the status bar for the "Sending email..." message!
Freyja: A firefox extension for porn addicts
A while ago I stumbled upon one of the weirdest/coolest/most original reasons for abandoning Linux I have seen in a long time. Check it out here: http://ubuntuforums.org/showthread.php?t=878991
Basically, the guy is heading back to Windows due to a massive addiction to pornography. He says there is not enough pornography-filtering software available for *nix systems.
Perhaps he is right? I'm not sure. But it sure is very original!
At one point in the post, he makes mention of an O/S tool that scans your browsing habits and threatens to email a person of your choosing. I thought to myself that such an idea is great for a Firefox Extension!
So I sat down and started hacking on an extension that works in the following way: The user (adictee) preconfigures the extension with a list of the websites he/she visits in order to "fill their need" and provides one or more appropriate email addresses (their mother, girlfriend/boyfriend, etc). From here, the extension silently pays attention to the users browsing and, in the occurence of the user browsing to a "disallowed" page, the supplied individuals are instantly emailed informing them of what their son/partner/whatever is up to!
I don't believe in censorship or content filtering, and so the pages will be completely available to the user, but the consequences could be dire (provided they use it with integrity!).
I have basically finished the extension and should have it packaged up and ready for download within the next day or so. I will give out more specific details (including full source code) at that time.
Also, I have decided to name it "Freyja"; a not-so-accidental reference to the Norse Goddess of Fertility.
Now there is no excuse, porn addicts all over the world can feel comfortable using free software!
Resetting your SVN repository to a previous revision
NOTE: See the improved version of this shell script here: http://andrewbuntine.com/articles/2008/11/26/an-improved-shell-script-for-svn-resetting
For the majority of the year, I have been using Git as my preferred version control system. Recently, however, I have been using Subversion on some older projects at work.
One thing I have noticed is that the "revert" command seems to be oddly named. It's more of a svn-merge-and-undo. Maybe it's just me?
In either case, I wrote a little shell script that will act more like the Git-equivelant by allowing you to forcfully revert back to any previous revision of your project. It looks like this:
if [ $# = 3 ] then currdir=`pwd` cd $2 svn update svn merge -rHEAD:$1 $3 svn commit -m "Reverted back to r$1" cd $currdir exit 0 else echo "svnreset needs three arguments!" exit 1 fi
Save it as "svnreset" and call it like this (the period is intentional):
$ svnreset 200 . svn://server/project/trunk
That will reset your project to r200, provided you are currently in the repositories root directory. Note also, the changes will be committed as a new revision, so if you make a mistake, just reset the reset!
I suggest that you create a symlink to /usr/bin so you can just call it straight from the terminal whenever you need it.
Loading your Rails App independently
Yesterday I found myself in a position where some horrendously ugly code just became too much for me to handle. To cut a long story short, I needed to decouple a custom library from ActiveRecord. How did it get there in the first place, you ask? Please don't. It's a tragic story...
I set up an experimentation environment (in seperation from my working app), but of course, I needed to temporarily have full access to my models, controllers, etc, etc. Now, I use version control (who doesn't?) and I could have simply branched off and started hacking. But for shits-and-giggles, I decided not to.
I played around for a while and, as it turns out, it's very easy to load your application into an otherwise independent Ruby script. All you need is:
ENV['RAILS_ENV'] = 'development' require '/path/to/your/rails/app/config/environment'
Easy!
As a final note, it's worth pointing out that this is something that you should basically never need to do. If you simply want to automate something or perform an application-specific task, I highly recommend you write a raketask instead.
Hackers Hummus
I like to cook. I am shithouse at it.
With that said, I must mention that my recipe for easy Hummus (a Middle Eastern dip) is a fantastic addition to a late night hack-fest.
You will need the following:
- A Large handful of Garbanzo beans (chickpeas)
- Three tablespoons of Tahini (the sesame seed equivalent of peanut butter)
- Dash of Lemon Juice
- Two crushed garlic cloves
- Salt (must be Sicilian volcanic salt, or you will die instantly)
The salt is optional. I generally don't use it, but feel free.
The method is very straight-forward. So easy infact that I am going to write it in Ruby (assuming my ingredients hash already exists):
ingredients[:garlic].crush! bowl = Bowl.new bowl.add(ingredients) bowl.mash_contents_with(:potato_masher) if ingredients.has_key?(:salt) and ingredients[:salt] != "Sicilian Volcanic" puts "You have 4 seconds to live" end bowl.serve!
Yep, that's it. You just take all of the ingredients into a bowl and mash them up, slightly adding more of a particular ingredient until you have it just right.
Serve Hummus with dipping bread.
First post!
Welcome to my new blog. I look forward to writing about a whole bunch of uninteresting shit!
I am sure eventually I will get bored or lose motivation, but while it lasts, lets see what I can come up with.
Cheers,
Andy.