Archive

Archive for the ‘ruby’ Category

TextMate CheatSheet (Updated)

August 2nd, 2009 rupert Comments off

Shortcuts

Ruby:
---------------------------------
:key => "value" - : + tab
 
Navigate:
---------------------------------
go to file - cmd - t
tab to file (left|right) - opt + cmd + (left arrow|right arrow)
select bundle item - ctrl + cmd + t
navigate (from controller to view) - opt + cmd + down arrow
show methods - shift + cmd + t
 
Views:
---------------------------------
create partial - ctrl + shift + H
render partial (object|collection|locals) - r + p + (o|c|l)
inserting an erb tag <%=  %> or <% %> - ctrl + shift + . (cycle)
code completion, <h3>foo</h3> - opt + cmd + .
 
2RMobile Snippets:
---------------------------------
<% objects.each do |object| %>		2rmloope
	<%= object. %>
<% end %>

* Make the project drawer appear on the left or right.

From the menubar select View, then Hide Project Drawer.

Move your TextMate editor window over to the left side of the screen so the project gutter won’t have enough room to open up on that side. (You can move the window all the way to the left if you want, but you don’t need to go that far.)

Select View, then Show Project Drawer, and the drawer will now open on the right side of your TextMate editor.

* Plugins directory is located at

1. /Applications/TextMate.app/Contents/PlugIns/
2. /Users/rupert/Library/Application Support/

http://manual.macromates.com/en/bundles#support_folder

* Installing a bundle.
RubyOnRails Bundle: https://github.com/drnic/ruby-on-rails-tmbundle
RSpec Bundle: https://github.com/rspec/rspec-tmbundle
Cucumber Bundle: https://github.com/aslakhellesoy/cucumber-tmbundle

mkdir -p ~/Library/Application\ Support/TextMate/Bundles
cd ~/Library/Application\ Support/TextMate/Bundles
git clone git://github.com/drnic/ruby-on-rails-tmbundle.git "Ruby on Rails.tmbundle"

Launch TextMate > Bundles > Bundle Editor > “Reload Bundles”

* Identify *.html.erb to open in HTML(Rails)
Read http://blog.macromates.com/2007/file-type-detection-rspec-rails/

rupert:blog rupert$ defaults read com.macromates.textmate OakLanguageFileBindings
(
        {
        fileTypes =         (
            txt
        );
        language = "17994EC8-6B1D-11D9-AC3A-000D93589AF6";
        name = HTML;
    },
        {
        fileTypes =         (
            builder,
            rb,
            erb
        );
        language = "54D6E91E-8F31-11D9-90C5-0011242E4184";
        name = "Ruby on Rails";
    }
)
rupert:blog rupert$ defaults delete com.macromates.textmate OakLanguageFileBindings

* Identify Gemfiles in TextMate for Ruby on Rails
Read http://efreedom.com/Question/1-3174451/Bundler-Gemfile-Syntax-Highlight-Text-Mate

Include the ‘Gemfile’ as a filetype

fileTypes = ( 'rb', 'rxml', 'builder', 'Gemfile' );

* Soft Tabs and Spaces in TextMate
To turn on soft tabs (bottom of the window, in the tab size selector). This will insert spaces instead of tabs, so there won’t be anything to convert when it’s time to save
Source: http://old.nabble.com/Convert-tabs-to-spaces-when-saving–td28891731.html

Categories: osx, rails, ruby Tags: , ,

Rails Note #13: RubyonRails + Oracle on Linux (i386 / x64)

December 11th, 2008 rupert Comments off

In summary, install Oracle Instant Client and try to run sqlplus. If sqlplus connects to the oracle sid then go ahead and install the rails adapters for oracle. What is important to note here, is to install the oracle-instantclient for the architecture of your machine.. I have tested this on Debian Lenny (i386) and CentOS5 (x64)

1. Download from http://www.oracle.com/technology/software/tech/oci/instantclient/

