Example SQL Report with the Ruby Spreadsheet Gem
By kenglish
If you want to create a Excel reports for your users, this can be done rather easily in Ruby using the Spreadsheet Gem.
def spreadsheet_report(excel_filename, worksheet_name, column_order, result) book = Spreadsheet::Workbook.new sheet1 = book.create_worksheet :name => worksheet_name rownum = 0 for column in column_order sheet1.row(rownum).push column end for row in result rownum += 1 for column in column_order sheet1.row(rownum).push row[column].nil? ? 'N/A' : row[column] end end book.write "#{excel_filename}.xls" end
Here’s what the code would like in your rake task
column_order = ["Name", "DOB", "Rank","Hire Date", "Height", "Weight" ] sql =<<-END SELECT name AS Name, date_of_birth AS DOB, rank AS Rank, hire_date Hire Date, height AS Height, weight AS Weight FROM fire_fighters ORDER BY name END conn = ActiveRecord::Base.connection result = conn.select_all(sql) excel_filename = "FireFighterReport#{Time.year}" worksheet_name = "FireFighter Report #{Time.year}" spreadsheet_report(excel_filename, worksheet_name, column_order, result)
Now, that’s easy!
svn2git for real men
By kenglish
On the server, set up the remote repositories:
1 2 3 | mkdir project1.git cd project1.git git --bare init |
Here’s a script to do them all in one shot, just modifiy the REPOS variable:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #!/bin/sh REPOS="project1 project2 project3 project4" for repo in $REPOS do repo_dir="$repo.git" mkdir -p $repo_dir echo "Creating git directory $repo_dir" cd $repo_dir git --bare init cd .. done exit |
Now, on the workstation:
1 2 | sudo apt-get install git-core git-svn sudo gem install nirvdrum-svn2git --source http://gems.github.com |
Create the authors.txt in the following format:
1 2 | dburger = David Burger <email@email.com> jdoe = John Doe <jdoe@doe.com> |
For one project, do the following:
1 2 3 4 5 | mkdir project1 cd project1 svn2git https://svn.myserver.org/repos/ses --authors ../authors.txt git remote add origin hailstorm.myserver.org:/home/kenglish/repotest/ses.git git push --all |
The script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #!/bin/sh REPOS="project1 project2 project3 project4" for repo in $REPOS do mkdir $repo cd $repo cmd="svn2git https://svn.myserver.org/repos/$repo --authors ../authors.txt" echo $cmd `$cmd` cmd="git remote add origin hailstorm.myserver.org:/home/kenglish/repotest/$repo.git" echo $cmd `$cmd` cmd="git push --all" echo $cmd `$cmd` cd .. echo "DONE EXPORTING $repo" done exit |
Don’t diss the shell script, I leave in the echoes in case i need to test stuff out…
Note: Another option for the authors file is place it in your home directory .svn2git/authors (e.g. /home/kenglish/.svn2git/authors). Svn2git will automatically detect it and use it.
Any questions? Comments?
config.gem: Unpacked gem ezcrypto-0.7 in vendor/gems has no specification file. Run ‘rake gems:refresh_specs’ to fix this.
By kenglish
Thanks to Giles, I finally found the answer to this problem.
Open config/environment.rb and add “Rails::VendorGemSourceIndex.silence_spec_warnings = true”
1 2 3 4 5 6 | RAILS_GEM_VERSION = '2.2.2' unless defined? RAILS_GEM_VERSION # Bootstrap the Rails environment, frameworks, and default configuration require File.join(File.dirname(__FILE__), 'boot') Rails::VendorGemSourceIndex.silence_spec_warnings = true |
You can put an awesome comment like this guy did:
http://gilesbowkett.blogspot.com/2009/03/configgem-unpacked-gem-whatever-in.html
Getting started with BioRuby and Ruby on Rails.
By kenglish
Started messing with Bioruby this week. I went through George Githinji’s excellent tutorial on his biorlated blog, Bio-graphics, BioSQL and Ruby on Rails part 1 and Bio-graphics, BioSQL and Ruby on Rails part 2. This is very helpful. Before you do the stuff below, read through his tutorial so you understand what’s going on. Here’s what I had to do to get it working:
Install the gems
I got started by installing the bio, bioruby gem.
sudo gem install bioruby sudo gem install bio
I discovered that the bio-graphics gem needs cairo. Here’s how to install it on Ubuntu. This had 50-something dependencies. I just sat back and let them install:
sudo apt-get install cairo-clock cairo-dock-dev libcairo-ruby libcairo2
I also had to install ruby support for pango:
sudo apt-get install -y libpango1-ruby
I installed bio-graphics and the cairo gem.
gem install bio-graphics cairoGeorge also uses rails_sql_views so I installed this:
sudo gem install rails_sql_views
As George warned in his blog, I had to comment out these lines in
unless Kernel.respond_to?(:gem) Kernel.send :alias_method, :gem, :require_gem end
in rails_sql_views(0.6.1). Mine was located in /usr/lib/ruby/gems/1.8/gems/rails_sql_views-0.6.1/lib/rails_sql_views.rb. I commented out lines 28-30.
Install the biosql_rails_example application
To get george’s app running, I cloned the biorelate repository into my /home/kenglish/workspace directory:
cd /home/kenglish/workspace git clone git://github.com/georgeG/biosql_rails_example.git
I edited the database.yml file and added my mysql infomration. You can create the database by hand as George explains but I put in the mysql root username and password in the database.yml file and ran:
rake db:create.
Install the biosql database schema
As George explains, I had to download the biosql schema and untared it. I created the tables like this:
cd /home/kenglish/downloads/ tar xzvf biosql-1.0.1.tar.gz cd biosql-1.0.1/sql mysql -uroot -p var_biosql_development < biosqldb-mysql.sql
I put these in the directory biosql-1.0.1/scripts and loaded them with the script load_ncbi_taxonomy.pl.
perl load_ncbi_taxonomy.pl --dbname var_biosql_development --dbuser root --host localhost --download
I don’t have any bio-related data so I downloaded the following files from the Ncbi ftp site (ftp://ftp.ncbi.nih.gov/pub/taxonomy/): gi_taxid_nucl.dmp.gz taxcat.tar.gz and taxdump.tar.gz.
Now I was able to run George’s Rails app! To be continued…
Running an individual shoulda context test
By kenglish
I’m suffering from serious CRS. Yesterday, I was wondering how to run my shoulda tests individually. My co-worker said “Just google for it.” I entered a google search term and the 4th or 5th result was a post by ME to the shoulda google group. This was the post: Can you controller context test just one test via the -n flag?
To reinforce the lesson, you can run a single context using a regular expression.
context "on GET to :show for first record" do setup do get :show, :id => 1 end should_assign_to :sweater should_respond_with :success should_render_template :show end
Although, you need to put quotes around the regular expression or the command line interpreter mistaked the spaces for separate arguments:
ruby test/functional/sweater_controller_test.rb -n "/show for first record/"
Using gitk
By kenglish
Just installed gitk. Very nice…. Here’s how to install it on Ubuntu:
apt-get install gitk
Awesome git resources:
I hope this post has been helpful.
Learning Python and Pylons, Part 1
By kenglish
I don’t have anything insightful to blog about these days other than the fact that I’m learning Python and Pylons for my school project. I am using a package called BioPython. Instead of reading the standard Python tutorial, I went through the Python course in Bioinformatics . This tutorial walks biologists through the python language using biological examples. It then introduces the main packages in the BioPython library. It has some fun exercises to make it at hands-on experience.
The Biopython API documentation leaves a lot to be desired. I am probably spoiled by raiilsbrains where I can just enter a search string rather than clicking until I can find what I need.
I did bust out and purchase the Python Cookbook. The Programming Python book is a massive 1596. This is very impractical for the travelling programmer to carry. It would probably end up unread and collecting dust on my bookshelf the way my copy of the Cryptonomicon does.
I am surprised that python doesn’t have ruby/perl/php style string interpolation of variable.
In perl/php, I would do:
print "$first_name $last_name";
In ruby, I’d do:
puts "#{first_name} #{last_name}”
In Python, I must do:
print first_name + " " + last_name
but quite often I’m seeing examples like this:
print "%s %s" % (first_name, last_name)
Is that throw back to C or what? Didn’t they add string variable interpolation to languages to make the code more readable?
git reset, like svn revert, abandon all changes
By kenglish
Sometimes I’ve been making changes on the master branch and I want get my code back in sync with what is on the server. This is my stategy:
First, checkout the current branch into a new branch in case I need those change and then switch back to the master:
git checkout -b newbranch1.1.1 git checkout master
Then, view the log of the remote repository:
git log origin/master
Copy the commit number from the last commit, (eg 0b7e7260cf85ae0d57f6ab0e502202fade94df9d).
The, reset the master branch to the above commit number:
git reset --hard 0b7e7260cf85ae0d57f6ab0e502202fade94df9d
This seems like kind of long method, your comments are welcome.
git merge stategy for deploy.rb
By kenglish
My problem is that I have multiple git branch of a rails project and in each branch, I want to maintain a different value the branch setting my deploy.rb. For example:
In branch v1.0.0-stable
set :branch, "v1.0.0-stable"
In branch, I want v1.0.1-stable
set :branch, "v1.0.1-stable"
In the master branch, I want no value.
The problem is that I often merge from v1.0.1-stable into the master branch and this overwrites the deploy.rb. This can be frustrating because every time I need to remember to remove that “set :branch” line from the deploy.rb. If I forget and deploy from the master, it will deploy version “v1.0.1-stable” instead of master. This always results in my wondering “Why didn’t the deploy work? Why aren’t my changes on the site?”
The only way that I could find to prevent this is to adding a line my gitattributes file.
In .git/info/attributes, I added:
deploy.rb merge="ours"
Ours is a merge strategy and this tells git always apply
ours
This resolves any number of heads, but the result of the merge is
always the current branch head. It is meant to be used to
supersede old development history of side branches.
Note:
If you Google git merge the first 5 pages of results are the man page for the git-merge command. Why does everyone feel the need to replicate the man page all over the internet. Seriously! If what I needed was in the man page, I wouldn’t be using Google!
Refactoring ExtJS for readability
By kenglish
An old colleague once remarked that Perl is “write-only.” That wouldn’t be funny if it wasn’t true. I think JavaScript has a similar property. Being up to my eyeballs in ExtJS lately, I have been trying to come up with ways to make my code a little more manageable. One thing any JavaScript programmer hates is variable scoping bugs. They appear all the time and take 20 hours to fix.



June 17th, 2009