Open Source Oriented Architecture

I recently watched this talk by Vicent Marti (Spanish) about microservices and Github. I loved it. It is full of wisdom pills and common sense delivered in a fun and entertaining way.

One of the ideas that blowed my mind was what he called OSOA (Open Source Oriented Architecture). That’s the name he uses for the unnamed Github practice of treating each part of your application like an open source library. It’s a very simple concept with many positive implications in practice:

  • Low coupling. An library any person can use will minimize its dependencies. With low coupling, chances of needing a microservice to address the coupling problem decrease. And it is also easier to extract a microservice if you ever need to.
  • Coding quality. In an open source library, you will want to keep its code in the best possible shape, so that other people can collaborate.
  • Open source management. It implies a maintainer who takes care of the library, a README file, documentation, etc. Things that are very important for internal apps but we rarely do.

Of course, there is no need to release each library as an open source if it doesn’t make sense. He talks about the management approach and the mindset shift. Instead of having internal interconnected applications, each one managed and known by very few people, you have high-quality libraries other people can check and improve. Exactly like open source libraries.

Moved to Jekyll

I moved my personal page from Wordpress to Jekyll. It is exactly what I was looking for: a simple but customizable platform I can manage editing plain text files.

I imported my reading list into the new platform. In wordpress I was using now reading plugin and in Jekyll I wanted to use its collections feature. I exported now reading books table into CSV and used this script to generate Jekyll entries and download covers automatically.

I also wanted to have a mechanism to create new book entries easily. I created this little library to fetch books metadata from amazon and a rake task to generate entries automatically. It downloads book covers and serves them locally, since one of the problems with now reading is that amazon urls tend to change without notice.

I miss writing things from time to time. Hopefully the new platform will help me revamp my personal site.

Forceps: import models from remote databases

I recently released a gem called forceps. It lets you copy data from remote databases using Active Record. It addresses a problem I have found many times: importing data selectively from production databases into your local database in order to play with it safely. In this post I would like to describe how the library works internally. You can check its usage on the README.

The idea

Active Record lets you change the database connection on a per model basis using the method .establish_connection. Forceps takes each child of ActiveRecord::Base and generates a child class with the same name in the namespace Forceps::Remote. These remote classes also include a method #copy_to_local that copy the record and all the associated models automatically.

The main reason for managing remote Active Record classes is that I wanted to use its reflection and querying support for discovering associations and attributes. A nice side effect is that the library lets you explore remote databases in your local scripts with ease.

Defining remote classes and remote associations

The definition of the child model classes with the remote connection is shown below:

