SHOEISHA iD

※旧SEメンバーシップ会員の方は、同じ登録情報(メールアドレス&パスワード)でログインいただけます

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

Ruby on Rails + Curl(AD)

Ruby on Rails + Curl
Curl用Scaffoldを自作する

第3回

  • このエントリーをはてなブックマークに追加

ダウンロード サンプルソース (5.4 KB)

curl_scaffoldジェネレータのコード

 いよいよcurl_scaffoldジェネレータのコードcurl_scaffold_generator.rbです。コードはscaffold_generator.rbをコピーして作りました。コードを順に見ていきましょう。

manifestメソッド

 curl_scaffoldでは通常のscaffoldに比べ生成するファイルを減らしたので、manifestメソッドはだいぶシンプルになっています。

 ファイル名の衝突チェック、ディレクトリ作成に次いで行われる、テンプレートからのアプリケーションファイル作成はview_start.html.erbとコントローラのみです。後は初期化ファイルsetup_json.rb、ライセンスファイルのコピーを行っています。

 最後にconfig/route.rbの作成とモデルの作成を行います。

class CurlScaffoldGenerator < Rails::Generator::NamedBase

  ・・・scaffold_generator.rbと同じ ・・・
  def def initialize(runtime_args, runtime_options = {})
  ・・・scaffold_generator.rbと同じ ・・・
  end

  def manifest
    record do |m|
      # Check for class naming collisions.
      m.class_collisions"#{controller_class_name}Controller"
      m.class_collisions class_name

      # Controller, helper, views, test and stylesheets directories.
      m.directory File.join('app/models', class_path)
      m.directory File.join('app/controllers', controller_class_path)
      m.directory File.join('app/views', controller_class_path, controller_file_name)
      m.directory File.join('test/unit', class_path)

      m.template "view_start.html.erb",
                 File.join('app/views', controller_class_path, controller_file_name, "start.html.erb")
      m.template 'controller.rb', 
                  File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
                  
      m.file 'setup_json.rb', 'config/initializers/setup_json.rb'
      m.file 'curl-access.txt', 'public/curl-access.txt'
      m.file 'curl-license-5.dat', 'public/curl-license-5.dat'

      m.route_resource ":#{controller_file_name}, :collection => { :start => :get }"

      m.dependency 'model', [name] + @args, :collision => :skip
    end

Curl用属性クラスの作成

 コマンドで指定されたテーブルのカラム情報を保持する属性(GeneratedAttribute)クラスですが、型やデフォルト値の情報はRuby on Rails用なのでCurl用の型やデフォルト値を戻してくれるGeneratedCurlAttributeクラスを定義しました。またview_start.html.erbテンプレートの処理で属性配列の最後が分かると便利なので、最終要素を表すlast属性も追加しました。

class CurlScaffoldGenerator < Rails::Generator::NamedBase

・・・省略・・・

  protected
    def attributes
      unless (@attributes)
        @attributes = @args.collect do |attribute|
          GeneratedCurlAttribute.new(*attribute.split(":"))
        end
        @attributes[-1].last = true
      end
      @attributes
    end
end

class GeneratedCurlAttribute
  attr_accessor :name, :type, :column, :last

  def initialize(name, type)
    @name, @type = name, type.to_sym
    @column = ActiveRecord::ConnectionAdapters::Column.new(name, nil, @type)
    @last = false
  end

  def curl_type
    @curl_type ||= case type
      when :integer                     then "int"
      when :string                      then "String"
      else
        raise  "Not supported type in curl_scaffold."
    end      
  end

  def default
    @default ||= case type
      when :integer                     then 0
      when :string                      then ""
      else
        raise  "Not supported type in curl_scaffold."
    end      
  end
end

config/route.rb設定メソッドの作成

 config/route.rbに書かれるルーティング情報の追加メソッドroute_resourcesですが、Scaffold用に作られたものは:collection => { :start => :get }等の情報を付加できないので、やはり専用のメソッドroute_resourceに置き換えています。

module Rails
  module Generator
    module Commands
      class Create < Base
        def route_resource(resource)
          sentinel = 'ActionController::Routing::Routes.draw do |map|'

          logger.route "map.resources #{resource}"
          unless options[:pretend]
            gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
              "#{match}\n  map.resources #{resource}\n"
            end
          end
        end   
      end
    end
  end
end

まとめ

 今回のようにRuby on Railsに独自のコードジェネレータを作る事で、サーバ側のRuby on Railsのプログラムだけではなく、Curlコードの作成も飛躍的な生産性の向上がはかれる事を感じていただけたら幸いです。

この記事は参考になりましたか?

  • このエントリーをはてなブックマークに追加
Ruby on Rails + Curl連載記事一覧

もっと読む

この記事の著者

吉田裕美(ヨシダユウミ)

有限会社 EY-Office 取締役CADのベンチャー企業でCADのコア部分や図面管理システムなどの開発に従事した後、独立しJava,Ruby,PerlでWebアプリを中心に開発してきた。現在は殆どの開発はRuby on Rails。ここ数年はソフトウェアエンジニアの教育に興味をもち、従来の知識偏重な教育ではなく現実の問題を解決できるエンジニアを育てる教育に注力している。またLisp等に関心...

※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です

【AD】本記事の内容は記事掲載開始時点のものです 企画・制作 株式会社翔泳社

この記事は参考になりましたか?

この記事をシェア

  • このエントリーをはてなブックマークに追加
CodeZine(コードジン)
https://codezine.jp/article/detail/4167 2009/07/21 15:00

おすすめ

アクセスランキング

アクセスランキング

イベント

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

新規会員登録無料のご案内

  • ・全ての過去記事が閲覧できます
  • ・会員限定メルマガを受信できます

メールバックナンバー

アクセスランキング

アクセスランキング