Monday, November 15, 2010

Rack Developers Notebook

Download the book from : http://www.meetup.com/silicon-valley-ruby/files/

Sunday, September 12, 2010

How to install MongoDB on OS X

Download, unpack, and install the pre-compiled 64-bit binaries:




curl -O http://downloads.mongodb.org/osx/mongodb-osx-x86_64-1.6.2.tgz
tar xzf mongodb-osx-x86_64-1.6.2.tgz
sudo mv mongodb-osx-x86_64-1.6.2 /opt/local/mongodb
sudo mkdir /opt/local/mongodb_data /var/log/mongodb
sudo chown -R root /opt/local/mongodb


(If you’re on a 32-bit machine, substitute in i386 for each x86_64 above.)




Next, you’ll want to make a config file so you can change the server’s options without fiddling with command-line arguments.




Save as: /opt/local/mongodb/mongod.conf




# Store data alongside MongoDB instead of the default, /data/db/
dbpath = /opt/local/mongodb_data

# Only accept local connections
bind_ip = 127.0.0.1


Now, we’ll make a launchd job to register the server as an OS X daemon. launchd will start the server at startup, stop it before shutdown, make sure it stays up, and redirect its output to a nice log file.




Save as: /Library/LaunchDaemons/org.mongodb.mongod.plist




<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>

<string>org.mongodb.mongod</string>
<key>ProgramArguments</key>
<array>
<string>/opt/local/mongodb/bin/mongod</string>

<string>run</string>
<string>--config</string>
<string>/opt/local/mongodb/mongod.conf</string>
</array>

<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>WorkingDirectory</key>

<string>/opt/local/mongodb</string>
<key>StandardErrorPath</key>
<string>/var/log/mongodb/output.log</string>
<key>StandardOutPath</key>

<string>/var/log/mongodb/output.log</string>
</dict>
</plist>


Now we just need to load the launchd job:




sudo launchctl load /Library/LaunchDaemons/org.mongodb.mongod.plist


And that should do it! Try visiting http://localhost:28017 to see the status console for your database.




One last thing: you should probably add /opt/local/mongodb/bin to your $PATH. That way you can use the other binaries that ship with MongoDB, like the mongo console, mongoexport, and so on.




You can adjust your path the regular way by editing your shell’s profile, or you can use this nice paths.d mechanism that OS X provides:




sudo sh -c 'echo "/opt/local/mongodb/bin" > /etc/paths.d/mongodb'

Saturday, September 11, 2010

Install Postgresql server on Ubuntu 10.04

1) sudo apt-get install postgresql
2) sudo apt-get install postgresql-client
3) sudo /etc/init.d/postgresql-8.4 restart
4) ALTER USER postgres with encrypted password 'your_password';
5) sudo /etc/init.d/postgresql-8.4 restart

Install Java 6 on Ubuntu 10.04

1) sudo add-apt-repository "deb http://archive.canonical.com/ lucid partner"
2) sudo apt-get update
3) sudo aptitude install curl sun-java6-bin sun-java6-jre sun-java6-jdk

Reference:

Install Java 6 on Ubuntu

Friday, September 10, 2010

Cloning a VM with mutiple snapshots

As of 3.0.8 (or before) the VBoxManage clonehd command can clone any snapshot of a hard disk or clone the Current State of a hard disk. The resulting VDI file is flattened (it does not have any other snapshots attached).
You need to find the UUID of the Current State image file, or of the Snapshot image file (NOT the UUID of the snapshot!)
To clonehd the Current State of a disk:

* Open the Virtual Media Manager (Menu: File>Virtual Media Manager).
* Navigate to the hard disk you want to clone and fully expand the tree (use * key on numeric keypad).
* Find the {hexUUID}.vdi file that is attached to the VM you want to clone the Current State of.
It will be (one of) the leaf files at the end of a branch.
When you select it, the 'Attached to:' text at the bottom of the window will say:
'Attached to: ', without any (snaphot name) in brackets after it.
* Click the filename to the right of the 'Location:' displayed at the bottom of the window.
* Ctrl+C / Copy to copy the text to the clipboard, which will be something like:
(path){b6441469-5ccb-418c-8fdd-73c5e1a17314}.vdi
* Ctrl+V / Paste and extract the hexUUID value (removing the {} brackets) and run clonehd to clone it!
CODE: C:> VBoxManage clonehd b6441469-5ccb-418c-8fdd-73c5e1a17314 CloneDisk.vdi

