Yeoman backbone generator

In my blog post My Javascript workflow I already explained how to setup and use yeoman, grunt and bower for javascript development. So In this post I would like to show how to use backbone generator for yeoman.

Yeoman + Backbone

Scaffold your backbone app

Getting stated a backbone app is always tedious. we need to create all the directory structure, copy all dependency for development and tests, setup tests to run with mocha or whatever you use and also setup a build system. Here with yeoman, we are trying to make your life easier. Yeoman will take care of all of the above headaches for you.

yo backbone

Yeoman will create a initial scaffold with a single command.

yo backbone

yo backbone

This command will create the basic app structure and create all the files need to run your app. It copy client dependencies using bower and also install all the npm dependencies used for build the app.

That's it? Just basic scaffold? No!

Yeoman backbone generator is bundled with many sub generators, which helps to create backbone router, model, collections and views.

yo backbone:model todo : Creates backbone model
yo backbone:collection todos : Creates backbone collection
yo backbone:view todo : Creates backbone view and its template
yo backbone:router todo : Creates backbone router

What if I am coffeescripter ?

Its quite simple use --coffee option with the generator. If you created basic scaffold with coffee option then you don't want to use this option on every sub generator for that particular app. Yeoman sub-generators are intelligent enough to detect coffee and create scaffold accordingly.

Different HTML template engines

Yeoman backbone generator supports Lodash, Handlebars and Mustache. Default is set to lodash. if you wanna change it use --template-framework option.

yo backbone --template-framework=handlebars

This command will set the template engine as handlebars and when ever you create a backbone view it create the handlebars template.

Mocha or Jasmine?

You can use either mocha or jasmine as the test framework for the app. Use the option --test-framework option. Yeoman will setup the test environment according to the one you choose. This option is defaulted to mocha.

Found any bug or have any feature request? File an issue on github

Blogged via MarkdownBlogger

Ruby : create classes on fly

This is a simple blog post which demonstrate how we can create ruby classes on the fly.

Using class keyword and Class.new

We can create a ruby class either using class keyword or using Class constructor. Ruby newbies will be only familiar with class keyword, but in order to create a class on the fly we need to use Class constructor

Creating simple ruby class

Here is how we can create a simple class on the fly using Class constructor.
The above code will create a new class when you call the instance method create_class.
In most cases the new class name will be on some variable. So we need to use const_get method to the new class.
Object.const_get("airtel".classify).new.hello_class   
I required "active_record/inflector" to get the method classify. what const_set method is doing is sets a constant to the given object, here sets the class name.
If you want the newly created class to be a part of SomeModule then you need to use const_set on SomeModule like
then while const_get use
SomeModule.const_get('airtel'.classify)  

Creating inherited class

If you understand how to create a simple class, then creating a inherited class is quite simple. You just need to pass the parent class to the Class constructor.

This will create a new class inherited from SomeModule::Test.

self.class.ancestors will print the hierarchy like [SomeModule::Airtel, SomeModule::Test, Object, Kernel, BasicObject].


Am I missed something? Love to hear from you via comments.
Blogged via Markdown Blogger

Short circuit evaluation in ruby

What is short circuit evaluation??

It means in a conditional statement with two conditions the second condition is evaluated only when the first condition is not enough to determine the value of expression
Consider a conditional statement echo "Hello" if a && b. In && condition if the first condition is false (considering a is defined as false) then the result of a && b is always false. So in this ruby won't try to evaluate b.
Short circuit  evaluation in ruby
In the above image when a is false it neither print "Hello" nor raise any error. Means b is not evaluated.
Short circuit  evaluation in ruby
But when the a is true, a is not enough to determine the condition so the ruby will try to evaluate b. But since b is not defined the ruby will raise error saying undefined local variable or method b