Daniel Doubrovkine bio photo

Daniel Doubrovkine

aka dB., CTO at artsy.net, fun at playplay.io, NYC

Email Twitter LinkedIn Github

You know I love coffee. My coffee is supported by Vibiemme hardware and Barista software. We even have interns writing coffee. Yeah, take that corporate internship! How much coffee-script did you write this summer?

One of the annoying things about Barista, the coffee-script compiler that we use with Rails, is that it adds a preamble with a full path to every generated file (something like /home/dblock/bla/bla/bla.coffee). This makes it difficult to force file generation and have those files committed into source control, since they change all the time. I’ve always wanted to make these paths relative, therefore creating the same file contents on different machines.

This will be supported in the next release of Barista, since pull request #54 got merged. You can have completely custom preambles, enabling you to make a path relative or to insert a license. Add the following to config/initializers/barista_config.rb.

Barista.add_preamble do |location|
  "/\* : DO NOT MODIFY - compiled from #{Pathname.new(location).relative_path_from(Rails.root).to_s}\n\n"
end

My first implementation was a bit raw, but then I remembered that Ruby has this amazing thing called blocks. Let’s say you have a method called preamble that is currently a boolean. You can extend it to take an optional block by calling block_given?. Voila – now you can both say preamble = true and preamble do ... that passes some custom code that should get executed to generate a preamble. You can see the final code here. IMO it’s very elegant, beautiful Ruby-magic that enables functions to do extra work when you ask politely.