To clonehd a snapshot of a disk:

* Find the {hexUUID}.vdi file that is attached to the VM (and Snapshot) you want to clone.
* It will be one of the files along a branch.
When you select it, the 'Attached to:' text at the bottom of the window will say:
'Attached to: ()', with (snaphot name) in brackets after it.
* Follow the instructions above for cloning the current state of a disk.



Reference:

This thread

Wednesday, September 08, 2010

VirtualBox Shared Folders between Ubuntu 10.04 Guest and Mac Host

You’ll need the VirtualBox (1.6 or above) with Guest Additions installed in Ubuntu.

* With the Virtual Machine powered off and selected in VirtualBox, go to:
Machine > Settings > Shared Folders
* For “Folder Path”, click the icon to browse for the folder you want to share.
* For “Folder Name”, enter a name to describe the share. Let's say vshare
* Click “OK” and start the virtual machine again.
* Create a mount point which is basically an empty folder. Example: /vmnt on the Ubuntu guest VM.
* Fire up the terminal and type:
sudo mount -t vboxsf folder_name path_to_mount_pointfolder_name is the name you typed in earlier to describe the share. Example: sudo mount -t vboxsf vshare /vmnt
* You should be able to browse the shared folder now.

Reference:

VirtualBox Shared Folders on Ubuntu

How to install KDevelop on Ubuntu 10.4

KDevelop is a decent editor for Rails projects. It gives a nice class browser view of your Rails project.

To resolve the error:
E: Package kdevelop has no installation candidate

before install, Go to :

1. Applications -> System Tools -> KPackage Edit
2. Settings -> Edit Software Sources -> Updates tab
3. Check "Unsupported Updates"
4. sudo apt-get update
5. Run : sudo apt-get install kdevelop

Alternate way:

1. sudo add-apt-repository ppa:kubuntu-ppa/backports
2. sudo apt-get update
3. sudo apt-get install kdevelop

To run KDevelop, just type kdevelop on the terminal

Tuesday, September 07, 2010

How to Install Guest Additions on CentOS 5.5

Prerequisite:

yum install yum-priorities
Make sure that yum-priorities is enabled by editing the /etc/yum/pluginconf.d/priorities.conf file, and ensuring that it contains the following lines:

[main]
enabled=1

Edit the .repo files in /etc/yum.repos.d/ and set up priorities by adding the line:

priority=N

to a repository entry, where N is an integer number from 1 to 99.
The recommended settings are:
[base], [addons], [updates], [extras] ... priority=1
[centosplus],[contrib] ... priority=2
Third Party Repos such as rpmforge ... priority=N (where N is > 10 and based on your preference)


Based on architecture: uname -i download the corresponding rpm into your downloads directory.

wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.1-1.el5.rf.i386.rpm

rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt rpm -K rpmforge-release-0.5.1-1.el5.rf.*.rpm

rpm -i rpmforge-release-0.5.1-1.el5.rf.*.rpm

yum check-update

Now you will be able to install dkms (You need sudo access to run these commands)


1) Devices > Install Guest Additions
2) Browse to the Guest Additions CD on the desktop, right click and open terminal. Run
sudo chmod +x VBoxLinuxAdditions-x86.run
3)yum install dkms
4) sudo yum install kernel-devel kernel-headers
sudo sh ./VBoxLinuxAdditions-x86.run

This will automatically switch VM to Seamless Mode and the resolution upgraded to 1024x768. You can now copy/paste from host to guest and vice-versa.

References:



How to Install Virtual Box Guest Additions for a Linux Guest


RPMForge

Saturday, August 28, 2010

Getting Rails and Sinatra to Share Sessions - Part I

Introduction

This is the first part of a series of blog posts that will show you how to setup your machine so that you can share sessions between Rails and Sinatra apps.

