sinatra(とrails) -...
TRANSCRIPT
Sinatra( と Rails)
たきぐち (atakig)@minami.rb
メニュー
1.自己紹介2.Sinatra の概要3.Rails との比較4.workshop
自己紹介✗システム開発の仕事✗Emacs, Mac, CLI 好き✗✗Ruby を知ったのはたぶん 2000 年✗これまで書いた Ruby のコード数はたかが知れている
✗rails は 0.9 で少し触った✗sinatra は 1 画面のサイト作ったのみ
✗Ruby Association Certified Ruby Programmer Silver
✗twitter:atakig
sinatra とはsinatra は ruby で下記のような最小労力で手早くウェブア
プリケーションを作成するための DSL です。(http://www.sinatrarb.com/intro-jp.html )
# myapp.rb require 'rubygems' require 'sinatra' get '/' do 'Hello world!' End
>ruby myapp.rb
sinatra とは
JavaのHello Worldと比べると
public class HelloWorld { public static void main (String[] args) { System.out.println("Hello World !!"); }}
sinatra とは
DSL:特定のタスク向けに設計されたプログラミング言語を意味する。
汎用プログラミング言語の対義語
例 :SQL, 正規表現
Sinatra と Rails(0.9) の比較
sinatra rails1 結合
度疎結合データベース連携や、テンプレートエンジンに好きなものを選べる
密結合事前に組み込まれたActiveXXX 等が前提となっている
2 構成ファイル
少ない 多い
3 学習コスト
少し たくさん( コマンド多し !)
Sinatra と Rails 比較その 2sinatra Rails
1 大人数での開発 フリーダム過ぎて向いていない
ファイル構成、命名規約等がしっかりしているため、適用可能
2 実績 ??? たくさんあり3 大規模サイトを作
るとしたらファイル構成がカオスになり、収集がつかなくなりそう
上の上と同じ
4 情報量 あまりないし、あっても英語の場合が多い
上の上の上と同じ
O/R マッパーを題材に少し比較してみます。Active Record と Sequel の比較になるという突っこみは不用です。
Sequel のいいなと思う点●テーブル定義用のクラスが不用●プライマリキーは何でも OK
Sinatra と Rails 比較O/R マッパー
操作 種別 コードPKでの検索
AR Model.find(1)Seq DB[:table].filter(:id=>1)
Whereでの検索
AR Model.find(“id = :id AND name = :name”, {:id=>1, :name => “yalab” })
Seq DB[:table].filter(:id=>1, :name=>'yalab')表結合 AR Model.find(:all, :include=>[:other])
Seq DB[:table].left_outer_join(:other, :id=>:outer_key)UPDATE AR Model.update(1, :name=>'Who???')
Seq DB[:table].filter(:id=>1).update(:name=>'Who???')INSERT AR Model.new(:id=>2, :name='atakig')
Seq DB[:table].insert(:id=>2, :name=>'atakig')delete AR Model.delete(2)
Seq DB[:table].filter(:id=>2).delete
種別の「 AR 」は Active Record, 「 Seq 」は Sequel を指します
Sinatra と Rails 比較O/R マッパー
実習 : Hello World# app.rb require 'rubygems' require 'sinatra' require 'sinatra/reloader'
get '/' do 'Hello world!' end
実習 : Routing
# app.rb require 'rubygems' require 'sinatra' require 'sinatra/reloader'
get '/' do 'Hello world!' end
get '/name/:name' do "Hello #{params[:name]}!" end
実習 :テンプレート
#app.rbget '/haml' do @hello='はろう ' @world='わーるど ' haml :indexend
#views/index.haml%table %tr %td=@hello %tr %td=@world
Hamlの基本● % + タグ名でタグが生成● % タグ名の後に =ruby コードで ruby コードの評価値がタグの内容となる
● % タグ名 +ruby ハッシュで属性を指定可能例: %div{:class=>'code', :id='message'}
● % タグ名が指定されない場合は <div> タグが自動的に設定
● 「 . 」と文字列で文字列がクラス名になる● 「 # 」と文字列で文字列が id になる
例 : .item{:id => "item#{item.id}"}= item.body
<div class='item' id='item<%= item.id %>'> <%= item.body %></div>
● 参考URL: http://haml-lang.com/tutorial.html
実習:テストツールリファクタリング :Ruby エディションの p.30 に「コードを書きながら、 Test::Unit か RSpec を使ってテストをしていないプログラマは、 Ruby ディベロッパとしてまともに扱われなくなるだろう」というセリフがありますが、テストツールの使い方を実習します。
sinatra でテストツールを使う場合は、 rack/test とテストツールを組み合わせて行う●test/unit●rspec●bacon●test/spec
test::unit require 'app'require 'test/unit'require 'rack/test'
class MyAppTest < Test::Unit::TestCase include Rack::Test::Methods
def app Sinatra::Application end
def test_root get '/' assert_equal 'Hello world!', last_response.body end
def test_haml get '/haml' assert last_response.ok? assert last_response.successful? assert last_response.match /<td> はろう !<\/td>/ assert last_response=~/<td> わーるど<\/td>/ end end
rspec require 'app'require 'spec'require 'rack/test'
describe 'sinatra_rspec_test' do include Rack::Test::Methods
def app Sinatra::Application end
it "test_my_default" do get '/' last_response.body.should == 'Hello world!' last_response.should be_ok end
it "test_haml” do get '/haml' last_response.headers["Content-Type"].should == "text/html" last_response.should be_successful last_response.status.should == 200 (last_response.body =~ /<td> はろう !<\/td>/).should be_true last_response.body.match(/<td> わーるど<\/td>/).should be_true endend
中規模開発にあたり
ファイルの分割は可能
public/myapp.rbpublic/my_view/index.haml
public/yourapp.rbpublic/your_view/index.haml
config.ru
config.ru
require 'rubygems'require 'sinatra/base'
require 'public/myapp'require 'public/yourapp'
use MyAppuse YourApp
run Sinatra::Base
public/myapp.rb
class MyApp < Sinatra::Base require 'haml' set :views, File.dirname(__FILE__) + '/my_view' get '/myapp' do haml :index endend
my_view/index.haml%table %tr %td my_index
# rackup config.ru
Railsとの中間地点を埋めるPadrino<http://www.padrinorb.com/>
Padrinoは rubyのフレームワークで、 Sinatraというすばらしいマイクロフレームワークの上に構築されました。Sinatraは素早くシンプルなwebアプリケーションを rubyで作るためのDSLです。
Padrinoは Sinatraを素晴しくしようとし続けている間、 Sinatraで構築するのを楽しく、そしてより発展した
webアプリケーションを簡単に作成できます。
Padrino was created to make it fun and easy to code more advanced web applications while still adhering to the spirit that
makes Sinatra great!