a. oracle-instantclient-basic-10.2.0.4-1.i386
b. oracle-instantclient-devel-10.2.0.4-1.i386
c. oracle-instantclient-sqlplus-10.2.0.4-1.i386

2. Unzip everything to /opt/oracle/instantclient . You should have something like the ff:

[root@csapp1 instantclient]# ls -la
total 102704
drwxr-xr-x 3 root root     4096 Dec 10 21:54 .
drwxr-xr-x 3 root root     4096 Dec 10 22:03 ..
-rw-r--r-- 1 root root      228 Dec 10 21:52 BASIC_README
-r--r--r-- 1 root root  1609607 Dec 10 21:52 classes12.jar
-rwxr-xr-x 1 root root    67542 Dec 10 21:52 genezi
-r--r--r-- 1 root root     1525 Dec 10 21:52 glogin.sql
lrwxrwxrwx 1 root root       17 Dec 10 21:54 libclntsh.so -> libclntsh.so.10.1
-rwxr-xr-x 1 root root 21038613 Dec 10 21:52 libclntsh.so.10.1
-r-xr-xr-x 1 root root  3796601 Dec 10 21:52 libnnz10.so
-rwxr-xr-x 1 root root  1664116 Dec 10 21:52 libocci.so.10.1
-rwxr-xr-x 1 root root 72674185 Dec 10 21:52 libociei.so
-r-xr-xr-x 1 root root   138033 Dec 10 21:52 libocijdbc10.so
-r-xr-xr-x 1 root root  1435561 Dec 10 21:52 libsqlplusic.so
-r-xr-xr-x 1 root root   997409 Dec 10 21:52 libsqlplus.so
-r--r--r-- 1 root root  1555682 Dec 10 21:52 ojdbc14.jar
drwxr-xr-x 4 root root     4096 Dec 10 21:52 sdk
-r-xr-xr-x 1 root root     7773 Dec 10 21:52 sqlplus
-rw-r--r-- 1 root root      232 Dec 10 21:52 SQLPLUS_README
-rw-r--r-- 1 root root      516 Dec 10 21:53 tnsnames.ora
[root@csapp1 instantclient]#

3. Make a symbolic link for libclntsh.so.10.1 as shown above.

4. Create the Oracle Environment variables

export ORACLE_HOME=/opt/oracle/instantclient
export TNS_ADMIN=$ORACLE_HOME
export LD_LIBRARY_PATH=$ORACLE_HOME:$LD_LIBRARY_PATH
export DYLD_LIBRARY_PATH=$ORACLE_HOME
export PATH=$PATH:$ORACLE_HOME

5. At this point, you should have oracle-instantclient properly installed. You can test by running sqlplus.

[root@csapp1 instantclient]# sqlplus
 
SQL*Plus: Release 10.2.0.4.0 - Production on Thu Dec 11 11:47:40 2008
 
Copyright (c) 1982, 2007, Oracle.  All Rights Reserved.

NOTE: Sometimes you will get a SEGMENTATION FAULT. If so, try to open a new shell with the environment variables loaded and do an sqlplus in a directory which is not /opt/oracle/instantclient.

6. Install the oracle adapter for rails

7. gem install ruby-oci8

8. gem install oracle_enhanced-adapter –source=”http://gems.rubyonrails.org/”

activerecord-oracle-adapter (1.0.0.9250)
activerecord-oracle_enhanced-adapter (1.1.8)

NOTE: Try to look for the latest gems, the source above is at the time of this writing so it might change.

9. Test using irb

[root@csapp1 instantclient]# irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'oci8'
=> true
irb(main):003:0>
Categories: debian, linux, oracle, rails, ruby Tags: , , ,

Rails Note #12: Oracle on Intel Mac

December 5th, 2008 rupert 1 comment

1. Read http://www.foliosus.com/2008/05/05/connecting-ruby-on-rails-to-oracle-on-an-intel-mac-in-leopard-take-2/

2. Install Oracle Instant Client on Mac.

