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

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

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

pre>>
% merb-gen plugin hello
<<--

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

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

ruby>>
scope 'merb-gen' do
dir = File.join(File.dirname(FILE), 'lib', 'generators/')
Merb.add_generators dir + 'hello_generator'
end
<<--

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

pre>>
lib
-- generators |-- hello_generator.rb -- templates
-- hello |-- app | -- app以下のファイル群
-- spec -- spec以下のファイル群
<<--

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

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

ruby>>
module Merb::Generators
class MailerGenerator < NamespacedGenerator

def self.source_root
  File.dirname(__FILE__) / 'templates' / 'mailer'
end

desc <<-DESC
  Generates a mailer
DESC

option :testing_framework, :desc => 'Testing framework to use (one of: rspec, test_unit)'

first_argument :name, :required => true, :desc => "mailer name"

template :mailer do |t|
  t.source = 'app/mailers/%file_name%_mailer.rb'
  t.destination = File.join("app/mailers", base_path, "#{file_name}_mailer.rb")
end

template :notify_on_event do |t|
  t.source = 'app/mailers/views/%file_name%_mailer/notify_on_event.text.erb'
  t.destination = File.join("app/mailers/views", base_path, "#{file_name}_mailer/notify_on_event.text.erb")
end

template :controller_spec, :testing_framework => :rspec do |t|
  t.source = 'spec/mailers/%file_name%_mailer_spec.rb'
  t.destination = File.join("spec/mailers", base_path, "#{file_name}_mailer_spec.rb")
end

end

add :mailer, MailerGenerator
end
<<--

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

posted by genki genki on Thu 5 Feb 2009 at 05:39 with 0 comments
Contents rssrss
光ファイバーを二次元振動させて走査するAR用ディスプレイ
因果の取り違え
Swift2's defer for CoffeeScript
mongodb-3.0からcreateIndexのdropDupsが無くなったらしい
mongodb-3.0以降のWiredTigerの設定を動的に変更する方法
一般楕円の高速生成アルゴリズムへの道標
farro mantecatoのレシピ
Droonga関連の記事のまとめ
RuntimeErrorの特定のメッセージに限定してrescueする方法
jQueryでscriptタグを実行せずにappendする
Tags
coffeescriptdefergroongajsmemonodenodejs
Comments rssrss
https://xn--krken21-bn4c.com https://xn--krken21-bn4c.com: Как отличить настоящий маркетплейс от подделки Kra... 05/01 12:20
https://example.com/xkkpsk https://example.com/xkkpsk: Great blog you have here.. It's difficult to find ... 05/01 09:31
https://onlinemo.ir https://onlinemo.ir: جمع‌بندی برای اون گروه از کاربرا که سیستم‌ها... 05/01 00:58
https://xn--mgmarkt9-9db.com https://xn--mgmarkt9-9db.com: Как обезопасить MEGA: настройка менеджера паролей ... 04/30 20:14
https://qeshmtourist.ir https://qeshmtourist.ir: در کل قضیه برای اونایی که می‌خوان وارد بشن س... 04/30 19:31
https://negarinadv.ir https://negarinadv.ir: سلام و عرض ادب، بنده مدتی قبل هنگام گشتن تو اینت... 04/30 18:56
https://gsmcharge.ir https://gsmcharge.ir: در کل قضیه برای کاربران علاقه‌مند به پلتفرم‌... 04/30 13:51
https://azardl.ir https://azardl.ir: در کل برای کاربرایی که در جستجو هستن پلتفرم‌... 04/30 02:42