X-Combinator

Avatar

making the human scalable

use Quick Look as a Firefox application handler

I really dislike getting screen-shots in the format of power-point files. Thankfully, 10.5’s Quick Look can help me preview power-point files. What I needed was a way to have Firefox automatically Quick Look all power-point files that I downloaded. Here’s how to do it:

Open up Script Editor and paste this:

on open filelist
	repeat with i in filelist
		do shell script "qlmanage -p '" & POSIX path of i & "' >& /dev/null &"
	end repeat
end open

picture-2.png

File > Save As…. Choose “Application” and save it somewhere it won’t move. I choose /Applications/ql.app.

Now simply choose ql.app next time you download a power-point file or set it up in Firefox’s Preferences > Applications.

picture-3.png

qlmanage is the command-line tool to open files in Quick Look. By using AppleScript you are able to create this thin “application” that Firefox will trust and give a downloaded file (As opposed to a shell script, which Firefox will not let you choose as a file handler).

lftp has an option mirror:order

We’ve been using lftp for years here. It’s stable and has a volume of options. I just found out today that lftp has an option to set the order in which files are uploaded if you are using the mirror command. You can set it like so:

set mirror:order ‘*.jpg *.gif *.png *.js *.css * */’

This will send first jpg, then gif, then png files etc. This is very handy if you are mirroring an entire website and you want the images to go live before the html does.

String interpolation is faster than printf in Ruby

Just wanted to make a note, string interpolation is faster than printf in Ruby. Example:

$ irb
>> require 'benchmark'
=> true
>> Benchmark.measure { 100000.times {
  "%s" % ["hello world"] } }.total
=> 0.21
>> Benchmark.measure { 100000.times {
  "#{'hello world'}"} }.total
=> 0.04

ActiveRecord from_xml (and from_json) part 2

This post is an upgrade to the previous post about the unmarshalling of XML and JSON strings into rails objects, with arbitrarily deep object associations. As you may know if you are reading this, there doesn’t seem to be a way in rails to reverse to_xml and to_json when associations are included.

Example usage:

xml = firm.to_xml :include => [ :account, :clients ]  
firm = Firm.from_xml xml

firm.account and firm.clients will behave as intended (by default there does not seem to be a direct way in rails to unmarshall them.)

Here’s how to get from_xml and from_json defined for your ActiveRecord classes:

Create a file lib/extensions.rb with the following code:

module ActiveRecord
  class Base
 
    def self.from_hash( hash )
      h = hash.dup
      h.each do |key,value|
        case value.class.to_s
        when 'Array'
          h[key].map! { |e| reflect_on_association(
             key.to_sym ).klass.from_hash e }
        when /\AHash(WithIndifferentAccess)?\Z/
          h[key] = reflect_on_association(
             key.to_sym ).klass.from_hash value
        end
      end
      new h
    end
 
    def self.from_json( json )
      from_hash safe_json_decode( json )
    end
 
    # The xml has a surrounding class tag (e.g. ship-to),
    # but the hash has no counterpart (e.g. 'ship_to' => {} )
    def self.from_xml( xml )
      from_hash begin
        Hash.from_xml(xml)[to_s.demodulize.underscore]
      rescue ; {} end
    end
 
  end # class Base
end # module ActiveRecord
 
### Global functions ###
 
# JSON.decode, or return {} if anything goes wrong.
def safe_json_decode( json )
  return {} if !json
  begin
    ActiveSupport::JSON.decode json
  rescue ; {} end
end

At the bottom of config/environments.rb, insert the line:

require 'lib/extensions' # custom class extensions

You can name the “extensions.rb” file anything else you want; just update the environments.rb file accordingly.

This is an improvement over the previous post, in that it can work even when your associations use the :class_name parameter, you’re not editing the vendor/rails files directly, and it can deal with hashes of the rails class HashWithIndifferentAccess.

,