Rails Development Blog

Wednesday, March 31, 2010

Spotlight can't find Applications

So spotlight decided that it didn't know where any of my Applications
were. This really puts a damper in my work flow so here is the
solution.

In Snow Leopard open Terminal and type:

arch -i386 mdimport /Applications

If you aren't using Snow Leopard just skip the 'arch -i386' and it should work.

This will reindex your Applications directory which will enable
Spotlight to find your Applications again.

Posted via email from Bruce's posterous

Tuesday, March 2, 2010

Passing configuration options to Sinatra Middleware

When you are using Sinatra::Base apps as middleware it's sometimes
necessary to pass in configuration options.

This is how I do it in my config.ru

 1 2 3 4 5 6 
use MockClient do |base|
  base.send(:configure) do
    set :root, __FILE__
  end
end
 

MockClient is the Sinatra app.

You can do anything in the configure block that you would normally do
in the configure block in your Sinatra app. Nice eh?

Posted via email from Bruce's posterous

The elegant way to make square images in Ruby (avatar style)

The problem: we have an image and you want to make a smaller square
version of it. We want the smaller version to cropped so that the
image fills the square.

For example you have this:

And you want to square it and crop off the sides:

And then end up with a resulting square image:

This is a problem that I have solved over and over again. But this is
my favorite solution:

Using mini_magick (http://github.com/probablycorey/mini_magick)

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
width = 200
height = 200
 
mag = MiniMagick::Image.from_file( file_path )
 
mag.combine_options do |i|
  i.resize "#{width}x#{height}^"
  i.gravity 'Center'
  i.extent "#{width}x#{height}"
end
 
mag.write( new_file_path )
 
 
 

Dont' miss the '^' in the resize geometry. That resizes to the
closest dimension while maintaining the aspect ratio.

Note that this works when you want to "magically" crop rectangular
images as well.

Posted via email from Bruce's posterous

Saturday, February 27, 2010

Getting started with AMQP and Ruby

OK so I want to see the most simple basic example of using a message
queue from Ruby.

Install the rabbitmq server and the amqp ruby bindings.

brew install rabbitmq sudo gem install amqp sudo gem install carrot

(I use homebrew http://github.com/mxcl/homebrew for package management on OSX.)

Now start the server.

rabbitmq-server

Create a server.rb that handles the messages.

 1 2 3 4 5 6 7 8 9 10 11 
require 'rubygems'
require 'amqp'
require 'mq'
 
AMQP.start(:host => 'localhost' ) do
  q = MQ.new.queue('tasks')
  q.subscribe do |msg|
    puts msg
  end
end
 

This server is pretty basic it handles the messages and prints them
out as it gets them.

Now lets create a client.rb that sends messages to the queue.

 1 2 3 4 5 6 7 8 
require 'rubygems'
require 'carrot'
 
q = Carrot.queue('tasks')
 
10.times {|x| q.publish('Message number ' + x.to_s) }
 
 

Note that I used the 'carrot' library. Carrot makes it easy for me to
publish to asynchronous queues from synchronous processes like Sinatra
or Rails.

Now run the server in one terminal.

ruby server.rb

And run the client in another window.

ruby client.rb

You should see this show up in the terminal where you ran server.rb :

Message number 0 Message number 1 Message number 2 Message number 3 Message number 4 Message number 5 Message number 6 Message number 7 Message number 8 Message number 9

There you have it. A very simple example.

Now if you want to send structured data, you may want to try using the
JSON library to serialize the data you want to send.

Posted via email from Bruce's posterous

Tuesday, February 23, 2010

Getting PeriodicalExecuter to not depend on Prototype.

I have become accustomed to the PeriodicalExecuter that comes with Prototype.js.

So I made it framework independent.

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 
  // This is a port of the prototype.js PeriodcialExecuter
  // This should be framework independent
  // Valuable if you like to/have to program in jQuery
function PeriodicalExecuter(callback, frequency) {
  this.callback = callback;
  this.frequency = frequency;
  this.currentlyExecuting = false;
  this.registerCallback();
}
 
PeriodicalExecuter.prototype = {
  registerCallback: function() {
    var me = this;
    this.timer = setInterval(function() { me.onTimerEvent() }, this.frequency * 1000);
  },
 
  execute: function() {
    this.callback(this);
  },
 
  stop: function() {
    if (!this.timer) return;
    clearInterval(this.timer);
    this.timer = null;
  },
 
  onTimerEvent: function() {
    if (!this.currentlyExecuting) {
      try {
        this.currentlyExecuting = true;
        this.execute();
      } finally {
        this.currentlyExecuting = false;
      }
    }
  }
};
 

Posted via email from Bruce's posterous

Sammy a tiny MVCish Javascript framework (think Sinatra)

For a client side javascript framework it looks like Sammy is the bomb
digitty. It's just the right size.

It's modeled after Sinatra.

http://code.quirkey.com/sammy/index.html

If you don't know Sinatra:
http://www.sinatrarb.com

Posted via email from Bruce's posterous

Monday, February 22, 2010

A brilliant way to do Templates in javascript.

This comes from John Resig author of jQuery.

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 
// Simple JavaScript Templating
// John Resig - http://ejohn.org/ - MIT Licensed
(function(){
  var cache = {};
  
  this.tmpl = function tmpl(str, data){
    // Figure out if we're getting a template, or if we need to
    // load the template - and be sure to cache the result.
    var fn = !/\W/.test(str) ?
      cache[str] = cache[str] ||
        tmpl(document.getElementById(str).innerHTML) :
      
      // Generate a reusable function that will serve as a template
      // generator (and which will be cached).
      new Function("obj",
        "var p=[],print=function(){p.push.apply(p,arguments);};" +
        
        // Introduce the data as local variables using with(){}
        "with(obj){p.push('" +
        
        // Convert the template into pure JavaScript
        str
          .replace(/[\r\t\n]/g, " ")
          .split("<%").join("\t")
          .replace(/((^|%>)[^\t]*)'/g, "$1\r")
          .replace(/\t=(.*?)%>/g, "',$1,'")
          .split("\t").join("');")
          .split("%>").join("p.push('")
          .split("\r").join("\\'")
      + "');}return p.join('');");
    
    // Provide some basic currying to the user
    return data ? fn( data ) : fn;
  };
})();
 

Storing the templates in script tags in the head of the document is
just beautiful.

Original post is here:
http://ejohn.org/blog/javascript-micro-templating/

Posted via email from Bruce's posterous