Friday, August 24, 2012

Tuesday, March 6, 2012

Automate the process of running the test (watchr gem used with rails and rspec)

I today tried watchr gem while writing my unit test in rails application.It automate the process of running unit test. Watchr automatically run the test for the files which are change. That means if I changed Employee model then it will automaticall run employee_spec.rb . I found this gem very useful as save lot of time for manually run the test.

Installation watchr
Step 1.) Installing the gem
          gem install watchr
If you are using it with rails better bundle install the gem after adding the following code in gemfile

group :development, :test do
  gem 'rspec-rails'
  gem 'watchr'

Step 2.) Create .watchr file in root directory and add the following code:

def run_spec(file)
  unless File.exist?(file)
    puts "#{file} does not exist"
  puts "Running #{file}"
  system "bundle exec rspec #{file}"

watch("spec/.*/*_spec.rb") do |match|
  run_spec match[0]

watch("app/(.*/.*).rb") do |match|
  run_spec %{spec/#{match[1]}_spec.rb}

Step 3.) run the following command:
watchr .watchr

Now you can make change in your code and then automatically respective spec will run.

References :

Tuesday, February 28, 2012

Which mobile platform is the best Native or Web app ?

Mobilize your website: Native App or Mobile Web?

There is mobile buzz going on, every one want that there website should have access to smart phones. It was till last year that only Social/e-commerce sites have mobilized but now because of phenomenon growth of smart mobiles and tablets there is also good growth in enterprise applications. Each sector whether it is Education, Health, Social etc every one is trying to be mobilized.
In mobile world things are not simple as web development as there are lot of different devices  available. There is no standard in these devices and that's also changing at very fast rate. I will give you an example I am working on mobilizing BMC IT Service management. When we started we did a survey about which mobile device is mostly used by our Clients. The number of Blackberry users at that time were huge. So we started development concentrating on Blackberry but then we realized that iphone number has grown at very fast rate and our customers want the product for iphone also. It was good that we designed our API’s and code in good way that it does not take much time to release the same product for iphone also. So coming back to point the mobile world is changing at very fast rate, we can’t decide which platform will lead the future but choosing the right framework and designing good API will always help you.
I got frequently get mail and call by many companies and people asking which framework is good for creating Mobile application or asking which is better Native application or Web application. So just thought to consolidate all the ideas.

To start with we can create  mobile application in three type way:
Native Apps:
These are platform specific application which is coded in platform specific language like Objective C for iphone/ipad, Java for Andorid etc.
Mobile games are develop using this application.

  • This application are reliable, performance is good and very powerful.
  • Can use Device Cabiblity like GPS, phone book, camera etc
  • You can have push Notifications 

  • Take lot of time for Development
  • Creating and maintain cost is very high as we have to code for each platform.
  • Finding language specific resource for this language is big task. As we need dedicate resources which are expert in these languages.
  • Have to submit to Appstore which is a painful process.

Mobile Web application:
These are the simple web application which can be access from each web enable device.
  •   Don’t need for Appstore submission. This is the most painful and time consuming process.
  •  Cross platform- work on all HTML5 devices like iphone, Android and Blackberry
  •  Easy to create and maintain.
  •           Not work well for touch phones( Can use some CSS framework for touch effect)
  •           This application are not very fast and not reliable
  •           No Offline Capability
  •           Can’t use Device Capbilities and push notifications

Hybrid Applcations
These are application which are created with some HTML5 based framework like Rhomobie, phonegap, titanium. These application can access device capability with there API and easy to code.
  • Development is very fast,
  • Cross platform
  • Can use device capability and push notifications
  • If you need any specific feature you have to wait until these framework support that feature.
  • Choosing between these framework is difficult task. Each has its advantage and disadvantages.

So choosing in between depends on your requirements:

Sites like standard e-commerce sites, Social Networking site speed and experience can make a huge difference in the effectiveness of a mobile commerce website or native application. Sites that are slow to load — even by just a second or two — can often lead to users forgoing the transaction altogether. For this type of application I think Native application is best solution.

Sites like Enterprise application, Events, meetups etc were ease and time of development is important  there Hybrid application are best solutions. You can also use device capability, offline capabilities with them. 

If your application is used to check certain information and don’t want any offline capabilities/Device capabilities than Mobile Web application is best solutions.

And most important, design your API in good way so that they can be reused easily.

To know about different framework available follow this link

Also i have presentation in RubyConfIndia 2012 about "Which Mobile platform should i choose?"

Monday, January 9, 2012

Proc vs lamda in ruby

The are two major difference between procs and lambda 

1.) A proc can break the parent method, return values from it, and raise exceptions, while such statements in lambda will only affect the lambda object. 

def foo
  f = { return "return from foo from inside proc" } # control leaves foo here
  return "return from foo"

def bar
  f = lambda { return "return from lambda" } # control does not leave bar here
  return "return from bar"

puts foo # prints "return from foo from inside proc"
puts bar # prints "return from bar"

2.) The other difference is unlike Procs, lambdas check the number of arguments passed.

def some_method(code)
  one, two = 1, 2, two)

some_method({|first, second, third| puts "First argument: #{first} and Second argument #{second} and Third argument #{c. third}"})

some_method(lambda{|first, second, third| puts "First argument: #{first} and Second argument #{second} and Third argument #{c. third}"})

# => First argument: 1 and  and Second argument 2 and Third argument NilClass
# *.rb:8: ArgumentError: wrong number of arguments (2 for 3) (ArgumentError)

We see with the Proc example, extra variables are set to nil. However with lambdas, Ruby throws an error instead.

Sunday, January 8, 2012

rhomobile tutorial and Example

If you are new to Rhomobile and don't know how to start with Rhomobile products. Then may be this blog will helpful to you. I have consolidate all the tutorials and Examples.






I will keep updating this with new example and sources to learn Rhomobile products

Rhomobile Ajax Calls using JQuery Mobile

Note: This will only work on IPhone,Android and Blackberry(version 6 and above) 

There is common scenario to use Ajax with Rhodes but I was not able to find good source to use them so thought of blogging it. I will be using Jquery Mobile framework which is by default with Rhodes.

In this example I will load some text inside a div on clicking of a button.

Step 1.) Create a div and button on a page
<input type= "button" id="some-button"/ name="button" value="Load Value By Ajax">
      <div id= "some-div">

Step 2.) Create a Javascript 

Step 3.) Create a action ajax_method in controller
def ajax_method
    @some_value = "Its all about ruby ---" +@params['some_variable']
    render :action => :ajax_method, :layout => false, :use_layout_on_ajax => false

Step 4.) Create a ajax_method.erb file
<i><b><%= @some_value %></b></i>

It is just a very basic example you can do many more things like error handling, adding loading image etc.

Tuesday, December 27, 2011

Refreshing the column in rails

It may be possible that your database is shared with multiple applications and you data are frequently changing. You can manually refresh the object by:

stock = Stock.find_all
loop do
puts “price = #{price}”
sleep 60