This idea was inspired by the In a World of Middleware, Who Needs Monolithic Applications? Mountain West Ruby Conference 2009 talk given by Corey Donohoe of Engine Yard.

However I will not be using Hancock or Hancock Client for Single Sign On. I think it is too complicated. I want something simple. My motivation is to build Rack compliant apps that are very focused on doing one thing really well and combining them to create a mashup.

At the end of the talk Corey said that in a world of why do we even need Rails? My opinion is that Rails app must be the last app in your Rack Stack and must be focused on your Core Domain. Anything else must be built as a Rack app. Because it is independent of your business and can be re-used across all your web apps.

Installation and Configuration

Environment: Ruby 1.8.7 managed by RVM

1)
gem install passenger

2)
passenger-install-apache2-module

Note: Steps 1 and 2 does not use sudo

3)
sudo mkdir /usr/local/apache2/conf

Add the lines shown during installation to httpd.conf file and save it in the directory created above.

In my case it is:

LoadModule passenger_module /Users/bparanj/.rvm/gems/ruby-1.8.7-p299/gems/passenger-2.2.15/ext/apache2/mod_passenger.so
PassengerRoot /Users/bparanj/.rvm/gems/ruby-1.8.7-p299/gems/passenger-2.2.15
PassengerRuby /Users/bparanj/.rvm/rubies/ruby-1.8.7-p299/bin/ruby

4) Setup folder to hold vhosts

sudo mkdir /usr/local/apache2/conf/vhosts

5) Add the following to the httpd.conf

NameVirtualHost *
Include /usr/local/apache2/conf/vhosts/*

6) Add virtual hosts to a file virtual_hosts.conf under /usr/local/apache2/conf/vhosts directory

# Example App

ServerName app.test
DocumentRoot /Users/benr/Rails/app/public
RailsEnv development



# Example App 2

ServerName app2.test
DocumentRoot /Users/benr/Rails/app2/public
RailsEnv development


7) Edit /etc/hosts file

sudo vi /etc/hosts

and add:

127.0.0.1 app.test app2.test

8)
sudo apachectl graceful or

sudo apachectl start

9) To restart app, create a file under RAILS_ROOT/tmp/restart.txt

when you run touch on restart.txt your Rails app will be reloaded

References:

http://benr75.com/2008/04/12/setup-mod_rails-phusion-mac-os-x-leopard
http://www.modrails.com/videos/passenger.mov

Sunday, August 15, 2010

error: Error running 'make ', please check. rvm/log/ruby-1.8.7-p299/make*.log

Step 1: Install readline.

1 curl -O ftp://ftp.cwru.edu/pub/bash/readline-6.0.tar.gz
2 tar -xvzf readline-6.0.tar.gz
3 cd readline-6.0
4 ./configure
5 make
6 sudo make install

Step 2: Install Ruby 1.8.7
rvm install 1.8.7 -C --enable-shared,--with-readline-dir=/usr/local

Wednesday, July 28, 2010

RSpec API - The Good and the Bad

The Good:

Operator Expressions : Good use of meta-programming

The Bad:

#1. Have method has bubble words and it complicates the API. It increases the noise in the code. Make the API minimal. (Bubble word - Words that does not do anything, but is used only for readability)

#2. Leaky API : render method is leaky because it depends on the argument passed in to the outer method. Basically it has parametric coupling. Developer has to *remember* the coupling in order to use the API.

Tuesday, July 27, 2010

undefined method `visit' for cucumber

Fix: Include the following configuration in features/support/env.rb

require 'webrat'
require 'webrat/core/matchers'

Webrat.configure do |config|
config.mode = :rack
config.open_error_files = false # Set to true if you want error pages to pop up in the browser
end

World(Webrat::Methods)
World(Webrat::Matchers)

This also fixes the error : "no such file to load -- action_controller/integration"

Saturday, July 24, 2010

Getting RSpec Book Rails app working on Rails 3.0 Beta 4

1. Create a new Rails app called blog, skip test unit since we will be using RSpec
rails new blog --skip-testunit

2. Edit gem file :

group :test do
gem 'rspec-rails', '>= 2.0.0.beta.10'
gem 'webrat'
gem 'selenium-client'
gem 'database_cleaner'
gem 'rspec-rails'
gem 'cucumber'
gem 'cucumber-rails'
gem 'rspec'
end

3. From app's root directory, run:

bundle install

4. rails g rspec:install

5. rails g cucumber:install

6. rails g cucumber:skeleton --rspec --webrat

7. rails g cucumber:feature

8. script/cucumber --tag @focus

9. script/cucumber -t @focus

10. rails g model movie showtime_date:date showtime_time:time
11. rake db:migrate
12. rake db:test:prepare
13. cucumber -t @focus
14. cucumber

Reference:

Rails 3, RSpec, and Cucumber

Monday, July 19, 2010

How to add a source to Ruby gems

gem sources -a http://gems.github.com

To remove a source:

gem sources -r http://gems.github.com

Run:
gem env

to see the sources that is currently in your Ruby gems.

Monday, July 12, 2010

Extremely Simple Alternative to AutoTest

I am reading the Rails 3 in Action by Yehuda. To run the sample code I customized the stakeout.rb to output the results of the test when specs get run whenever the file is changed.

1. Copy the source to a directory that is in your path:

#!/usr/bin/env ruby
if ARGV.size < 2
puts "Usage: stakeout.rb [files to watch]+"
exit 1
end

command = ARGV.shift
files = {}

ARGV.each do |arg|
Dir[arg].each { |file|
files[file] = File.mtime(file)
}
end

loop do

sleep 1

changed_file, last_changed = files.find { |file, last_changed|
File.mtime(file) > last_changed
}

if changed_file
files[changed_file] = File.mtime(changed_file)
puts "=> #{changed_file} changed, running #{command}"
puts system(command)
puts "=> done"
end

end

2. chmod +x stakeout.rb

3. To run:

stakeout.rb "spec bacon_spec.rb" **/*