a. Instant Client Package – Basic: All files required to run OCI, OCCI, and JDBC-OCI applications
- instantclient-basic-macosx-10.2.0.4.0.zip (34,020,719 bytes)

b. *Instant Client Package – SDK: Additional header files and an example makefile for developing Oracle applications with Instant Client
instantclient-sdk-macosx-10.2.0.4.0.zip (603,493 bytes)

OR download the whole bundle (10.2.0.4.zip) with sqlplus installed from my installers.

3. Put this on your sudo vim ~/.bash_profile.

export ORACLE_HOME=/Library/Oracle/instantclient/10.2.0.4
export TNS_ADMIN=$ORACLE_HOME
export LD_LIBRARY_PATH=$ORACLE_HOME
export DYLD_LIBRARY_PATH=$ORACLE_HOME
export PATH=$PATH:$ORACLE_HOME

4. Make a symbolic link

cd /Library/Oracle/instantclient/10.2.0.4
ln -s libclntsh.dylib.10.1 libclntsh.dylib

5. Go to /Library/Oracle/instantclient/10.2.0.4 and edit tnsnames.ora. Point the Oracle SID to the IP where you installed Oracle.

ORCL =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.155)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl)
    )   
  )
 
 
ORCL_2_11 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.2.11)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl)
    )   
  )

6. Install the oracle-adapter for rails

sudo gem install activerecord-oracle-adapter --source http://gems.rubyonrails.org

7. In your database.yml file

development:
  adapter: oracle
  database: orcl
  username: youzhu_mobile_dev
  password: your_password

or browse the contents of a sample rails project youzhumobile.tar.gz

8. If you ever encounter an encoding problem, then we need to set the NLS_LANG environment variable before running script/server.

# export NLS_LANG=American_America.UTF8
# script/server

or I prefer setting it in the environment.rb

Rails::Initializer.run do |config|
  ENV['NLS_LANG']='American_America.UTF8'
  # Settings in config/environments/* take precedence over those specified here.

Note: If you don’t know your database encoding, then read this post.

Categories: mac, oracle, osx, rails, ruby Tags: , , ,

Rails Note #9: Getting HTTP Headers

November 20th, 2008 rupert Comments off

1. I was trying to get the http headers dump from a mobile phone. So I quickly dump it in a log file to see its contents..

    headers['Content-Type'] = 'text/xml; charset=utf-8'
    for header in request.env.select {|k,v| k.match("^HTTP.*")}
        logger.info(header[0].split('_',2)[1] + ":" + header[1])
    end

Reference:
http://tonycode.com/wiki/index.php?title=Dumping_HTTP_Headers

2. However, please note that you can actually see everything from the request as parameters.

Parameters: {“MSAG-ADDRESS-PREFIX”=>”aSTARTa”, “format”=>”xml”, “protocol”=>”ussd”, “user-agent”=>”Jakarta Commons-HttpClient/3.0.1″, “WHOISD-ABONENT”=>”8613520747210″, “action”=>”menu”, “controller”=>”ussd”, “subscriberID”=>”8613520747210″, “WHOISD-USR”=>”-1″, “host”=>”wap.watago.mobi”, “WHOISD-USSD-MESSAGE”=>”", “content-length”=>”0″}

This means we can easily do..

@whoisd_abonent = params['WHOISD-ABONENT'] || nil
Categories: rails, ruby Tags: ,

Rails Note #8: Testing with session variables

November 18th, 2008 rupert Comments off

Taken from http://guides.rails.info/testing_rails_applications.html

The get method kicks off the web request and populates the results into the response. It accepts 4 arguments:

*The action of the controller you are requesting. This can be in the form of a string or a symbol.
*An optional hash of request parameters to pass into the action (eg. query string parameters or post variables).
*An optional hash of session variables to pass along with the request.
*An optional hash of flash values.

Example: Calling the :show action, passing an id of 12 as the params and setting a user_id of 5 in the session:

get(:show, {'id' => "12"}, {'user_id' => 5})
Categories: rails, ruby Tags: ,