Updated time_diff gem

Now with time_diff gem you can calculate the time differnce between two Time in the formatted form by passing a third parameter to the Time.diff() method.

If you are not passing any parameter then by default it will take the third parameter value as'%y, %M, %w, %d and %h:%m:%s' and it will return the formatted time difference in the form: '1 year, 2 months, 3 weeks, 4 days and 12:05:52'.

For hours, minutes and seconds have two formats, with text and without text. The parameter is with '%h', '%m', '%s' are the formats which won't add text with the value, ie., it return 3 for '3 hours'. The parameters with he formats '%H', '%N', '%S' will return the values with text, ie., '2 hours', '30 minutes'.

If you are using the default format ie., formats with comma seperated, then it will remove the intervals(year, month, ..) which are zero.

If you need only day and hours in the time difference you can specify that by passing parameter in the format of '%d %h'. This will calculate the formatted time diff text only for days and hours.

Read More...

Create ruby gem using Jeweler

Recently I created a ruby gem time_diff using the help of jeweler. Jeweler is a gem using that you can create and manage ruby gems.
First install jeweler:

sudo gem install jeweler

Then create the scaffold for your gem using the command:

jeweler your-gem

This will create two directory test and lib. In lib folder you can create the files for your gem. In test folder you can create the files for tests. The above command will create some other files also like Rakefile, README.rdoc, Gemfile. You can add your dependencies to the Gemfile.

You can create the version using:

rake version:write

Then you can install the gem in your local machine using:

rake install

Then to release the gem to RubyGem:

rake release

You need a RubyGem account to push gem to your account.
For more information visit GitHub

Read More...

time_diff - a gem for calculating the difference between two TIME

Yesterday I created a gem for calculating the difference between two TIME.

When I was working in my office, we found that there is no method to extract the difference between two Time in a useful format like years, month, week, day, hour, minute and second.

time1 - time2 return only difference inseconds.

So I created this gem time_diff which return the hash with difference:

{ :year => 0, :month => 0, :day => 0, :week => 0, :hour => 0, :minute => 0, :second => 0 }

Read More...

State Machine

In some application we need to track the state of a model. For example, a user requested for signup. Admin has to approve the user for the user to login. Admin has the option to reject the user also. So the status of the user model will be in one of the following states [pending, approved, rejected].

The famous plugin used for tracking the state is aasm. You can install Acts as state machine using:

sudo gem install aasm

Then in the user model you have to include the plugin

include AASM

Then specify which field you want to use as the state.

aasm_column :status

Now you can write the states of the model:

aasm_state :pending 
aasm_state :approved 
aasm_state :rejected

You can set initial state using:

aasm_initial_state :pending

Now we need the events to change the state:

aasm_event :approve do
  transitions :to => :approved, :from => [:pending]
end


aasm_event :reject do
  transitions :to => :rejected, :from => [:pending]
end

This is all we need in model.

Now to change the state of the model object you can use:

user.approve!  #To approve the user
user.reject!      #To reject the user

If you want to do something while the state of the model is change from one state to another you can call the method at the time of state change using:

aasm :approved, :enter => entered_to_apporoved, :exit => exit_from_approved

At the time of state definition you can specify the enter method and exit method to call.


Read More...

Heroku

Heroku is a PAAS (Platform As A Service) for Ruby on Rails. You will get a free domain with limited support for your Rails App. After signup to Heroku, you can install the gem:

gem install heroku

Then you can start your application with create command:

heroku create yourapp

Using git commands you can deploy this app to the Heroku:

git add, git commit,

git push heroku master

This will deploy to your heroku repository, git @heroku.com:yourapp.git
And your URL will be, http://yourapp.heroku.com

To do the db:migrate run the command:

heroku rake db:migrate

For more details visit Heroku

Read More...