YAML to Ruby hash to CoffeeScript object

In one of my projects I found myself in the odd position where I had data in a YAML file, that needed some processing done on and then being inserted into a CoffeeScript file.

Now I could’ve just done a YAML to JSON conversion, but seeing as I had the intermediate Ruby processing steps and because I really wanted the output to be CoffeeScript as I was likely to have to work and manipulate it further later anyway, and would want to make changes to the CS directly instead of parsing the whole beast again.

So after loading the YAML into a Ruby hash and manipulating it appropriately I needed to do the conversion to CoffeeScript.

This is my solution

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
      defaultProc = Proc.new do |output|
        print output
      end

      #input is a Ruby hash
      #spaces is a prefix string of spaces used for whitespace significance
      #proc acts on the output
      def HashToCS.convert(input, spaces, proc=defaultProc)
        if input.is_a? String
          proc.call spaces + '"' + input + '"' + "\n"
        elsif input.is_a? Array
          proc.call spaces + "[\n"
          input.each do |a|
            convert(a, spaces + "  ", proc)
          end
          proc.call spaces + "]\n"
        elsif input.is_a? Hash
          proc.call spaces + "{\n"
          input.each do |k, v|
            proc.call spaces + "  #{k}:\n"
            convert(v, spaces + "    ", proc)
          end
          proc.call spaces + "}\n"
        else
          proc.call spaces + input.to_s + "\n"
        end
      end

usage:

1
2
3
4
5
    proc = Proc.new do |output| 
      coffee_script_file.puts output
    end

    HashToCs.convert(ruby_hash, "", proc)

I am using it from a rather intricate Thor script to create a data file for my coffeescript app to act on.
More on using Thor to manage intricate application builds in a later post.

Setup Mac for CoffeeScript development

For my pet projects I mainly use CoffeeScript these days.

I recently decided to do some of my mobile app development on my old mac book pro, so after installing Snow Leopard I had to do the following to get started.

Snow leopard comes preinstalled with Ruby, although I would suggest installing RVM, and Ruby 1.9.2 – there are plenty examples of doing that elsewhere. This is not a guide for Ruby development, so I’ll show the minimal required Ruby. Expect a future article on how I use Ruby and the Middleman gem to do mobile app development and using the same source to build and maintain multiple apps that share some core functionality.

First step is to make sure you have XCode installed. and specifically, be sure to have the version that includes iOS SDK. This would most likely mean that you have to download the whole 4.2+Gb. I tried downloading it from the Apple dev center a few times, only to have the download fail after 1.5~Gb . I then resorted to downloading a torrent. If you want to do development for an Apple mobile device, then you need this. You will need XCode regardless, various Ruby, Node.js and other tools/libraries rely somewhat on it.

Homebrew: Is a great package management tool for OSX. We’ll use it to install Node.js, but you’ll use it for loads of other stuff too, once you know about it.

1
/usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"

Node.js : Server-side Javascript.

1
2
brew install node
curl http://npmjs.org/install.sh | sh

CoffeeScript: awesome language that releases the power by taking the pain out of Javascript.

1
npm install -g coffee-script

See if things work.

1
coffee -v

I’ve recently been introduced to the excellent Sublime Text 2 text editor. I know everyone has their own favourite text editor; mine was Textmate. ST2 just does everything so much nicer than any text editor i’ve tried before.

Here’s how to set it up for CoffeeScript editing:
Download it from here

Install it, then in the console:
cd ~/Library/Application\ Support/Sublime\ Text\ 2/Packages/
git clone git://github.com/jashkenas/coffee-script-tmbundle CoffeeScript
ln -s “/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl” /usr/local/bin

I usually use the Railscast colour scheme textmate bundle, or the Twilight colour scheme. In both I just change the comments colour to be more striking.

You can create your own custom build system recipes to compile/translate CoffeeScript file from Sublime Text 2 directly. I don’t currently use this as I prefer to use the console. I might create a build system to use in the future once my project gets a bit more complex.

:franc