This will run the spec called back_spec.rb whenever any files found recursively from the current file is changed.

Reference:

Faster TDD with Stakeout.rb

Problem Installing Sqlite3-ruby on Snow Leopard

I was getting this error when I started the server. I had installed RVM, Ruby 1.9.1, Rails 3.0 Beta 4:

.rvm/gems/ruby-1.9.1-p378/gems/sqlite3-ruby-1.3.1/lib/sqlite3/sqlite3_native.bundle: dlopen(.rvm/gems/ruby-1.9.1-p378/gems/sqlite3-ruby-1.3.1/lib/sqlite3/sqlite3_native.bundle, 9): no suitable image found. Did find: (LoadError)
.rvm/gems/ruby-1.9.1-p378/gems/sqlite3-ruby-1.3.1/lib/sqlite3/sqlite3_native.bundle: mach-o, but wrong architecture - .rvm/gems/ruby-1.9.1-p378/gems/sqlite3-ruby-1.3.1
/lib/sqlite3/sqlite3_native.bundle


Resolution:

Uninstall sqlite3-ruby (1.3.1)
1. gem uninstall sqlite3-ruby

Install sqlite3-ruby version 1.2.5
2. gem install sqlite3-ruby --version 1.2.5

If you get the error:

no such file to load -- sqlite3

when running rake db:migrate

Edit the Gemfile for the line sqlite3-ruby as :

gem 'sqlite3-ruby', :require => 'sqlite3'

Installing Ruby 1.9.2 using RVM on Snow Leopard

When I tried to install Ruby 1.9.2
rvm install 1.9.2

I got :

error: Error running 'make ', please check /Users/bparanj/.rvm/log/ruby-1.9.2-rc2/make*.log
error: There has been an error while running make. Aborting the installation.

Steps to Resolve :

1. Install libxml

curl -O ftp://xmlsoft.org/libxml2/libxml2-2.7.7.tar.gz
tar zxvf libxml2-2.7.7.tar.gz
cd libxml2-2.7.7
./configure --with-python=/System/Library/Frameworks/Python.framework/Versions/2.3/
make &
sudo make install

2. Install libxslt

