Just wanted to quickly share something on ES6 and CoffeeScript I recently came across. Those who love CoffeeScript being a non-javascript developer like me, may find it interesting to know more about ES6 features and CoffeeScript.

This article on Toptal on what is new in ES6 the author provides a wonderful introduction on both of the worlds.

Advertisements

Replica set involves at least 3 instances of Mongo running with respect to voting so that any one server can take over as master in case of failure. So first thing is to install mongo in 3 servers in your network which is a good idea for redundancy. You can easily install mongo by following instructions at mongodb documentation site. In this blog we will be deploying mongo db replica set in linux environment.

Once you have completed installation of mongo, it’s time for some configuration tweaks before you start the databases. But first generate a key file in your master instance for authentication so that the 3 mongo instance can talk to each other securely. 

openssl rand -base64 741 > /etc/mongo.key

Copy the key file in your other instances in same location.

Now edit the following in all of the 3 mongo DB /etc/mongo.conf

keyFile=/etc/mongo.key
replSet=rs0 #[unique name for your set]
rest=true # if you want rest api to view status of your mongodb

Note: auth=true only works for single mongodb instance authentication, keyFile is required for replica or sharding authentication setup.

Now start your mongo db process

service mongod start

start interactive mongo to initiate the replication and setup replication configuration

mongo
>rs.initiate()
# will return no configuration found
> config = {_id: rs0, members:[{_id:0, host: 'ip:port'},{_id:1, host: 'ip:port'},{_id:2, host: 'ip:port'}]}
>rs.initiate(config)
# will return success status
> exit

that’s it! you have setup mongo DB replica set with authentication.

I had been really lazy to write this blog to finish off the thor application example. So I’ll just go right to the application, it’s nothing complicated. This application intends not to tweet actually in twitter but to demonstrate how thor tasks work.

So where I left from last post add a new folder bin in your application root and create a file tweet. This is going to be our executable file which will allow us to do something like,


tweet post hello

and will print You tweeted: hello.

Put the following in your bin/tweet file,


#!/usr/bin/ruby

require "tweet"

Tweet.start # this registers the thor class as task

Now install build and install the gem by executing ‘rake install’ command in application root. It will install the executable as well. So that’s it, the code can be downloaded from https://github.com/mftaher/Tweet-Thor. You can use it as a basic structure to create your own executable thor tasks.

Note: Before you install or build the gem always commit your git changes.

Ruby is a a wonderful language, you can never stop praising what it lets you do and help you create great applications. To eliminate the need to copy my initial application structure every time I decided to write a scaffolding application in ruby that will give me an initial structure with necessary files in it just like rails generator (I use Sinatra micro web-framework for ruby web development).

I had been researching many options out there to accomplish this and ruby gives you not only wide range of options but also quickest turn around time for a command line application. To name a few the ruby core OptionParser is a powerful class to begin with, there is also Trollop, of course rake and there’s Thor that replaces rake, sake and rubigen.

A great advantage with Thor is, it documents your command line tool as you develop. Thus makes it easy for the developers create several tasks with plenty options and documenting at the same time. So I thought this reason is good enough to build my application using Thor then the others.

Instead of doing a hello world application, I’m going to demonstrate a simple tweeting application using thor. First you’ll need to have a structure of your application you can easily get it by doing:

bundle gem tweet

You will get the default app structure provided by bundle which can be used to publish your own gem. Then create and edit tweet.thor in your project lib diretory:

class Tweet > Thor
  desc "post MESSAGE", "Tweet message in your command line"
  def post(message)
    puts "You tweeted: #{message}"
  end
end

You should be able to see the task list typing `thor -T`.

To make the command available in your terminal, we’ll cover that in next blog post when we will actually post a tweet.

Yes, I’m getting very intoxicated with coffescript as I’m writing this yet another blog on coffeescript. It’s because coffeescript has made my life easier while I’m writing javascript whether applications or libraries or RIA. Here, I’m going to share some useful resources that might help everyone who are interested in learning coffeescript.

