Writing command line programs in Elixir: Vowel Counter

Note: This is a very basic example of mix tool and escript utility. One can skip this post if already have an idea about these tools.

Lets write a simple command line program which would take a String as a command line argument and display the number of vowels in it. We'll use Elixir's mix build tool and OptionParser to parse command line arguments.

Lets start by setting up the application:

$ mix new vowel_counter

Open mix.exs, you will see something like:

We'll be using Erlang's escript utility to precompile our app and package our application's code along with its dependencies(no dependencies in this example app) in one single executable. So lets add escript: escript_config to project method in our mix.exs to define our escript configuration, also define a private method escript_config to specify the the main_module. In this case, after these changes our mix.exs would look like:

Lets compile and run our application by running:
$ mix escript.build
$ ./vowel_counter "my test string"

Since we have specified VovelCounter module as our main_module in elixir_config, our app expects and call the main method from this module and pass command line arguments to it. We haven't defined this main method yet, so we should see something similar when we execute our app:

Lets open lib/vowel_counter.ex and define main method as:

Lets re-compile and re-run our application by:
$ mix escript.build
$ ./vowel_counter "This string has 5 vowels."
We should see: "There are 5 vowels"

Your comments/suggestions are most welcome.

Trying my hands on Elixir

Recently, I began spending some of my time with Elixir and started following Dave Thomas' Programming Elixir. Though, I am half way through with the book and still don't have a very good hold on the language but after reading about processes in Elixir, I thought to write something on my own so I wrote a small ping-pong program:

I am pretty sure there is much scope of improvement but this is what I could write as of now. Your comments/suggestions are welcome, kindly add them below.

Custom model callbacks in RubyOnRails

RubyOnRails provides us with many model callbacks around the object's lifecycle when object is being created, updated or destroyed. For example: before_create, after_create, before_update, after_destroy etc. We use them to write & run our code around the object's lifecycle by defining a method and associating them as one of the callbacks.

But then how can we make a piece of code execute as a callback for any another defined method except create, update, save and destroy? For example, let's say we have a model Article and we want to execute something just before and after an article is being published without hooking into model's before_save and after_save callbacks?

RubyOnRails or more precisely ActiveRecord provides us with a module ActiveModel::Callbacks, which allows us to define and register custom callbacks using define_model_callbacks method. Lets have a look at the snippet below for the above scenario:
Read more

Generate paperclip missing styles on the fly

Like most developers, who use paperclip gem with their RubyOnRails applications to support picture uploading and generation of distinct styles/thumbnails of pictures, for displaying them in various views, we also do the same in a lot of our apps.

After running one such web app in production for 2 years, the app needed to be redesigned. This required us to generate couple more styles/sizes for each uploaded image.

Paperclip provides a method 'reprocess!(:style)' to generate all/one style(s).

Now, when we planned to move this new version of the app to production, we had to generate those missing styles. There were a few thousand thumbnails to be generated, and our estimated time to generate these missing styles was around half an hour. Since we could not have the site available to users without the graphics, we had to put the site in maintenance mode until the new styles got generated. We also realized that as more images get uploaded and used in the system, this downtime if we ever needed to resize graphics again would go up. We could also not do this conversion without taking the site down as more images may get uploaded while we might be resizing using a script.
Read more

Full-Calendar Rails Engine

A couple of years ago we integrated jQuery FullCalendar plugin with a rails backend. The plugin became reasonably popular and was being used in many Rails apps.

Recently we upgraded it to include an efficient full-sized, drag & drop calendar functionality within a Rails Engine and release it as a gem. With Rails 4 deprecating Rails::Plugin(commit) this would come really handy.

It easily blends within a rails application. Provides a great UI experience with the use of AJAX to fetch events on-the-fly for each month and hooks for user-triggered events (like clicking or dragging an event) for an easy event editing functionality. The view can be easily customized using the configurations provided.

A demo version of this engine implementation can be viewed at http://vinsol.com/fullcalendar-demo

Work in progress : Optimizing Recurring Event functionality.

Source code is hosted on github. Feel free to fork and send a pull request etc.