Archive for April, 2013

ActiveRecord like where for array of hashes in ruby

Edit : Updated the code as per the suggestions made by Masklinn.

Recently I was working on Issue#1 of my Soulmate Rails gem, which is a very simple and obvious optimization that I should have thought of myself. Thanks to lephyrius though for pointing it out. In my search method I was looping through the search results and calling find on each to get it from db. The fix involved to instead use a single where for all ids so that it translates to a single db call. A simple fix as well as an optimization.

However once I implemented it, I notice my tests were failing. On closer inspection i found they are failing because the models I created for testing using supermodel gem, didn’t have a where method. For a moment I thought WTF, and then I headed on to supermodel and looked through the source and to my surprise, it in fact did not implement a where method. So I took it upon myself to do it.

Here’s the generic code I wrote that could be applied to any array of hashes :

Now you can do

And you will get what you’d expect.

I updated supermodel in my fork to support such a where method and have sent the original author a pull request, hopefully it will get pulled soon.

All my tests pass now! Yay! I have released v0.3.0 of Soulmate Rails. Be sure to check it out.

PS : How about a code review ? Is this optimal or can it be improved further ? I’d like to hear thoughts from others.

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

VIM Markify

Recently I picked up some minor configs for ruby from Tim Pope’s vimrc. Here’s the code :

What this does is given the filetype is Ruby, it correctly sets up the compiler and makeprg such that it allows you to run your tests via :make! This is pretty neat I must say :), because it correctly loads the quickfix list with the errors and I can actually traverse them in order and fix them. All this without needing any plugin whatsoever :).

Ever since I started using this, I always wanted to mark the lines with errors / warnings with nice signs to highlight lines with errors instantly after I ran :make or :lmake, something similar to what Syntastic does but not just for syntax errors, actual test run errors. As I searched for similar plugins, although I found a bunch of them, I didn’t find them to be very intuitive or simple enough.

Hence I started exploring through vim’s help in an attempt to trying and understanding how I can tap into quickfix & location lists and process them. As it turns out, it’s quite simple and I ended up developing vim-markify overnight.

I’ve made it quite flexible and it works quite well with all quickfix commands – :make, :lmake, :grep, :lgrep, :grepadd, :lgrepadd, :vimgrep, :lvimgrep, :vimgrepadd, :lvimgrepadd, :helpgrep, :lhelpgrep, by tapping into the QuickFixCmdPost autocmd event which triggers right after you run any of the previously mentioned quickfix commands. Although you can choose to disable the autocmd by putting let g:markify_autocmd=0 in your vimrc so that it doesn’t do this automatically and you can trigger :Markify, :MarkifyClear or :MarkifyToggle commands or their corresponding mappings <Leader>mm, <Leader>mc or <Leader>M which can of course be changed. And I was able to achieve all this under 170 sloc :). A lot more things are configurable, I would encourage you to check :h markify.txt within vim or refer to the code at Github.

Screenshot:
Markify2

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)