Merbのデフォルト構成にはRailsのような HTMLのsanitizeを行うようなヘルパーメソッドは無いのですが、 Gemで提供されている sanitize というライブラリを使うと、簡単にHTMLのsanitizeを行う事が出来ます。

   1  Sanitize.clean(html, Sanitize::Config::RELAXED)

タグ毎に有効・無効の細かい設定が出来て、使い勝手が良いです。

posted by Png genki on Tue 24 Feb 2009 at 15:28

gemファイルはRubyのライブラリ配布用のアーカイブ的なファイル形式ですが、 今回はこれを展開して中身を取り出す方法を紹介します。

まずは、適当なgemファイルを用意して、tarコマンドで展開します。

   1  % tar xvf foo.gem
   2  % ls
   3  data.tar.gz         metadata.gz

このように、data.tar.gzとmetadata.gzという二つのファイルが出てきます。

続いて、data.tar.gzを展開すると、gemspecファイルに記載されている ファイル群一式が展開されます。

   1  % tar data.tar.gz

metadata.gzは、gzipで圧縮されたYAML形式のgemspecです。

追記

gemファイルを展開する方法としては、gem unpackという専用のコマンドがあるようです。見落としていた。

posted by Png genki on Sat 14 Feb 2009 at 16:35 with 2 comments

RubyGemsをインストールする時に、--source オプションによってgemリポジトリを指定する事が出来ますが、 これを指定した場合、そのGemが依存するGemも同じsourceから取ってこようとするため、 依存するGem一式を全てそのgemリポジトリ上に用意する必要があります。

これはなかなか大変なので、--source指定した場合には、 指定した場所に無い場合、システムに設定されているデフォルトのgemリポジトリに探しにいってくれるとありがたいですね。

posted by Png genki on Fri 13 Feb 2009 at 09:25

methopara is a gem that enables us to use Method#parameters with Ruby-1.9.1. It was originally made by @ko1_twitter, the creater of YARV, at the previous meet-up of asakusa.rb.

And this time, I added an interface to use it for UnboundMethod. Now you can use UnboundMethod#parameters with new methopara, like this.

   1  require 'rubygems'
   2  require 'methopara'
   3  
   4  class Foo
   5    def foo(a,b) end
   6  end
   7  Foo.instance_method(:foo).parameters
   8  #=> [[:req, :a], [:req, :b]]

Enjoy!

posted by Png takiuchi on Wed 11 Feb 2009 at 16:26

Yesterday (Feb 10, 2009), asakusa.rb took place at Akihabara, Tokyo. asakusa.rb is a rubyist community that is based on Asakusa.

In the monthly meet-up of that community, @ko1_twitter wrote a code snippet. This snippet is a kind of backport of Method#parameters for Ruby-1.9.1. His quick job led a way for merbists to change merb to be corresponding to Ruby-1.9.1. He named his work "methopara". It stands for METHOd PARAmeters.

And I made it as a gem for ease of use. Now we can use it by the command below.

   1  % sudo gem install methopara --source http://merbi.st

Usage is as follows.

   1  >> require "rubygems"
   2  >> require "methopara"
   3  >> def foo(a,b=nil,&c) end
   4  >> method(:foo).parameters
   5  => [[:req, :a], [:opt, :b], [:block, :c]]

This gem is only for Ruby-1.9.1. Because Ruby18x have merb-action-args, and Ruby-1.9.2 and its successors will have built-in Method#parameters.

Enjoy!

See Also

posted by Png genki on Wed 11 Feb 2009 at 10:35 with 1 comment

Gemをインストールすると、依存するGemも一緒にインストールする事が出来ますが、 Gemの依存関係は、通常の依存関係の他に、開発環境用の依存関係も設定されています。 開発環境用の依存関係は、そのGemの開発や修正を行うために必要なGemを表しています。 今回は、開発環境用の依存関係をまとめてインストールする方法を紹介します。

   1  % sudo gem install --development gemname

これでOK.

posted by Png genki on Wed 4 Feb 2009 at 03:06

I made a simple plugin to explain a concept of render-filters.

merb_render_filter

In your controller,

   1  class Posts < Application
   2    before_render :set_title1, :only => :show
   3    before :set_title2, :only => :show
   4  
   5    def show(id)
   6      @post = Post.get(id)
   7      display @post
   8    end
   9  
  10  private
  11    def set_title1
  12      @title = @post.title # <= you can access to @post here
  13    end
  14  
  15    def set_title2
  16      @title = @post.title #=> @post is nil!
  17    end
  18  end

Without this plugin, you couldn't access to instance variables from before-filters. Of course you can prepare @post in the first before-filter so that you can access from other before-filters. But why Merb has the action-args? They are ignored in such case. This was the problem I wanted to solve by the plugin.

posted by Png takiuchi on Sun 1 Feb 2009 at 10:19

Merbを使ってWebアプリケーションを開発している or しようと考えている方の為に、 お勧めのプラグイン/Slice情報を共有するサービス Merbist Plugins をテスト公開いたします。

ss

デザインもScaffoldに毛が生えた程度で、 この手のサービスには欠かせないRatingなどの基本機能が無い状態なのですが、 そもそもMerbのプラグイン情報を共有する場所が無いので、 無いよりはちょっとましかなという事で公開しておきます。

merb_rating, dm-has-rating のようなプラグインを見つけたら、 あるいは作ったら、Rating出来るようになると思います。

ちなみに、新着プラグインのFeedも配信しています。

よろしくお願いします。

posted by Png genki on Sat 24 Jan 2009 at 02:50

I released the merb_full_url plugin that provides URL which has origin (scheme, host and port)

You can install this gem like this;

   1  % sudo gem install merb_full_url --source http://merbi.st

And you get full URLs by calling full_url/full_resource methods instead of url/resource.

But I think, it is better for merb to become providing such methods in advance :-)

posted by Png takiuchi on Sat 24 Jan 2009 at 02:30

githubの仕様変更 により、githubでEdgeGem (EdgeのコードをGemにまとめたもの) を常にフレッシュな状態で公開する事が難しくなってしまったので、 Merbist向けにプラグイン配布用のGemサーバを用意しました。

gems.rubyforge.orgやgems.github.comなどの通常のGemサーバと同様に、以下のようにsourcesに登録して使う事ができます。

   1  % sudo gem sources -a http://merbi.st

仕組みとしては、http://merbi.st/fetch にアクセスされると、 登録されているgithub上のリポジトリから、Edgeのコードがpullされ、 GemとGemサーバ用のインデックスデータを作成します。

現時点では、以下のGemを公開しています。

   1  % gem list -r -s http://merbi.st
   2  
   3  *** REMOTE GEMS ***
   4  
   5  dm-has-versions (0.1.1)
   6  dm-pagination (0.1.1)
   7  merb_babel (0.1.2.2)
   8  merb_component (0.1.1)
   9  merb_recognize_path (0.0.2)
  10  merb_slice-gen (0.0.2)
  11  merb_timezone_select (0.0.2)
  12  pagination_scope (0.0.8)
  13  rttool (1.0.2)

サーバの負荷の面で不安があるので、 現時点では同期するリポジトリの登録は管理者のみに制限していますが、 http://merbi.st/plugins よりプラグイン情報を登録していただければ(要アカウント作成)、 問題が無い限り定期的に確認して同期リストに追加いたします。

反応がない場合は@takiuchi までご一報ください。

posted by Png genki on Fri 23 Jan 2009 at 05:16