Follow mekdigital on Twitter

:bad => to :good

Screen_shot_2012-05-09_at_5
It's that easy! ;)

:(\w+)\s*=>\s*

for

$1:

published on 9 May

Comments [0]

Untitled

What about renaming all those ugly camelcased files?

 

#!/bin/bash
for i in *.mp4 ; do
  y=$(echo $i | tr '[A-Z]' '[a-z]');
  mv $i $y
done
published on 21 Feb

Comments [0]

batch Image resize?

Today I almost opened photoshop to resize a bunch of images, but then I rememebered: magick!

#!/bin/bash
for i in *.jpg ; do
 convert "$i" -resize 720x720 "$i"
done
published on 21 Dec

Comments [0]

Another Lesson Learned

Don't ever assume that Rails 2.3.5 and 2.3.9 are interchangeable just because for months everything worked perfectly in development, test, qa and staging...

Production Day will teach you the hard way!

published on 19 Dec

Comments [0]

All I got was a 404! (URI::NotFoundError) trying to bundle rails

Today I was playing around with R 3.1 and suddenly I wanted to submit a patch. Following the guide I tried to 'bundle' to be able to run the tests. OOPS!

Well, if your gem building joy is interrupted by a 404 trying to fetch the ruby source mentioned in your RVM, (for me Looking for http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p274.tar.gz), you can use a dirty workaround and install the specific gem with a flag:

gem install some_gem -- --with-ruby-include=/your_home/.rvm/src/ruby-1.9.2-head

 

/peace

published on 12 Sep

Comments [0]

An autocomplete middleware for Sinatra & jQuery

An autocomplete script is based on a dom element that uses javascript to generate asynchronous requests to a remote service and parse the response to offer matching choices to the user, if any is found.

Let's use a trivial Player model with a username method and build an autocomplete field for it.

View

we need to specify three things for the input field: it must have `autocomplete` as class attribute so that jQuery will be able to attach events to it, and specify the ruby class and method (klass, method) we want to invoke. It's also a good idea to disable the browser autocomplete feature that would interfere with our autocomplete. Specifying klass and method may seem redundant as they could appear in the ID, but it's not: player_picture_count is an example of ambiguous ID. 

Javascript

the autocomplete javascript attaches and handles keyboard and mouse events for all the input fields of `autocomplete` class and the associated autocomplete list div. In our example, the player_username field will generate requests to /autocomplete/player/username/query when the length of the input is at least 2 and the ASCII code for the pressed key is between 45 and 106 (letters, numbers and some symbols), or a backspace. Arrows, clicks and tabs are used to interact with the autocomplete list.

Middleware

the middleware responds to autocomplete requests with some magic*. It's important to whitelist every single class and method that are allowed to avoid issues: the ensure_autocompletable will halt the execution otherwise. There are two possibliy configurable elements in the query: the search by wildcard - that is usually a use case expected by the user - and a limit of 10 items to return. Also, generating HTML here... TODO: refactor.

Partial

the js partial rendered by the autocomplete simply populates the autocomplete div with the result from the query and attaches some event handling for highlighting the currently selected item in the autocomplete list.

Main App

That's about it!
published on 18 Jul

Comments [0]

using WillPaginate with Sinatra

The use of the will_paginate gem with Sinatra is trivial, all you need to do is instruct your ORM about pagination and make the helpers available to the views. In addition to this, you also need to implement the url method for the LinkRenderer class. This snippet assumes the use or ActiveRecord :)

This is what I was thinking... Google it, find it, put it in your code and it should work..

Run this snippet and you will see what we're talking about!

This seems to be working:

 

 

published on 2 Jun

Comments [0]

Hash_All is now available as a Gem

The simple logic of the initial library is now much simpler. I am now using this gem in all my projects and it saves me a lot of lines of code!

Comments and suggestions are more than welcome!

 

published on 31 Mar

Comments [0]