merbでGeneratorプラグインを作る方法を紹介します。

merbのgeneratorの仕組みは、 templater という汎用的なGeneratorフレームワークを使って作られています。 なので、基本的にはtemplaterを使ったGeneratorの作り方という事になります。

まずは、Generatorプラグインのひな形を生成します。

   1  % merb-gen plugin hello

Gemの形で提供されるものであれば、merb-pluginの形式である必要は無いですが、簡単にそれを用意してくれるので、merb-gen pluginを使ってみました。

さて、続いて、GEM_ROOTにGeneratorsというファイルを作ります。

   1  scope 'merb-gen' do
   2    dir = File.join(File.dirname(__FILE__), 'lib', 'generators/')
   3    Merb.add_generators dir + 'hello_generator'
   4  end

続いて、GEM_ROOT/lib以下のディレクトリ構成を以下のような感じにします。

   1  lib
   2  `-- generators
   3      |-- hello_generator.rb
   4      `-- templates
   5          `-- hello
   6              |-- app
   7              |   `-- app以下のファイル群
   8              `-- spec
   9                  `-- spec以下のファイル群

hello_generator.rbが、Railsのgeneratorで言うところの Generatorマニフェストファイルに相当します。

以下はmerb-mailerのgeneratorファイルの例です。

   1  module Merb::Generators
   2    class MailerGenerator < NamespacedGenerator
   3  
   4      def self.source_root
   5        File.dirname(__FILE__) / 'templates' / 'mailer'
   6      end
   7  
   8      desc <<-DESC
   9        Generates a mailer
  10      DESC
  11  
  12      option :testing_framework, :desc => 'Testing framework to use (one of: rspec, test_unit)'
  13  
  14      first_argument :name, :required => true, :desc => "mailer name"
  15  
  16      template :mailer do |t|
  17        t.source = 'app/mailers/%file_name%_mailer.rb'
  18        t.destination = File.join("app/mailers", base_path, "#{file_name}_mailer.rb")
  19      end
  20  
  21      template :notify_on_event do |t|
  22        t.source = 'app/mailers/views/%file_name%_mailer/notify_on_event.text.erb'
  23        t.destination = File.join("app/mailers/views", base_path, "#{file_name}_mailer/notify_on_event.text.erb")
  24      end
  25  
  26      template :controller_spec, :testing_framework => :rspec do |t|
  27        t.source = 'spec/mailers/%file_name%_mailer_spec.rb'
  28        t.destination = File.join("spec/mailers", base_path, "#{file_name}_mailer_spec.rb")
  29      end
  30  
  31    end
  32  
  33    add :mailer, MailerGenerator
  34  end        

あとは、GemをインストールすればOK.

posted by Png genki on Thu 5 Feb 2009 at 05:40

Comments:

or Preview
Social Bookmarks
  • Delicious
  • B_entry1274
  • Clip_16_12_w
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