Friday, 3 June 2011

rails - bundle exec



"It’s worth noting that typing in rake foo (or anyexecutable foo) in the presence of aGemfile.lock, and expecting it to execute in the bundler sandbox doesn’t make any sense" - Yahuda


Ex: "rake middleware" should be replaced as "bundle exec rake middleware"



Wednesday, 25 May 2011

Testing your email notifications with MailCatcher.

    Often its hard to test email notifications sent from our application. MailCatcher is a gem which helps you to check those mails sent from your application.
Install the gem as 'gem install mailcatcher' and run 'mailcatcher' on your console. This  will start two servers. One smtp server(on 1025 port), use this server to sent mails. Another server, web server, helps us to view the 'sent' email on our browser 'http://localhost:1080/'. I tested it on Chrome 5.

Tuesday, 24 May 2011

Art of Light: plugin for loading application constants in rails.

Art of Light: plugin for loading application constants in rails.

plugin for loading application constants in rails.

https://github.com/shilesh/app_config


I have created a simple plugin which could be helpful in your rails projects. It is available at https://github.com/shilesh/app_config . This plugin, once installed , loads constants from a config file, while the application gets initialized (server start/restart, rails console etc ...). 

There will be situations when application specific constants are required in our application. Which we could configure as needed.

Summary:
=======
This plugin helps to load values from a config file(yml) file based on the 
environment, with out writing extra code. ie: if you have a config file as 'app_config.yml' and has values in it as title: 'Caption'.The plugin helps you to call as "AppConfig::TITLE". If your config file name is 'ror_values.yml' then the values can be accessed as RorValue::TITLE". In short ::.
 
I have tested against ruby 1.9 and rails 3.0

How to use:
==========
  1. Install the plugin.
  2. This will create a folder 'app_config' inside 'config' folder of your rails application.
  3. A mock config file, named 'app_config.yml', will be created with sample data.
  4. User can edit the file with proper values.
  5. The file name can be renamed
  6. Based on the file name the namescope will be created to access the values( :: ).
  7. Restart the server if you change the file name.
  8. You can expect helpful error messages being raised on invalid access.
  9. When you un-install the files in config/app_config/* will be retained, 
  10. expecting that it would contain useful data. You can manually delete it if needed.
   

    I have tested it against rails 3.0.x and 2.3.8 .

Monday, 25 October 2010

exception handling in ruby

Normally we don't explicitly specify the Exception type while 'rescue' ing it. ex:
begin
  raise 'alert'
rescue => e
  p "Error"
end
=> Error
Everything is fine here, But look at another case,
begin
  require 'IamNotThere'
rescue => e
  p "Error"
end
=> LoadError: no such file to load -- IamNotThere
Never expected ?, Our rescue block didn't handle this case.
begin
  require 'IamNotThere'
rescue Exception=> e
  p "Error"
end
=> Error
Again, everything is safe here.

So, It's always better narrow down our expectation or use 'Exception => e'. Default is 'StandardError'.


Heirarchy:

Exception
  NoMemoryError
  ScriptError
    LoadError
    NotImplementedError
    SyntaxError
  SignalException
    Interrupt
  StandardError
    ArgumentError
    IOError
      EOFError
    IndexError
    LocalJumpError
    NameError
    NoMethodError
    RangeError
      FloatDomainError
    RegexpError
    RuntimeError
    SecurityError
    SystemCallError
    SystemStackError
    ThreadError
    TypeError
    ZeroDivisionError
  SystemExit
  fatal

Tuesday, 20 July 2010

Test ruby after installation

Sounds silly and obvious, But if we miss to run './rubytest' after installing ruby(any software, especially from source) we might land in big trouble. We might get the feeling that it got installed correctly but it may not. A random check in irb could show something like "123.12".to_f => 123123.0 Which is wrong, But ruby was installed fine.

I got the tests ran successfully later and "123.12".to_f gave me 123.12.

Wednesday, 28 October 2009

Rails Scraps

Asynchronous ...mails using workling.

http://playtype.net/past/2008/11/11/sending_mail_asynchronously_in_rails/