September 2008 Archives

我在 jifty.org 上撰寫了此頁,在此以中文簡單說明一次。

Nginx 內附的 fastcgi 已經可以直接配合 Jifty application,請對照著 Wiki 上的設定內容,幾個重點如下:

fastcgi_param PATH_INFO $fastcgi_script_name;
Jifty 跟 Catalyst 內附的 fastcgi server 都需要 PATH_INFO 這個環境變數,FreeBSD 跟 Linux 上安裝好的 nginx 的預設 fastcgi 設定裡面並沒有此值,必需要手動加入。
root /APP/share/web/;
目錄設定到 share/web 就可以
fastcgi_pass localhost:9000;
這裡是指定 fastcgi server 的所在之處。似乎沒有方法讓 nginx 自動啟動 fastcgi server。因此需要另外啟動。

手動啟動 jifty fastcgi server 的方式為:

FCGI_SOCKET_PATH="localhost:9000" bin/jifty fastcgi &

此外,原本 Jifty 有附許多 static assets,但不知為何我給的設定並不會使 jifty fastcgi server 去 serve 那些 static assets。解決辨法之一是直接全部複製到 share/web/static 底下來:

cp -rn /usr/local/lib/perl5/site_perl/5.8.8/auto/Jifty/web/static/* share/web/static

此處的 -n 參數是「不要覆蓋現有檔案」之意,有些系統的 cp 沒有這個參數,多半可用 cp -i 替代

這組設定應該還可以再調整以解決此問題。但我覺得這種解法反而對佈署有益。因為讓 fastcgi server 來 serve static assets 完全是以牛刀殺雞。也無法善用 nginx async io,能很快回應靜態檔案的這個大優點。

更新:後來發現可以使用 Nginx 的 rewrite 規則來解決這個問題,只要你知道系統上的 @INC 路徑中,放 Jifty 那些 asset 的路徑是哪條。方法如下:

location /__jifty/static {
    alias /usr/local/lib/perl5/site_perl/5.8.8/auto/Jifty/web/static;
}

location /static {
    if (!-f $request_filename) {
        rewrite ^(.*)$ /__jifty$1 last;
    }
}

第一條規則其實是使 /__jifty/static 這個 URL 對應到 Jifty 所用來存放其內附的 asset 路徑。第二條 location 規則中則定義,如果找不到檔案的話,則重導到 /__jifty/static 去。說是重導其實也不太正確,因其並非經過瀏覽器的重導,只是在 Nginx 內部而已。

Labels: , , ,

賣 Macbook Pro 電腦一台

| No Comments | No TrackBacks
Update: 此機已經賣出。請勿再來信詢問。


在前幾年 Macbook Pro 剛出沒多久,我就買了一台來使用。那時第一件事情就是帶去北京參加 Apple 辨的輸入法廚房活動,跟 lukhnoszonble 一個晚上搞出了輸入法入力娘跟現在 LeopardVanilla 的雛型出來。後來也帶著在日本參加了兩次 YAPC::Asia, 還有一次輸入法燒鳥會。這台 Macbook Pro 還真是戰果輝煌啊。
而目前它已經功成身退,我希望轉手賣給想要繼續使它的人們。 基本資料如下:
  • 中文鍵盤,15 吋螢幕
  • 序號: W8630241VWW
  • 處理器: Intel Core Duo 2Ghz
  • L2 快取記憶體: 2 MB
  • 記憶體: 2 GB
  • 匯流排速度: 667 MHz
  • 這台只有 Firewire 400, 沒有 Firewire 800
外觀方面.. 只能說「不盡理想」,由於我經常使用,鍵盤表面上有許多磨損的痕跡,此外,主要的 "Enter" 鍵已經被我一指按成兩截,現在簡單地用透明膠帶接合、對付著用。不過打字起來一切正常。因為我自已一直沒什麼時間到優仕修理這個狀況,因此乾脆直接報告出來。
保固方面,已經超過一年保固期限,我也沒有再加買 Apple Care。電池在保固期快要結束之前換過全新的一顆,因此現在還可以連續使用兩個小時左右。 附件方面,我會將裝著它的 Moshi 銀灰色內袋一起送給買家。買來時,這台附的是 Tiger 的光碟片,除此之外我會附送一盒正版的 Leopard 光碟片。目前裡面安裝的是 Leopard,在正式交貨前也會替買家重新安裝。原廠附的 Apple Remote、顯示器接線等也都會一併加上。簡單地說就是整套全賣 + Leopard + 內袋。
有意願購買者,請用以下方式連絡我議價或約時間看商品。我人在新竹,但視情況可約台北或台中。
  • Email: gugod at gugod.org
  • Skype: gugodliu
  • AIM / iChat: gugod@mac.com

Labels:

JiftyX-ModelHelpers 0.01 is released to CPAN

| No Comments | No TrackBacks
This is my work to make C objects a little bit easier to start with. It let you write something this obvious:
 $book = Book(42);
 $books = BookCollection( author => "Neal Stephenson" );

This module is released as an extension under JiftyX namespace as
requested by jifty team. Use it at your own risk.

Go grab it at http://search.cpan.org/dist/JiftyX-ModelHelpers/

Labels: , ,

JiftyX::ModelHelper makes me happy.

| No Comments | No TrackBacks

Download: JiftyX-ModelHelpers

It has been a while since audreyt first mentioned the namespace "JiftyX" for Jifty plugins.

Few month ago I had this hack to make my life coding in Jifty easier (in the aspect of typing):

sub M {
 return Jifty->app_class(Model => $_[0])->new;
}

This lets you do something like:

$book = M("Book");
$book->load(42);

Instead of

$book = Jifty->app_class(Model => "Book")->new;

Jifty always want to be polite, prevent global namespace clobbering. That is an absolutely good convention to follow. But it does not make life of developer easier. I decided to see if I can push this M hack further and came up with something like this:

$book = Book(42);

I think this is likely the shortest way you can do to load a record with id 42 from Book model. In RoR, you do this:

book = Book.find(42)

Or, we might need to load a record with other info instead of ID:

$book = Book(name => "Harry Potter");

This is like calling Jifty::DBI::Record::load_by_cols(), but shorter

To do this in Rails, you need to say:

book = Book.find(:first, :conditions => ["name = ?", "Harry Potter"])

OK, now I am very happy ;)

After discussed on jifty-dev mailing list, I put my currently progress of this work on svn.jifty.org here: JiftyX-ModelHelpers . It has no doc in there, but currently what's implemented is really all described in this blog entry. So feel free to try it in your Jifty app.

Sometimes in View you will failed to use JiftyX::ModelHelpers, perl will tell you there are some syntax error. In that case, please say:

require JiftyX::ModelHelpers;
 JiftyX::ModelHelpers->import();

This makes me feels especially good writing tests. Here's a piece of test setup code in one of my recently work in progress:

use Retail::Test::Fixtures qw(providers commodities consumers);

my $good_company = Provider(name => "good company");
my $good_consumer = Consumer(name => "good consumer");

my $a = Commodity(name => "Hello Kitty A");

is($a->quantities_in_stock, 0, "Initially its stockless");

add_supply($a, 10, 0);

is($a->quantities_in_stock, 10, "Added 10");

add_supply($a, 15, 0);

is($a->quantities_in_stock, 25, ".. added another 15");

# # Draft supply.
add_supply($a, 15, 1);
is($a->quantities_in_stock, 25, "A draft supply shouldn't count.");

# Sold 15 items
add_sale($a, 15, 0);
is($a->quantities_in_stock, 10, "Just sold 10.");

sub add_supply {
my $record = shift;
my $quantity = shift;
my $draft = shift;

my $s = Supply->create(
provider => $good_company,
draft => $draft
);

SupplyCommodity->create(
supply => $s,
commodity => $record,
quantity => $quantity
);
}

sub add_sale {
my $record = shift;
my $quantity = shift;
my $draft = shift;

my $s = Sale->create(
consumer => $good_consumer,
draft => $draft
);

SaleCommodity->create(
sale => $s,
commodity => $record,
quantity => $quantity
);
}


Or see it here: t/commodity-quantities-in-stock.t. Also take a look how I load fixtures in Fixtures.pm



Since it simplyfies the code to create new records on-the-fly. Now I feel much better to write more tests in my Jifty app. :)

Labels: , ,