def declare_remote_model_class(klass)
  class_name = remote_class_name_for(
  new_class = build_new_remote_class(klass, class_name)
  Forceps::Remote.const_set(class_name, new_class)
  remote_class_for(class_name).establish_connection 'remote'

def build_new_remote_class(local_class, class_name) do
        include Forceps::ActsAsCopyableModel

With this definition, remote classes let you manipulate isolated remote objects. But the inherited associations are still pointing to their local counterparts. I solved this problem by cloning the association and changing its internal class attribute to make it point to the proper remote class.

def reference_remote_class_in_normal_association(association, remote_model_class)
  related_remote_class = remote_class_for(

  cloned_association = association.dup
  cloned_association.instance_variable_set("@klass", related_remote_class)

  cloned_reflections = remote_model_class.reflections.dup
  cloned_reflections[] = cloned_association
  remote_model_class.reflections = cloned_reflections

Cloning trees of active record models

For copying simple attributes I ended up invoking each setter directly. I intended to do it with mass assignment but disabling its protection in Rails 3 is pretty tricky, as it can be enabled in multiple ways. Rails 4 moved mass-assignment protection to the controllers but I wanted forceps to support both versions.

def copy_attributes(target_object, attributes_map)
    attributes_map.each do |attribute_name, attribute_value|
      target_object.send("#{attribute_name}=", attribute_value)

Cloning associations is done by fetching all the possible associations of each model class with .reflect_on_all_associations, and

just copying the associated objects depending on its cardinality. For example: this method copies a has_many association:

def copy_associated_objects_in_has_many(local_object, remote_object, association_name)
  remote_object.send(association_name).find_each do |remote_associated_object|
    local_object.send(association_name) << copy(remote_associated_object)

It uses a cache internally to avoid copying objects more than once.

Handling STI and polymorphic associations

Supporting Single Table Inheritance and polymorphic associations turned out to be one of the most challenging parts. Both features rely on an type column containing the model class to instantiate. This column is referenced in multiples places in the Rails codebase, such as in join queries or when instantiating records.

For example, when instantiating objects from queries Rails uses the hash of attributes obtained from the database. In order to change the type column that method is overriden in remote classes: do

if Rails::VERSION::MAJOR >= 4
    def self.instantiate(record, column_types = {})
    def self.instantiate(record)

def self.__make_sti_column_point_to_forceps_remote_class(record)
  if record[inheritance_column].present?
    record[inheritance_column] = "Forceps::Remote::#{record[inheritance_column]}"


Testing against multiple Rails versions

Testing against multiple Rails versions was far more easier than I expected. I used this approach by Richard Schneeman: using an environment variable to configure the Rails version at the .gemspec file:

    s.add_dependency "rails", "~> #{ENV['RAILS_VERSION']}"
    s.add_dependency "rails", "> 3.2.0"


set the target versions in travis.yml:

  - "RAILS_VERSION=3.2.16"
  - "RAILS_VERSION=4.0.2"

The awesomeness of travis will do the rest.


A thing I loved about this project is that I started with a very simple idea without knowing if it was going to work with real-life complex models. I just wrote a very simple test and handled more and more cases incrementally. It ended up being more complex than I expected but it is still a pretty compact library thanks to the wonders of Ruby, metaprogramming and Active Record.

The code for Forceps is available at Github. Pull requests are welcomed.

Mailgun adapter for Action Mailer

At bandzoogle we wanted to use Mailgun as our mail provider for sending emails. Mailgun offers 2 interfaces: SMTP and an HTTP api. If you want to use mailgun in a Rails app via Action Mailer, the only alternative you can use out of the box is the Action Mailer SMTP adapter.

The SMTP approach works great unless you want to send mail in batches. For sending emails in batches with mailgun you must use recipient variables, which are substitutions you want to make based on each recipient. They indicate to mailgun that each message must be individualized and they prevent the to field from containing the full list of recipients.

Providing these recipient variables via SMTP requires you to use a very specific MIME format for wrapping the message. Doing that with Rails is not trivial, as it requires you to use the internal objects of Action Mailer to form the custom MIME. I didn't manage to make it work, even replicating the documented format, and Mailgun support couldn't help me with this approach in Rails.

So I decided to go with the HTTP API, which is much better documented and much easier to use for setting recipient variables. I created an adapter named mailgun_rails:

  • It lets you use plain Action Mailer for sending emails with Mailgun.
  • It supports Mailgun specific features like recipient variables or custom variables that will be received via Mailgun webhooks.
  • It supports sending HTML messages, TEXT messages or both depending of how the Action Mailer message is composed.

An example of usage:

email = mail from: '',
             to: ['', ''],
             subject: 'Hey, this is a test email'

email.mailgun_variables = {name_1: :value_1, :name_2 => value_2}
email.mailgun_recipient_variables = {'': {id: 1}, '': {id: 2}}

You can read more about the adapter on github.

On better information habits

I have recently read The information diet. It is a book about information consumption. It makes an analogy with food that works pretty well: in the same way that healthy eating habits are good and needed in a world of food choices, good information habits are necessary in a world where information is everywhere.

The Information Diest Book Cover

The book contains great pieces of advice. Some ideas I loved are:

  • Information overload is a wrong concept. Information is not requiring you to consume it.

  • There has always been more human knowledge that any human could consume. The problem is that today it is all at the tip of your fingers.

  • Information should be consumed consciously. This implies consuming exactly what you want to consume when you want to consume it. The author even recommends scheduling your information consumption.

I have reviewed many times the approach I use for consuming technical information. In fact, I bought this book because I wasn't quite happy with the system I had in place:

  • I was receiving too much information

  • I was exposed to a lot of noise

Too much information

I have always tried to be selective with the sources I was subscribed to. But at the same time, I wasn't comfortable with the idea of missing interesting stuff. The result is that I was subscribed to a lot of sites that published great content. This made it difficult to read all the new content periodically. This kind of stressed me out and prevented me from enjoying the things I read.

So I decided to reduce drastically my number of subscriptions. I reduced my subscriptions from 24 to 5:

But more important that the specific sites themselves, which for sure will change with time, I clarified what I wanted from now on:

  • Keep a really small group of subscriptions.
  • Carefully crafted publications. Their authors work hard to produce and select high-quality content
  • Predictable and slow cadence (weekly or monthly)

A nice side effect of these specific publications is that I didn't need an RSS reader anymore. Just plain old mails and a filter to forward them to my Pocket account does the job perfectly.

In addition to these subscriptions, I love watching random screencasts from Peepcode, CleanCoders and Railscasts.

Finally, I think that technical books should form the basics of my education. In fact, reading books requires more time and effort that reading articles or watching screencasts. So freeing time for doing it was very important for me.

Not surprisingly, with the exception of the weekly newsletters, the contents I consume are not free. To me this is logical. Producing high-quality content is incredibly hard. When a publisher depends on ads, it is very easy that it ends up favoring volume and frequency, two things I really want to avoid.

Too much noise

I call noise to the pieces of information I spend time consuming that are irrelevant to me. It took me a lot of time realizing I had this problem and even more time taking measures against it. The source of my noise was basically Twitter.

Despite of all my efforts, I have failed to make Twitter a valuable information-consumption tool for me. At the end, I realized that the problem was that I was trying to use Twitter for something it was never intended to be.

One of my favorite parts of the book is when the author synthesizes his research on neuroscience applied to the way we consume information. At some point he talks about a neurotransmitter called Dopamine which is in the heart of brain stimulus reinforcement. In words of the author:

Dopamine makes us seek, which causes us to receive more dopamine, which causes us to seek more.

As a species, dopamine is good. It helped us to find acquire knowledge and innovate. With abundance of information and multiple notifications in place (emails, mobile phones, social networks, blogs'), dopamine is bad as it puts us in a loop where we can't focus on a given task at hand.

The book doesn't mention Twitter specifically, but when I learned about dopamine I realized Twitter was a perfect dopamine generator. So I wasn't surprised to find articles like Twitter as the ultimate dopamine dispensary. I wasn't quite happy with Twitter before learning about dopamine, but learning about it was enlightening.

I found myself reading about political opinions, hobbies, great and not that great technical articles, quotes, thoughts, discussions, travels, news, jokes, rants, food, cool apps' Reading tweets that made me curious and click the associated link or google for more. Things that most of the times were entertaining and, some times, highly educative. But that didn't pass the filter of 'high-quality and relevant to my goals only' that I wanted to use from now on. The dose of narcissism present in Twitter (as in any other social network) doesn't help either.

So what I did was basically to remove Twitter from my daily life one month ago. The only measure I took was removing my personal account from the desktop app (I will use the web app if I need to). I still have the app on my phone as it's a good time killer in some circumstances, but I am really trying to keep these circumstances rare. For example, they don't include reading before going to sleep or pauses when working, which were 2 typical scenarios for me. So far, I am really happy with the new silence gained by turning Twitter off.


The idea of selecting good content to consume is pretty obvious. But in my experience, more important than selecting the best content, is the act of selecting a small group (of great content, of course). I want to manage a volume of publications I can handle effectively with ease. The system won't work for me otherwise. Even when that implies leaving a lot of great content out.

I have never been a fan of social networks, of any kind. Twitter was different in my mind. In fact, my first tweet in March of 2010 was a question about whether Twitter would be useful as a technical information source. It took me more than 3 years to answer myself. To me it isn't, because the noise of the channel is too high for my taste.

Why I love RubyMine

I love RubyMine. I have been using it for one year and a half as my main Rails/Javascript editor.

Before RubyMine I used TextMate. I never felt completely satisfied with it but I liked its minimalistic approach, the rich suite of available plugins and how it looked and felt in general. At some point I decided to learn Vim motivated by the good press it had among Rails developers. I didn't get engaged at all.

I tried RubyMine and felt that I have finally found something that matched my taste and needs.

Things I like about RubyMine


There are 2 things I keep doing when I code:

  • Renaming things
  • Extracting blocks of code as new methods or variables

When I code I am continuously renaming stuff and extracting methods and variables until the code looks clean to me. And I feel much more productive when I can have these refactorings performed automatically. RubyMine offers excellent support for them in both Ruby and Javascript.

Everything is navigable

RubyMine lets you navigate to any symbol by pressing CMD-B (or CMD click with the mouse). This includes Ruby gems in your Gemfile or JavaScript functions included in any file of your project.

I use this feature all the time. I missed it badly when using TextMate, and never felt totally comfortable using it in Vim via CTAGS support.

Keyboard friendly

RubyMine offers excellent keyboard support:

  • Most commands have predefined shortcuts and they appear in the menus so learning them is easy.

  • It has a nice editor with a search box for defining shortcuts for whatever action the IDE offers.

  • The whole environment is designed to be used with the keyboard. For example, when using modal dialogs: CTRL-ENTER will perform the default action and close the dialog (such as commit in Git), and ALT-SHIFT-ENTER will open the alternative options so you can choose other option and close the dialog when selected (such as commit and push in Git).

Selection in scope

When you press CTRL-W in RubyMine you get the word on the cursor selected (as in TextMate). Press it again and the selection will expand to include that word's scope, such as the enclosing string or the method invocation it is part of. Consecutive keystrokes will expand the selection intelligently (enclosing block, method, class, etc.) I use this feature all the time.

Standard, discoverable environment

I appreciate having standard menus with actions and toolbars, having shortcuts displayed in menus and tooltips, having tabs, browsable trees and sheets of properties, having contextual menus properly displayed when I right click on something, and so on'

It is true that once you master the environment you rarely move your hands out of the keyboard. But a I think a nice environment benefits everyone: both power users and newcomers.

RubyMine offers many search options and these are beautifully executed. Coming from TextMate this was another major relief for me:

  • Search in the current file is incremental and highlights matches as you type the search expression, even when you use regular expressions.

  • Global search lets you easily filter by file patterns and folders. It also lets you move through the matches using your keyboard (CMD-ALT-UP/DOWN), invalidating the results in real time if you modify the code.

Split views

Sometimes I like to have both the code and its test displayed in parallel. Splitting views is a feature TextMate users have been demanding for a long time (I think it was finally included in TextMate 2).

RubyMine lets you split the current editor in independent views and keep a set of tabs opened in each view.

Consistency out of the box

This feature is probably the most difficult to describe, but is also one of the most appealing to me. In RubyMine things work in a consistent way. For example:

  • If you have a list of search matches, you can browse them by pressing CMD-ALT+UP/DOWN. If you have a list of failing tests, you can browse them the same way.

  • If you place the cursor inside a hash entry and press ALT-ENTER you are offered the possibility of converting between => and : notations. Do the same inside a block, and you can convert automatically between the braces or do/end syntax. Do it inside a string to convert it to a symbol.

  • Errors highlighting, code formatting, refactorings' they all work the same way whether you are editing Ruby, JavaScript, a CSS or Cucumber. I remember in my TextMate days not being able to pretty format Ruby, while I could do it with JavaScript. To have errors highlighting in JavaScript or CSS, but not in Ruby.

I appreciate the possibility of configuring and extending your editor but I like things working out of the box. And I think that RubyMine is exactly that: an extensible environment that works out of the box. RubyMine itself is a set of plugins running on a JetBrains runtime. And all of these plugins present a high level of quality in terms of usability and IDE integration.

Integrated tools

My initial approach with RubyMine was using it as an editor only, using the Terminal for the rest of work (rails server, git, tests, rake tasks') Eventually I started to use RubyMine built-in tools more and more, and found some of them very useful:

  • Running your server or rake tasks within RubyMine let you click on exceptions traces and browse to the corresponding line in source.
  • RubyMine built-in test runner lets you browse failing tests, listen for changes to re-run tests automatically and filter the output for each test individually.
  • Git support is excellent and it lets you commit, pull and merge changes without leaving RubyMine and without moving your hands out the keyboard.

When I am coding, I keep my RubyMine minimal. I only have the editor visible, without toolbars or other windows. But I use many of the built-in tools when I need them, and I have ended up liking them a lot.

Many other nice things to have

There are many other nice features of RubyMine I use everyday:

  • Clipboard history
  • Automatic completion
  • Live templates (a.k.a snippets)
  • Underline syntax errors in real time in both Javascript and Ruby
  • Possibility of configuring automatic spell checking for comments but not for code
  • Search for usages and references
  • Excellent cucumber support, including generating steps from missing invocations or navigate to steps from their usages in features
  • See the list of last opened files (CMD-E)
  • Navigate editing history back and forward (CMD-ALT-RIGHT/LEFT)

Major flaw of plain editors

Beyond the 'work out of the box' factor, I find another source of pain with plain editors: as long as they don't manage a proper model of the edited code, the kind of assistance they can provide will fall short to my expectations.

I think most of the pain I felt when using TextMate, Vim or SublimeText 2 (my current plain Editor) came from this problem. You can use plugins and external tools to circumvent this limitation (such as CTAGS), but the experience is just not the same.

If the editor doesn't know that the word under your cursor is a local variable defined as a parameter of a method, it won't be able rename it automatically, browse to its definition or include it as a parameter in a new method if you extract the block of code where it appears. I value syntactical tricks and powerful combos, but for my editing happiness I need to get this basic stuff right first.


I don't think there is such a thing such as the best editor for everyone. RubyMine is the best Ruby/Javascript editor for me, and I wanted to expose my reasons, since I feel that this IDE doesn't receive as many praises as it should.

I don't think RubyMine is perfect neither. My major complain is not having much better editor themes. RubyMine's built-in themes or community themes don't feel as good and polished as in TextMate or SublimeText. And of course, RubyMine is heavy in both terms of CPU and memory consumption. They have improved performance a lot in recent releases but it will always feel heavier than any simple editor.