Thursday, April 14, 2011
LogFileTailer - simple sinatra app
It is never too late to contribute. Following on my year 2009 post - sinatra - web application framework in ruby, I recently pushed that code to github @http://github.com/NiranjanSarade/LogFileTailer
Tuesday, April 12, 2011
Converting Unix newlines (LF) to Windows newlines (CR\LF)
In one of my earlier projects, I faced an issue with newline characters in csv file. The csv file generated on a unix system was uploaded to Windows system through sftp. However, the program on the Windows server was not able to parse the file properly as the csv file contained Unix newlines (LF) instead of Windows newlines (CR\LF) at the end of each line.
The problem is UNIX/Linux uses a Line Feed character (\n) as a line terminator while Windows uses Carriage Return\Line Feed pairs (\r\n).
For those who want to know what is the exact difference between CR, LF and EOL characters, here is a brief description :-
The Carriage Return (CR) character (0x0D, \r) moves the cursor to the beginning of the line without advancing to the next line. This character is used as a new line character in Commodore and Early Macintosh operating systems (OS-9 and earlier).
The Line Feed (LF) character (0x0A, \n) moves the cursor down to the next line without returning to the beginning of the line. This character is used as a new line character in UNIX based systems (Linux, Mac OSX, etc)
The End of Line (EOL) character (0x0D0A, \r\n) is actually two ASCII characters and is a combination of the CR and LF characters. It moves the cursor both down to the next line and to the beginning of that line. This character is used as a new line character in most other non-Unix operating systems including Microsoft Windows, Symbian OS and others.
I was able to resolve this issue with 'Sed' (Stream Editor). (http://www.grymoire.com/Unix/Sed.html)
The command matches the regexp pattern ($ - ending position of line or the position just before a string-ending newline) and replaces it with '\r' (Carriage Return). So the end result would be conversion from '\n' to '\r\n' which will support windows new line format.
The problem is UNIX/Linux uses a Line Feed character (\n) as a line terminator while Windows uses Carriage Return\Line Feed pairs (\r\n).
For those who want to know what is the exact difference between CR, LF and EOL characters, here is a brief description :-
The Carriage Return (CR) character (0x0D, \r) moves the cursor to the beginning of the line without advancing to the next line. This character is used as a new line character in Commodore and Early Macintosh operating systems (OS-9 and earlier).
The Line Feed (LF) character (0x0A, \n) moves the cursor down to the next line without returning to the beginning of the line. This character is used as a new line character in UNIX based systems (Linux, Mac OSX, etc)
The End of Line (EOL) character (0x0D0A, \r\n) is actually two ASCII characters and is a combination of the CR and LF characters. It moves the cursor both down to the next line and to the beginning of that line. This character is used as a new line character in most other non-Unix operating systems including Microsoft Windows, Symbian OS and others.
I was able to resolve this issue with 'Sed' (Stream Editor). (http://www.grymoire.com/Unix/Sed.html)
The command matches the regexp pattern ($ - ending position of line or the position just before a string-ending newline) and replaces it with '\r' (Carriage Return). So the end result would be conversion from '\n' to '\r\n' which will support windows new line format.
Friday, April 8, 2011
Setting up Oracle XE locally for your Rails app
I recently pushed 'database_initializer.rb' to github @http://github.com/NiranjanSarade/oracle_xe_database_initializer. Please check its README for more information.
This can be used to set up your schema and other database objects in your local Oracle XE database. The script assumes that you have following set up on your machine :-
1. Oracle client
2. activerecord-oracle_enhanced-adapter gem
3. Oracle XE (http://www.oracle.com/technetwork/database/express-edition/downloads/index.html)
This can be used to set up your schema and other database objects in your local Oracle XE database. The script assumes that you have following set up on your machine :-
1. Oracle client
2. activerecord-oracle_enhanced-adapter gem
3. Oracle XE (http://www.oracle.com/technetwork/database/express-edition/downloads/index.html)
Thursday, March 24, 2011
Approach for Oracle view unit testing with Rspec
A View in Oracle and in other database systems is simply the representation of a SQL statement (select query). It provides access to a subset of columns from one or more tables.
In a rails migration, when you incorporate a view definition which represents data from multiple tables with multiple joins and multiple conditions, how will you ensure that the view is fetching the data that it is supposed to fetch? How will you test the view logic?
And here, Unit Testing plays a very important role. Today, I am going to tell you the simple approach that we can follow for unit testing the view definition with Rspec.
First, with rails convention, you will create the model class (which maps to the view name) extending ActiveRecord::Base.
You will then set up the test data as you generally set for testing other model methods. (You can use factory_girl or simple model methods)
The purpose is to unit test each and every column data of the view according to the joins and the conditions. It all depends on the view logic that you have written according to your business requirement. The view definition can include decode statements, group by/ order by clauses, oracle aggregator functions e.g. sum, max, etc., function calls from the view sql, if-else logic to name a few.
With this, you have all the unit level requirement specs ready for your view which you can validate by running your Rspecs. Simple but succinct ! Isn't it ?
In a rails migration, when you incorporate a view definition which represents data from multiple tables with multiple joins and multiple conditions, how will you ensure that the view is fetching the data that it is supposed to fetch? How will you test the view logic?
And here, Unit Testing plays a very important role. Today, I am going to tell you the simple approach that we can follow for unit testing the view definition with Rspec.
First, with rails convention, you will create the model class (which maps to the view name) extending ActiveRecord::Base.
You will then set up the test data as you generally set for testing other model methods. (You can use factory_girl or simple model methods)
The purpose is to unit test each and every column data of the view according to the joins and the conditions. It all depends on the view logic that you have written according to your business requirement. The view definition can include decode statements, group by/ order by clauses, oracle aggregator functions e.g. sum, max, etc., function calls from the view sql, if-else logic to name a few.
With this, you have all the unit level requirement specs ready for your view which you can validate by running your Rspecs. Simple but succinct ! Isn't it ?
Monday, March 21, 2011
Installing windows platform specific gem libxml-ruby with Bundler
I was facing some issue in installing 'libxml-ruby' gem on my windows machine with bundler. The entry in the Gemfile was :-
gem 'libxml-ruby', '1.1.3', :require => 'libxml'
This works perfect on unix machines, but fails on windows machine (which is my local development platform) during building native libraries.
The DevKit has already been set up on my machine. So I tried doing gem install first :-

So, I mentioned the platform while doing gem install and it worked as below :-
D:\>gem install libxml-ruby -v=1.1.3 --platform x86-mswin32-60
Temporarily enhancing PATH to include DevKit...
Building native extensions. This could take a while...
Successfully installed libxml-ruby-1.1.3-x86-mswin32-60
1 gem installed
Installing ri documentation for libxml-ruby-1.1.3-x86-mswin32-60...
Installing RDoc documentation for libxml-ruby-1.1.3-x86-mswin32-60...
D:\>
However, with bundle install, it gave the same error on the windows machine : ERROR: Failed to build gem native extension.
I tried specifying Gemfile :platforms option - 'mswin', 'mingw' for Windows platform, but it did not work.
Finally, the :path parameter rescued me out of the pain. With :path parameter, you can specify that a gem is located in a particular location on the file system. The ':path' option requires that the directory in question either contains a '.gemspec' for the gem, or that you specify an explicit version that bundler should use. So the bundler uses the gem from the source specified in the path and it resolved my issue with installing libxml-ruby gem with bundler on windows machine. The steps that I followed :-
(1) Download the libxml-ruby-1.1.3-x86-mswin32-60.gem gem from http://rubyforge.org/frs/?group_id=494 and do gem install
OR
gem install libxml-ruby -v=1.1.3 --platform x86-mswin32-60
(2)From app_root>gem unpack libxml-ruby-1.1.3-x86-mswin32-60
(3) Gemfile.lock :-
gem 'libxml-ruby-1.1.3-x86-mswin32-60', '1.1.3', :require => 'libxml', :path => 'vendor/gems/libxml-ruby-1.1.3-x86-mswin32-60'
(4) Now, when you execute bundle install, it will bundle the gem from the source specified in the :path -
Using libxml-ruby-1.1.3-x86-mswin32-60 (1.1.3) from source at vendor/gems/libxml-ruby-1.1.3-x86-mswin32-60
gem 'libxml-ruby', '1.1.3', :require => 'libxml'
This works perfect on unix machines, but fails on windows machine (which is my local development platform) during building native libraries.
The DevKit has already been set up on my machine. So I tried doing gem install first :-
So, I mentioned the platform while doing gem install and it worked as below :-
D:\>gem install libxml-ruby -v=1.1.3 --platform x86-mswin32-60
Temporarily enhancing PATH to include DevKit...
Building native extensions. This could take a while...
Successfully installed libxml-ruby-1.1.3-x86-mswin32-60
1 gem installed
Installing ri documentation for libxml-ruby-1.1.3-x86-mswin32-60...
Installing RDoc documentation for libxml-ruby-1.1.3-x86-mswin32-60...
D:\>
However, with bundle install, it gave the same error on the windows machine : ERROR: Failed to build gem native extension.
I tried specifying Gemfile :platforms option - 'mswin', 'mingw' for Windows platform, but it did not work.
Finally, the :path parameter rescued me out of the pain. With :path parameter, you can specify that a gem is located in a particular location on the file system. The ':path' option requires that the directory in question either contains a '.gemspec' for the gem, or that you specify an explicit version that bundler should use. So the bundler uses the gem from the source specified in the path and it resolved my issue with installing libxml-ruby gem with bundler on windows machine. The steps that I followed :-
(1) Download the libxml-ruby-1.1.3-x86-mswin32-60.gem gem from http://rubyforge.org/frs/?group_id=494 and do gem install
OR
gem install libxml-ruby -v=1.1.3 --platform x86-mswin32-60
(2)From app_root>gem unpack libxml-ruby-1.1.3-x86-mswin32-60
(3) Gemfile.lock :-
gem 'libxml-ruby-1.1.3-x86-mswin32-60', '1.1.3', :require => 'libxml', :path => 'vendor/gems/libxml-ruby-1.1.3-x86-mswin32-60'
(4) Now, when you execute bundle install, it will bundle the gem from the source specified in the :path -
Using libxml-ruby-1.1.3-x86-mswin32-60 (1.1.3) from source at vendor/gems/libxml-ruby-1.1.3-x86-mswin32-60
Wednesday, March 16, 2011
Moving on to Bundler to manage my Rails 2.3.9 app's gem dependencies
Recently I faced some gem dependency issues on one of our servers where we have a good amount of rails applications. (more Rails 2.3.x apps as compared to a couple of Rails 3.0.x apps). (This was a cruise box used by all apps to execute rspecs and generate metric_fu reports after you commit your code - Continuous Integration concept :-)
My app was using Rails 2.3.9, and due to some reason, I did not get a chance earlier to move my application to use Bundler for managing all the gem dependencies. Finally, I made it :-)
I followed 2 links :-
http://gembundler.com/
http://gouravtiwari.blogspot.com/2011/03/bundler-with-rails-222.html
Since then, I have not faced any gem dependency issues on any of the boxes.
So what are you waiting for ? If you haven't done it till date, do it now ... or you will spend (waste?) time in resolving weird gem dependency issues ... :-)
A good learning for me !
My app was using Rails 2.3.9, and due to some reason, I did not get a chance earlier to move my application to use Bundler for managing all the gem dependencies. Finally, I made it :-)
I followed 2 links :-
http://gembundler.com/
http://gouravtiwari.blogspot.com/2011/03/bundler-with-rails-222.html
Since then, I have not faced any gem dependency issues on any of the boxes.
So what are you waiting for ? If you haven't done it till date, do it now ... or you will spend (waste?) time in resolving weird gem dependency issues ... :-)
A good learning for me !
Friday, March 4, 2011
Avoid oracle sequence during ActiveRecord model record insert
I was writing rspecs for one ActiveRecord model in my project and was connecting to different schema in which the corresponding mapping table was defined. The schema object (tables) definition was a legacy code, what I meant to say is I used the already existing object definition from that schema which was not using oracle sequence. There was no primary key for that table as well.
I was setting up the data for that table in my rspec using ActiveRecord insert.
e.g. AnotherSchema::TableObject.create(:ts => 123, :proj_code => "ABC", ...)
But, in Rails, when you try to insert the record with ActiveRecord Model, it always looks for a sequence, either a default one (Table_Object_seq) or explicitly defined one with set_sequence_name in the Model class.
So, it threw error as expected :-
OCIError: ORA-02289: sequence does not exist: select "Table_Object_seq
".nextval id from dual
I could have defined a sequence on this table only in the test database, but I googled around for a way to avoid oracle sequence in a model and I got this link :-
http://www.dixis.com/?p=127
I followed the suggestion and it worked for me :-)
What I did was :-
Monkeypatching OracleEnhanced Adapter's next_sequence_value method :-
and adding set_sequence_name to the Model class :-
set_sequence_name 'autogenerated'
It avoided the default oracle sequence look up while inserting new records with Rails ActiveRecord methods which solved my problem.
I was setting up the data for that table in my rspec using ActiveRecord insert.
e.g. AnotherSchema::TableObject.create(:ts => 123, :proj_code => "ABC", ...)
But, in Rails, when you try to insert the record with ActiveRecord Model, it always looks for a sequence, either a default one (Table_Object_seq) or explicitly defined one with set_sequence_name in the Model class.
So, it threw error as expected :-
OCIError: ORA-02289: sequence does not exist: select "Table_Object_seq
".nextval id from dual
I could have defined a sequence on this table only in the test database, but I googled around for a way to avoid oracle sequence in a model and I got this link :-
http://www.dixis.com/?p=127
I followed the suggestion and it worked for me :-)
What I did was :-
Monkeypatching OracleEnhanced Adapter's next_sequence_value method :-
and adding set_sequence_name to the Model class :-
set_sequence_name 'autogenerated'
It avoided the default oracle sequence look up while inserting new records with Rails ActiveRecord methods which solved my problem.
Monday, February 28, 2011
Wednesday, February 16, 2011
Speech by Shankar Abhyakar on Veer Savarkar
Few days ago, I listened to a great speech series (in Marathi language) given by Vidyavachaspati Shankar Abhyankar on Swatantryaveer Vinayak Damodar Savarkar. (http://www.savarkar.org/en/audio)
I have always admired greatness of Savarkar. He was a fearless freedom fighter, social reformer, writer, dramatist, poet, historian, political leader and philosopher to name a few. After listening to the speech, my respect about him got multiplied by thousand times. A good amount of information about Veer Savarkar and some of his books are made available at http://www.savarkar.org/ in both Marathi and English languages. Please read and get inspired !
Vande Mataram !
I have always admired greatness of Savarkar. He was a fearless freedom fighter, social reformer, writer, dramatist, poet, historian, political leader and philosopher to name a few. After listening to the speech, my respect about him got multiplied by thousand times. A good amount of information about Veer Savarkar and some of his books are made available at http://www.savarkar.org/ in both Marathi and English languages. Please read and get inspired !
Vande Mataram !
Saturday, February 12, 2011
Do you really need a Code Beautifier ?
A food of thought! As a good developer, do you really need a Code Beautifier?
This thought came to my mind when I was reviewing my friend's code which was not properly intended for whatever reason and appeared to me as if scribbled on a paper. Soon I got frustrated comprehending the code with my brain muscles stretching out beyond their limits. When I asked the reason behind improper indentation, I got an answer (rather a question) - Do we have a good code beautifier which would properly intend the code without human intervention? That was an unexpected answer to me. There are certainly a lot of paid and open source code beautifier tools available, but do we really need them when we write a code?
Why can't we make it a good practice to properly intend the code when we write? Almost all IDEs provide good facility to have proper indentation and formatting. Why am I emphasizing so much on this? In my opinion, a good readable and logically structured code is necessary not merely for outlook. It actually reflects your mind - your attitude, discipline and neatness. This also brings a very important aspect when you are collaborating amongst your development team.
Yes, when you download a third party code and if that code is not properly formatted, then the code beautifier tools play a good role in making the code clean and readable. The code beautifiers are meant for existing ugly code faces, they are not meant for fresh development!
The ultimate message is - whenever you are creating something, be it a code or anything... make it always beautiful .... :-)
This thought came to my mind when I was reviewing my friend's code which was not properly intended for whatever reason and appeared to me as if scribbled on a paper. Soon I got frustrated comprehending the code with my brain muscles stretching out beyond their limits. When I asked the reason behind improper indentation, I got an answer (rather a question) - Do we have a good code beautifier which would properly intend the code without human intervention? That was an unexpected answer to me. There are certainly a lot of paid and open source code beautifier tools available, but do we really need them when we write a code?
Why can't we make it a good practice to properly intend the code when we write? Almost all IDEs provide good facility to have proper indentation and formatting. Why am I emphasizing so much on this? In my opinion, a good readable and logically structured code is necessary not merely for outlook. It actually reflects your mind - your attitude, discipline and neatness. This also brings a very important aspect when you are collaborating amongst your development team.
Yes, when you download a third party code and if that code is not properly formatted, then the code beautifier tools play a good role in making the code clean and readable. The code beautifiers are meant for existing ugly code faces, they are not meant for fresh development!
The ultimate message is - whenever you are creating something, be it a code or anything... make it always beautiful .... :-)
Subscribe to:
Posts (Atom)






