Saturday, April 26, 2008

learning perl review ch2


scalar:
    1.233, 2E-3, 3e45
# exponential float
123_123_234
# integer with underscore
0377, 0xff, 0b1111
# octal => 0, hexadecimal => 0x, binary => 0b
"hello,"."world!"
# concatenate with .
"hello" x 3
# repeat 3 time with x
"20fred" * 3
# ignore string when transform
$str = $str . "blah"; === $str .= "blah";
$int = $int ** 3; === $int **= 3;
# binary assignment operator
${word}blah~
# insert variable with dereference brace
eq == | ne != | < lt | > gt | <= le | >= ge
# string comparison operator

# return with \n at line end
defined($var)
# return true or false

initial parameters (perl -M)
    use warnings;
use diagnostics;

 
 

Friday, April 25, 2008

boring comparison

show complex data structure in perl and ruby

perl
#!/usr/bin/env perl
use warnings;
use Data::Dumper;

$a = ["a", "b", "c", { "key" => "value" } ];
print Dumper $a;


ruby
#!/usr/bin/env ruby
a = ["a", "b", "c", { "key" => "value" } ]
puts a.inspect
 
 

Saturday, April 19, 2008

has_many() function note

if has_many :order
order.delete_all just set foreign key null
order.destroy_all clear all sub record
 
 

Friday, April 18, 2008

apache2 and mongrel cluster on debian

Enable apache2 proxy
$ sudo a2enmod proxy proxy_http proxy_balancer

Configure Proxy in a virtual host
/etc/apache2/site-enabled/default add
DocumentRoot "RailsApp/public"
    # even only RailsApp also work

ProxyPass / balancer://localhost/
ProxyPassReverse / balancer://localhost/
< proxy balancer://localhost/ >
    BalanceMember http://localhost:3000
    BalanceMember http://localhost:3001
    BalanceMember http://localhost:3002
< /proxy >


Then set mongrel cluster
$ sudo gem install mongrel_cluster
$ cd RailsApp
$ mongrel_rails cluster::configure -e development -p 3000 -N 3
    # write config/mongrel_cluster.yml

$ mongrel_rails cluster::start


Final, restart apache2 and it work.

 
 

Thursday, April 17, 2008

rss feed in rails

Note for the Way to create Atom and RSS 2.0 feed.

$ ./script/generate controller feed

Suppose that the model we want show in feed is Post

class FeedController < ApplicationController
    def rss
        @rss = @@post
    end

    def atom
        @atom = @@post
    end

  private
    @@post = Post.find :all,
        :order => "update_at", :limit => 10

end


Then write the view.

app/view/feed/rss.rxml

01 xml.instruct! :xml, :version=>"1.0"
02 xml.rss(:version=>"2.0"){
03     xml.channel{
04         xml.title("The site title")
05         xml.link(url_for("/"))
06         xml.description("The site description")
07         xml.language('en-us')
08
09         @rss.each do |rss|
10         xml.item {
11             xml.title rss.title
12             xml.link ""
13             xml.description rss.content
14             xml.pubDate rss.update_at
15             xml.guid ""
16             xml.author rss.declarer
17         }
18         end
19     }
20 }



app/view/feed/atom.rxml

01 xml.instruct! :xml, :version=>"1.0"
02 xml.feed "xmlns" => "http://www.w3.org/2005/Atom" do
03     xml.title("The site title")
04     xml.link(url_for "/")
05     @atom.each do |atom|
06         xml.entry {
07             xml.title(atom.title)
08             xml.summary(atom.content)
09         }
10     end
11 end


app/view/layout/application.rhtml add

<%= auto_discovery_link_tag :rss, { :controller => '/rss', :action => 'rss' }, { :title => "RSS 2.0" } %>
<%= auto_discovery_link_tag :rss, { :controller => '/rss', :action => 'atom' }, { :title => "Atom" } %>


 
 

Tuesday, April 15, 2008

rails helper block

Write for memo.
To set a helper in the form

<% helper_name do %>
    <div>blah</div>
<% end %>
.

The definition is

def helper_name(&block)
    content = capture(&block)
    concat("what we want write before block".block.binding)
    concat(content,block.binding)
    concat("what we want write after block",block.binding)
end


By the way, partial is the way do the same thing once. if the outer html is used frequently, the way to define a helper as above will be better.
 
 

Sunday, April 13, 2008

spakit

The rails plugin spakit means Single Page Application KIT. It can transform rails app into single page app but not modify app structure in large scale by loading content of other controller into primary controller.

The installation step is from handlino.com
$ sudo gem install spakit
$ cd railsapp/vendor/plugins/
$ gem unpack spakit
$ mv spakit-version spakit

now installation finished.

Before using it, spakit need a layout railsapp/app/view/layout/spakit.rhtml<%= flash[:notice] %>
<%= yield %>

And the layout application.rhtml should contain
<%= javascript_include_tag :defaults %> to include default js file.

Now we can use it happy. Add <div id="content"></div> into the view of primary controller, and set a spakit link: <%= spakit_link_to 'new person', :url => new_person_path %>, the content of the url will appear in the div#content.

Also two other helpers contain in spakit, spakit_form_for and spakit_form_tag

link1: spakit at rubyforge
link2: spakit at github

annotate models

The plugin used for adding column annotate into model file is introduced in Ralls Bible. Write for note. the newer introduction at http://pragdave.pragprog.com/pragdave/2006/02/annotate_models.html. Just two step to use it.
1.script/plugin install http://repo.pragprog.com/svn/Public/plugins/annotate_models
2. rake annotate_models
And all model now with column annotate at file head.

Saturday, April 5, 2008

.bashrc and screen on leopard

Leopard does not put .bashrc in user home directory as default. It is not convenient when using screen because screen only load ~/.bashrc but not /etc/bashrc. So copy the latter as my .bashrc.

debian package hyperlink

If writing <a href=”apt:package_name”>package_name</a> in html, On click the hyperlink, debian will install it.

openid resource

Report by solidot.org: openid resource. Noticeable point is blogger.com is one of the openid provider. It is a little surprising to me.