curl -O ftp://xmlsoft.org/libxslt/libxslt-1.1.26.tar.gz
tar xvzf libxslt-1.1.26.tar.gz
cd libxslt-1.1.26
./configure
make
sudo make install

3.
rvm install 1.9.2-head -C --enable-shared,--with-readline-dir=/opt/local,--build=x86_64-apple-darwin10

Reference:

Installing Ruby 1.9.2 with RVM on Snow Leopard

Tips:

#1 :
rvm 1.9.2-head --default

to make it 1.9.2 when you open new terminal or after reboot.

#2 :
To revert to system installed Ruby:

rvm system --default

Thursday, July 08, 2010

uninitialized constant Mysql while running rake db:create

1. Uninstall the mysql gem

bparanj$ sudo gem uninstall mysql

Successfully uninstalled mysql-2.8.1

2. Installing the gem the right way on Snow Leopard
bparanj$ sudo env ARCHFLAGS="-arch i386" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
Building native extensions. This could take a while...
Successfully installed mysql-2.8.1
1 gem installed

3. Edit the gemfile and include the line:

gem 'mysql'

4. Run bundle install from the rails project directory:

mbp2:blog bparanj$ bundle install
Fetching source index from http://rubygems.org/
Using rake (0.8.7) from system gems
Using abstract (1.0.0) from system gems
Using activesupport (3.0.0.beta4) from system gems
Using builder (2.1.2) from system gems
Using i18n (0.4.1) from system gems
Using activemodel (3.0.0.beta4) from system gems
Using erubis (2.6.6) from bundler gems
Using rack (1.1.0) from bundler gems
Using rack-mount (0.6.6) from system gems
Using rack-test (0.5.4) from system gems
Using tzinfo (0.3.22) from system gems
Using actionpack (3.0.0.beta4) from system gems
Using mime-types (1.16) from system gems
Using polyglot (0.3.1) from system gems
Using treetop (1.4.8) from system gems
Using mail (2.2.5) from system gems
Using actionmailer (3.0.0.beta4) from system gems
Using arel (0.4.0) from system gems
Using activerecord (3.0.0.beta4) from system gems
Using activeresource (3.0.0.beta4) from system gems
Using bundler (0.9.26) from system gems
Using mysql (2.8.1) from system gems
Using thor (0.13.7) from bundler gems
Using railties (3.0.0.beta4) from system gems
Using rails (3.0.0.beta4) from system gems
Using sqlite3-ruby (1.3.0) from bundler gems
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

5. rake db:create will now work
mbp2:blog bparanj$ rake db:create
(in /Users/bparanj/projects/blog)
db/test.sqlite3 already exists

Friday, June 25, 2010

Include Vs Extend in Ruby

Include vs Extend

Include

Classes and modules can include modules and reuse the methods defined in the module. The method in the module is available as an instance method. The constants, methods and module variables are available once the module is mixed in.

Extend

You can extend an instance of any class with module. The method in the module is available as an instance method.

The difference is extend is only applicable for that particular instance which was extended. Where as the include is applicable to any instance of the class.

Screen cast IncludeExtend.mov

Addition to IncludeExtendRuby.mov

module Talkable
def talk
p "hi"
end
end

class Foo

end

Foo.extend Talkable

Foo.talk


If extend is used on the class instead of the object, the method becomes available as class method. The extend can be used inside the class or outside the class. Refer the screencast to see how it works.

Thursday, June 24, 2010

Upgrading Snow Leopard to Rails 3.0 Beta 4

1. gem env

should be 1.3.6 or above

2. gem update --system

Update Ruby gems to the latest version

3. sudo gem install i18n tzinfo builder memcache-client rack rack-test rack-mount erubis mail text-format thor bundler i18n bundler

Install required gems for Rails 3.0

4. sudo gem install rails --pre

Installs Rails 3.0 Beta 4

5. sudo mkdir /usr/local/lib/ruby/gems/1.9.1/gems/rails-3.0.0.beta4/lib

Create the lib directory to resolve the following error

ERROR: While executing gem ... (Errno::ENOENT)
No such file or directory - lib