I spend a lot of time using the command line, and I find these features particularly useful. This might be old news to some, but they are still immensely helpful. (Also, these are extremely hard to google for, so hopefully this will save you time in more ways than one!) I tried to include as many examples as I could to make these easy to understand.
More →
Making advanced search forms for your backend kinda sucks, right? I ran into this problem with a project and decided to come up with something similar to how Google handles search queries. No dropdowns, no checkboxes, no radios, none of that stuff. I just wanted to be able to type status:active user:rob [term] and get what I asked for. I came up with a pretty nice utility class that can handle some basic query syntaxes and give you back hashes or arrays and the left over query.
More →
I may be a devout rubyist, but I still have a soft spot for WordPress — and maybe even a little for PHP too. One of the things I think is lacking is a good way to manage WordPress updates since, as we all know, WordPress updates constantly. I wrote this quick thor script to help me update to the latest version from the command line so I can do quick version testing. As an added bonus, there is also an export task as well which will simulate doing a wordpress XML export from the dashboard.
So, for all you WordPress guys out there who hate doing manual updates, check this out. It may make life a little bit easier.
More →
There are tons of PHP logging apps out there, but here is my take on it. This is a simple drop-in class that is 1 file and is pretty flexible. In my opinion this is about all that is required of a PHP logging class, and its been working out well for me.
More →
If you look around this site you’ll notice that I’m hosting all my code snippets on GitHub’s awesome gist feature. While this allows me to not maintain syntax highlighter plugins and libraries, as well as lets me change the code snippet without updating the blog is nothing short of awesome. The only thing that kind of sucks is that I/we are losing the ability to have our code be search engine friendly since a gist is a 1-line javascript include. This sucks not so much from the “index me!” perspective, but more of the “if I google a line of code/error, will I find relevant code?”
To tackle this, I ended up with a small project: given a gist ID, generate the embed code along with NOSCRIPT tags so the content is indexable. This will only solve the initial problem, as updates will not propagate to the blog, but… you can always manually update.
More →
This one is a pretty specific hack. I’ve had the need to build mini reporting apps, using Sinatra, that aggregate data from multiple databases and produce a report. This is all fine since ActiveRecord is awesome, but I’ve always missed AR’s query caching ability.
After some digging I found that query caching is only enabled on AR’s Base connection and all other models must use that awkward Model.cache{} construct — which is annoying to use. I’ve hacked up a solution that works, although it relies on :send and instance_variable_set which isn’t very elegant or solid.
For what it’s worth, here is a “simple” way to get Sinatra to use ActiveRecord’s native query caching for more than 1 database:
More →
I use content_for a lot in Rails code, but a large percentage of my projects are done as a simple Sinatra app, so I find myself missing a lot of features that Rails has. One big one was content_for — so I drafted this up to fill the void.
It is similar to how rails 2.x implements it, so in your main layout you can check if that content is there and if so display it, otherwise you can use defaults, or ignore it. This particular gist uses it to create custom sidebars based on what page you are on: if you are logged in, in a particular area, etc.. I like this approach as opposed to having a single sidebar partial with tons of display logic, I just think it keeps thing tidy.
More →
I was creating a project the other day that used anonymous pages for users and I wanted to make it a little less “guessable” but more human friendly than a SHA1 hash, so I decided to make a small home-brew URL shortener similar in format to Tiny URL.
The neat thing about ruby is that when you turn a Fixnum to a String with to_s you can specify the base, and when you turn a String back to a Fixnum you can also specify a base in to_i. This lends itself perfectly to turning (large) IDs into a shortened URL thats easier to remember.
In the first gist you can see how this would work with a model to make simple url shortening in rails — or, you can use randomized values to generate more user friendly “unique” tokens (as opposed to using SHA1 hashes for user confirmation/password reset pages).
More →
I recently had a need to parallel process some tasks for a non-critical app, but really didn’t want it to get out of hand. I wrote this as a simple way to multi-thread without the pain of worrying about synchronizing and queues and being thread safe.
It achieves ghetto thread pooling by chunking the result set into whatever size slice is needed and spins up some threads, once that set is done it will repeat until the data is all processed before moving on.
It ended up working out really well as a direct drop-in, and speeded up the processing greatly. I wouldn’t recommend this for production code, however.
More →
This little script is a convenient way to add paths to your $PATH variable without worrying about duplication. I generally break out my .bash files to be organized across multiple servers/computers and each one seems to need different paths appended — so thats where this little guy comes in handy. Just a simple push_path and no worries.
More →