First, starting with what is coffeescript and an detailed overview blogged by krawaller, the writer has given a very good overview on coffeescript which I’m sure will help beginners. This article has referenced links that any new beginner can treat as a learning resource.

Second, coffeescript site. http://www.coffeescript.org. it has very good tutorials an online compiler and many more useful code snippets.
Third, a conversion tool js2coffee or coffee2js http://www.js2coffee.org

Enjoy! Coffee!

I have been using coffeescript lately a lot to build a javascript library. The experience so far has been great as far as the complex programming I’m doing instead of using javascript. Coffeescript does the conversion for my code. I’m using jquery with coffeescript which is really easy to use nothing complex at all.

Here’s how I do it

$ ->
  $(some_link_id).click () ->
    # do something

which translates to

$(function() {
  return $(some_link_id).click(function() {
    // do something
  });
});

If you are going to use underscore you can just use as you would in your javascript functions i.e

_.map () ->

Yeah it’s that simple.

The coffeescript author has a coffee port of underscore which you can use directly if you are interested.

Mostly I have enjoyed using the loops in coffeescript since I’m more familiar with ruby syntax. It follows kind of the same way as I would do in ruby for ‘for loop’ or if using ‘when’

show = console.log # I like doing this to print in console
for code of codes
  show code

Coffeescript has great deal of resource in it’s website www.coffeescript.org. It’s really simple if you have ruby background and even if you don’t it’s just that simple to understand. I’m enjoying writing 300 lines of coffeescript instead of writing 1000 lines of javascript when coding OO style programming which transforms in javascript prototypical programming.

I hope you’ll like coffeescript as much I do. Enjoy!

I have always appreciated the things you can achieve with javascript. The power of javascript has been shown over the years and there’re very few web sites which doesn’t use Javascript today. Although javascript is powerful and great but the syntax and debugging had never been so easy. Writing a object oriented javascript required skill and discipline and lots of patience. And those who are programming javascript kudos because you are all doing great work!

Javascript is an object oriented but a prototypical language where harnessing the power of prototypal inheritance is a little cumbersome. The jQuery library did help us all by taking care of the DOM but it always lacked the structured programming like OOP, it simply isn’t designed to help you organize your code outside of vanilla JavaScript. Then there is Prototype JS framework which  lets you write OOP style dynamic web applications and Moo4q by Ryan Florence also gives you the ability to write OOP style on top of jQuery.

Then in 2009 came Jeremy Ashkenas released Coffeescript, a programming language that transcompiles to Javascript. Writing applications and managing your code can never been easier because of the simplicity used in Coffeescript.  “The language adds syntactic sugar inspired by Ruby, Python and Haskell to enhance JavaScript’s brevity and readability, as well as adding more sophisticated features like array comprehension and pattern matching. CoffeeScript compiles predictably to JavaScript and programs can be written with less code (typically 1/3 fewer lines) with no effect on runtime performance“- [wikipedia].

Speaking of fewer lines of code and simplicity here’s an example of simple Person class in coffeescript

class Person
  constructor: (name) -> 
  @name = name

Defining constructors (functions that get invoked upon instantiation) is simple, just using function named constructor. This is akin to using Ruby’s initialize or Python’s __init__. Which compiles to javascript as this

var Person;

Person = (function() {

  function Person(name) {
    this.name = name;
  }

  return Person;

})();

By prefixing argument’s with @, CoffeeScript will automatically set the arguments as instance properties in the constructor. You can try playing with Coffeescript here. Coffeescript is website a great resource for learning.

Installing Coffeescript:

You can download and install nodejs and then use npm to install coffeescript in your environment with the following command:

 npm install -g coffeescript

You can then type coffee and it will invoke the interactive coffee. Or you can start writing your script in a *.coffee file and compile to js with following command and require in your html file for testing.

 coffee -c somefile.coffee

Then it will generate a samename.js file to require in your html. You can also direclty use coffee files in your html, I’ll show an example in later post.

%d bloggers like this: