mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
Updated Japanese README to follow up latest README.rdoc.
Signed-off-by: Konstantin Haase <konstantin.mailinglists@googlemail.com>
This commit is contained in:
parent
4214342f9b
commit
4fd9aeb83f
1 changed files with 407 additions and 6 deletions
413
README.jp.rdoc
413
README.jp.rdoc
|
@ -79,6 +79,9 @@ Sinatraでは、ルートはHTTPメソッドとURLマッチングパターンが
|
||||||
"Hello, #{c}!"
|
"Hello, #{c}!"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
=== 条件
|
||||||
|
|
||||||
ルートにはユーザエージェントのようなさまざまな条件を含めることができます。
|
ルートにはユーザエージェントのようなさまざまな条件を含めることができます。
|
||||||
|
|
||||||
get '/foo', :agent => /Songbird (\d\.\d)[\d\/]*?/ do
|
get '/foo', :agent => /Songbird (\d\.\d)[\d\/]*?/ do
|
||||||
|
@ -89,6 +92,58 @@ Sinatraでは、ルートはHTTPメソッドとURLマッチングパターンが
|
||||||
# Matches non-songbird browsers
|
# Matches non-songbird browsers
|
||||||
end
|
end
|
||||||
|
|
||||||
|
ほかに+host_name+と+provides+条件が利用可能です:
|
||||||
|
|
||||||
|
get '/', :host_name => /^admin\./ do
|
||||||
|
"Admin Area, Access denied!"
|
||||||
|
end
|
||||||
|
|
||||||
|
get '/', :provides => 'html' do
|
||||||
|
haml :index
|
||||||
|
end
|
||||||
|
|
||||||
|
get '/', :provides => ['rss', 'atom', 'xml'] do
|
||||||
|
builder :feed
|
||||||
|
end
|
||||||
|
|
||||||
|
独自の条件を定義することも簡単にできます:
|
||||||
|
|
||||||
|
set(:probability) { |value| condition { rand <= value } }
|
||||||
|
|
||||||
|
get '/win_a_car', :probability => 0.1 do
|
||||||
|
"You won!"
|
||||||
|
end
|
||||||
|
|
||||||
|
get '/win_a_car' do
|
||||||
|
"Sorry, you lost."
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
=== 戻り値
|
||||||
|
|
||||||
|
ルートブロックの戻り値は、HTTPクライアントまたはRackスタックでの次のミドルウェアに渡されるレスポンスボディを決定します。
|
||||||
|
|
||||||
|
これは大抵の場合、上の例のように文字列ですが、それ以外の値も使用することができます。
|
||||||
|
|
||||||
|
Rackレスポンス、Rackボディオブジェクト、HTTPステータスコードのいずれかとして
|
||||||
|
妥当なオブジェクトであればどのようなオブジェクトでも返すことができます:
|
||||||
|
|
||||||
|
* 3要素の配列: <tt>[ステータス(Fixnum), ヘッダ(Hash), レスポンスボディ(#eachに応答する)]</tt>
|
||||||
|
* 2要素の配列: <tt>[ステータス(Fixnum), レスポンスボディ(#eachに応答する)]</tt>
|
||||||
|
* <tt>#each</tt>に応答し、与えられたブロックに文字列を渡すオブジェクト
|
||||||
|
* ステータスコードを表現するFixnum
|
||||||
|
|
||||||
|
そのように、例えばストリーミングの例を簡単に実装することができます:
|
||||||
|
|
||||||
|
class Stream
|
||||||
|
def each
|
||||||
|
100.times { |i| yield "#{i}\n" }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
get('/') { Stream.new }
|
||||||
|
|
||||||
|
|
||||||
== 静的ファイル
|
== 静的ファイル
|
||||||
|
|
||||||
静的ファイルは<tt>./public</tt>ディレクトリから配信されます。
|
静的ファイルは<tt>./public</tt>ディレクトリから配信されます。
|
||||||
|
@ -146,6 +201,19 @@ hamlを使うにはhamlライブラリが必要です:
|
||||||
|
|
||||||
<tt>./views/index.erb</tt>を表示します。
|
<tt>./views/index.erb</tt>を表示します。
|
||||||
|
|
||||||
|
=== Erubis
|
||||||
|
|
||||||
|
erubisテンプレートを表示するには、erubisライブラリが必要です:
|
||||||
|
|
||||||
|
## erubisを読み込みます
|
||||||
|
require 'erubis'
|
||||||
|
|
||||||
|
get '/' do
|
||||||
|
erubis :index
|
||||||
|
end
|
||||||
|
|
||||||
|
<tt>./views/index.erubis</tt>を表示します。
|
||||||
|
|
||||||
=== Builder テンプレート
|
=== Builder テンプレート
|
||||||
|
|
||||||
builderを使うにはbuilderライブラリが必要です:
|
builderを使うにはbuilderライブラリが必要です:
|
||||||
|
@ -196,6 +264,173 @@ see {Options and Configurations}[http://www.sinatrarb.com/configuration.html],
|
||||||
sass :stylesheet, :sass_options => {:style => :expanded } # 上書き
|
sass :stylesheet, :sass_options => {:style => :expanded } # 上書き
|
||||||
end
|
end
|
||||||
|
|
||||||
|
=== Scss テンプレート
|
||||||
|
|
||||||
|
Scssテンプレートを使うにはsassライブラリが必要です:
|
||||||
|
|
||||||
|
## hamlかsassを読み込みます
|
||||||
|
require 'sass'
|
||||||
|
|
||||||
|
get '/stylesheet.css' do
|
||||||
|
scss :stylesheet
|
||||||
|
end
|
||||||
|
|
||||||
|
<tt>./views/stylesheet.scss</tt>を表示します。
|
||||||
|
|
||||||
|
{Sass' options}[http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#options]
|
||||||
|
はSinatraの設定でグローバルに設定することができます。
|
||||||
|
see {Options and Configurations}[http://www.sinatrarb.com/configuration.html],
|
||||||
|
を参照してそれぞれ設定を上書きして下さい。
|
||||||
|
|
||||||
|
set :scss, :style => :compact # デフォルトのScss styleは:nested
|
||||||
|
|
||||||
|
get '/stylesheet.css' do
|
||||||
|
scss :stylesheet, :style => :expanded # 上書き
|
||||||
|
end
|
||||||
|
|
||||||
|
=== Less テンプレート
|
||||||
|
|
||||||
|
Lessテンプレートを使うにはlessライブラリが必要です:
|
||||||
|
|
||||||
|
## lessを読み込みます
|
||||||
|
require 'less'
|
||||||
|
|
||||||
|
get '/stylesheet.css' do
|
||||||
|
less :stylesheet
|
||||||
|
end
|
||||||
|
|
||||||
|
<tt>./views/stylesheet.less</tt>を表示します。
|
||||||
|
|
||||||
|
=== Liquid テンプレート
|
||||||
|
|
||||||
|
Liquidテンプレートを使うにはliquidライブラリが必要です:
|
||||||
|
|
||||||
|
## liquidを読み込みます
|
||||||
|
require 'liquid'
|
||||||
|
|
||||||
|
get '/' do
|
||||||
|
liquid :index
|
||||||
|
end
|
||||||
|
|
||||||
|
<tt>./views/index.liquid</tt>を表示します。
|
||||||
|
|
||||||
|
LiquidテンプレートからRubyのメソッド(+yield+を除く)を呼び出すことができないため、
|
||||||
|
ほぼ全ての場合にlocalsを指定する必要があるでしょう:
|
||||||
|
|
||||||
|
liquid :index, :locals => { :key => 'value' }
|
||||||
|
|
||||||
|
=== Markdown テンプレート
|
||||||
|
|
||||||
|
Markdownテンプレートを使うにはrdiscountライブラリが必要です:
|
||||||
|
|
||||||
|
## rdiscountを読み込みます
|
||||||
|
require "rdiscount"
|
||||||
|
|
||||||
|
get '/' do
|
||||||
|
markdown :index
|
||||||
|
end
|
||||||
|
|
||||||
|
<tt>./views/index.markdown</tt>を表示します。(+md+と+mkd+も妥当な拡張子です)
|
||||||
|
|
||||||
|
markdownからメソッドを呼び出すことも、localsに変数を渡すこともできません。
|
||||||
|
それゆえ、他のレンダリングエンジンとの組み合わせで使うのが普通です:
|
||||||
|
|
||||||
|
erb :overview, :locals => { :text => markdown(:introduction) }
|
||||||
|
|
||||||
|
他のテンプレートからmarkdownメソッドを呼び出してもよいことに注意してください:
|
||||||
|
|
||||||
|
%h1 Hello From Haml!
|
||||||
|
%p= markdown(:greetings)
|
||||||
|
|
||||||
|
=== Textile テンプレート
|
||||||
|
|
||||||
|
Textileテンプレートを使うにはRedClothライブラリが必要です:
|
||||||
|
|
||||||
|
## redclothを読み込みます
|
||||||
|
require "redcloth"
|
||||||
|
|
||||||
|
get '/' do
|
||||||
|
textile :index
|
||||||
|
end
|
||||||
|
|
||||||
|
<tt>./views/index.textile</tt>を表示します。
|
||||||
|
|
||||||
|
textileからメソッドを呼び出すことも、localsに変数を渡すこともできません。
|
||||||
|
それゆえ、他のレンダリングエンジンとの組み合わせで使うのが普通です:
|
||||||
|
|
||||||
|
erb :overview, :locals => { :text => textile(:introduction) }
|
||||||
|
|
||||||
|
他のテンプレートからtextileメソッドを呼び出してもよいことに注意してください:
|
||||||
|
|
||||||
|
%h1 Hello From Haml!
|
||||||
|
%p= textile(:greetings)
|
||||||
|
|
||||||
|
=== RDoc テンプレート
|
||||||
|
|
||||||
|
RDocテンプレートを使うにはRDocライブラリが必要です:
|
||||||
|
|
||||||
|
## rdocを読み込みます
|
||||||
|
require "rdoc"
|
||||||
|
|
||||||
|
get '/' do
|
||||||
|
rdoc :index
|
||||||
|
end
|
||||||
|
|
||||||
|
<tt>./views/index.rdoc</tt>を表示します。
|
||||||
|
|
||||||
|
rdocからメソッドを呼び出すことも、localsに変数を渡すこともできません。
|
||||||
|
それゆえ、他のレンダリングエンジンとの組み合わせで使うのが普通です:
|
||||||
|
|
||||||
|
erb :overview, :locals => { :text => rdoc(:introduction) }
|
||||||
|
|
||||||
|
他のテンプレートからrdocメソッドを呼び出してもよいことに注意してください:
|
||||||
|
|
||||||
|
%h1 Hello From Haml!
|
||||||
|
%p= rdoc(:greetings)
|
||||||
|
|
||||||
|
=== Radius テンプレート
|
||||||
|
|
||||||
|
Radiusテンプレートを使うにはradiusライブラリが必要です:
|
||||||
|
|
||||||
|
## radiusを読み込みます
|
||||||
|
require 'radius'
|
||||||
|
|
||||||
|
get '/' do
|
||||||
|
radius :index
|
||||||
|
end
|
||||||
|
|
||||||
|
<tt>./views/index.radius</tt>を表示します。
|
||||||
|
|
||||||
|
RadiusテンプレートからRubyのメソッド(+yield+を除く)を呼び出すことができないため、
|
||||||
|
ほぼ全ての場合にlocalsを指定する必要があるでしょう:
|
||||||
|
|
||||||
|
radius :index, :locals => { :key => 'value' }
|
||||||
|
|
||||||
|
=== Markaby テンプレート
|
||||||
|
|
||||||
|
Markabyテンプレートを使うにはmarkabyライブラリが必要です:
|
||||||
|
|
||||||
|
## markabyを読み込みます
|
||||||
|
require 'markaby'
|
||||||
|
|
||||||
|
get '/' do
|
||||||
|
markaby :index
|
||||||
|
end
|
||||||
|
|
||||||
|
<tt>./views/index.mab</tt>を表示します。
|
||||||
|
|
||||||
|
=== CoffeeScript テンプレート
|
||||||
|
|
||||||
|
CoffeeScriptテンプレートを表示するにはcoffee-scriptライブラリと`coffee`バイナリが必要です:
|
||||||
|
|
||||||
|
## coffee-scriptを読み込みます
|
||||||
|
require 'coffee-script'
|
||||||
|
|
||||||
|
get '/application.js' do
|
||||||
|
coffee :application
|
||||||
|
end
|
||||||
|
|
||||||
|
<tt>./views/application.coffee</tt>を表示します。
|
||||||
|
|
||||||
=== インラインテンプレート
|
=== インラインテンプレート
|
||||||
|
|
||||||
|
@ -245,7 +480,7 @@ see {Options and Configurations}[http://www.sinatrarb.com/configuration.html],
|
||||||
%div.title Hello world!!!!!
|
%div.title Hello world!!!!!
|
||||||
|
|
||||||
注意: sinatraをrequireするファイル内で定義されたファイル内テンプレートは自動的に読み込まれます。
|
注意: sinatraをrequireするファイル内で定義されたファイル内テンプレートは自動的に読み込まれます。
|
||||||
他のファイルで定義されているテンプレートを使うには <tt>use_in_file_templates!</tt>メソッドで指定します。
|
他のファイルで定義されているテンプレートを使うには <tt>enable :inline_templates</tt>を明示的に呼んでください。
|
||||||
|
|
||||||
=== 名前付きテンプレート
|
=== 名前付きテンプレート
|
||||||
|
|
||||||
|
@ -264,7 +499,7 @@ see {Options and Configurations}[http://www.sinatrarb.com/configuration.html],
|
||||||
end
|
end
|
||||||
|
|
||||||
「layout」というテンプレートが存在する場合、そのテンプレートファイルは他のテンプレートが
|
「layout」というテンプレートが存在する場合、そのテンプレートファイルは他のテンプレートが
|
||||||
表示される度に使用されます。<tt>:layout => false</tt>.することでlayoutsを無効にできます。
|
表示される度に使用されます。<tt>:layout => false</tt>することでlayoutsを無効にできます。
|
||||||
|
|
||||||
get '/' do
|
get '/' do
|
||||||
haml :index, :layout => !request.xhr?
|
haml :index, :layout => !request.xhr?
|
||||||
|
@ -301,12 +536,36 @@ beforeフィルタはリクエストされたコンテキストを実行する
|
||||||
params[:splat] #=> 'bar/baz'
|
params[:splat] #=> 'bar/baz'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
afterフィルタは同じコンテキストにあるリクエストの後に評価され、
|
||||||
|
同じくリクエストとレスポンスを変更することができます。
|
||||||
|
beforeフィルタとルートで設定されたインスタンス変数は、
|
||||||
|
afterフィルタからアクセスすることができます:
|
||||||
|
|
||||||
|
after do
|
||||||
|
puts response.status
|
||||||
|
end
|
||||||
|
|
||||||
|
フィルタにはオプションとしてパターンを渡すことができ、
|
||||||
|
この場合はリクエストのパスがパターンにマッチした場合のみフィルタが評価されます:
|
||||||
|
|
||||||
|
before '/protected/*' do
|
||||||
|
authenticate!
|
||||||
|
end
|
||||||
|
|
||||||
|
after '/create/:slug' do |slug|
|
||||||
|
session[:last_slug] = slug
|
||||||
|
end
|
||||||
|
|
||||||
== 強制終了
|
== 強制終了
|
||||||
|
|
||||||
ルートかbeforeフィルタ内で直ちに実行を終了する方法:
|
ルートかbeforeフィルタ内で直ちに実行を終了する方法:
|
||||||
|
|
||||||
halt
|
halt
|
||||||
|
|
||||||
|
ステータスを指定することができます:
|
||||||
|
|
||||||
|
halt 410
|
||||||
|
|
||||||
body部を指定することもできます ...
|
body部を指定することもできます ...
|
||||||
|
|
||||||
halt 'ここにbodyを書く'
|
halt 'ここにbodyを書く'
|
||||||
|
@ -315,6 +574,10 @@ body部を指定することもできます ...
|
||||||
|
|
||||||
halt 401, '立ち去れ!'
|
halt 401, '立ち去れ!'
|
||||||
|
|
||||||
|
ヘッダを指定:
|
||||||
|
|
||||||
|
halt 402, {'Content-Type' => 'text/plain'}, 'リベンジ'
|
||||||
|
|
||||||
== パッシング(Passing)
|
== パッシング(Passing)
|
||||||
|
|
||||||
ルートは<tt>pass</tt>を使って次のルートに飛ばすことができます:
|
ルートは<tt>pass</tt>を使って次のルートに飛ばすことができます:
|
||||||
|
@ -386,13 +649,13 @@ body部を指定することもできます ...
|
||||||
...
|
...
|
||||||
end
|
end
|
||||||
|
|
||||||
環境変数<tt>:production</tt>(RACK_ENV環境変数) がセットされている時だけ実行する方法:
|
環境(RACK_ENV環境変数)が<tt>:production</tt>に設定されている時だけ実行する方法:
|
||||||
|
|
||||||
configure :production do
|
configure :production do
|
||||||
...
|
...
|
||||||
end
|
end
|
||||||
|
|
||||||
環境変数<tt>:production</tt> か<tt>:test</tt>の場合に設定する方法:
|
環境が<tt>:production</tt>か<tt>:test</tt>の場合に設定する方法:
|
||||||
|
|
||||||
configure :production, :test do
|
configure :production, :test do
|
||||||
...
|
...
|
||||||
|
@ -415,7 +678,7 @@ body部を指定することもできます ...
|
||||||
=== エラー
|
=== エラー
|
||||||
|
|
||||||
+error+ ハンドラーはルートブロックかbeforeフィルタ内で例外が発生した時はいつでも発動します。
|
+error+ ハンドラーはルートブロックかbeforeフィルタ内で例外が発生した時はいつでも発動します。
|
||||||
block or before filter. 例外オブジェクトはRack変数<tt>sinatra.error</tt>から取得されます。
|
例外オブジェクトはRack変数<tt>sinatra.error</tt>から取得できます。
|
||||||
|
|
||||||
error do
|
error do
|
||||||
'エラーが発生しました。 - ' + env['sinatra.error'].name
|
'エラーが発生しました。 - ' + env['sinatra.error'].name
|
||||||
|
@ -437,6 +700,23 @@ block or before filter. 例外オブジェクトはRack変数<tt>sinatra.error</
|
||||||
|
|
||||||
エラーメッセージ... 何かがまずかったようです
|
エラーメッセージ... 何かがまずかったようです
|
||||||
|
|
||||||
|
あるいは、ステータスコードに対応するエラーハンドラを設定することもできます:
|
||||||
|
|
||||||
|
error 403 do
|
||||||
|
'Access forbidden'
|
||||||
|
end
|
||||||
|
|
||||||
|
get '/secret' do
|
||||||
|
403
|
||||||
|
end
|
||||||
|
|
||||||
|
範囲指定もできます:
|
||||||
|
|
||||||
|
error 400..510 do
|
||||||
|
'Boom'
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
開発環境として実行している場合、Sinatraは特別な<tt>not_found</tt>と<tt>error</tt>ハンドラーを
|
開発環境として実行している場合、Sinatraは特別な<tt>not_found</tt>と<tt>error</tt>ハンドラーを
|
||||||
インストールしています。
|
インストールしています。
|
||||||
|
|
||||||
|
@ -447,6 +727,10 @@ block or before filter. 例外オブジェクトはRack変数<tt>sinatra.error</
|
||||||
|
|
||||||
mime_type :foo, 'text/foo'
|
mime_type :foo, 'text/foo'
|
||||||
|
|
||||||
|
これはcontent_typeヘルパで利用することができます:
|
||||||
|
|
||||||
|
content_type :foo
|
||||||
|
|
||||||
== Rackミドルウェア
|
== Rackミドルウェア
|
||||||
|
|
||||||
SinatraはRack[http://rack.rubyforge.org/]というRubyのWEBフレームワーク用の
|
SinatraはRack[http://rack.rubyforge.org/]というRubyのWEBフレームワーク用の
|
||||||
|
@ -553,16 +837,133 @@ Sinatra::Baseのサブクラスで使えるメソッドはトップレベルのD
|
||||||
{Sinatra::Delegator mixin}[http://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb#L1064]
|
{Sinatra::Delegator mixin}[http://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb#L1064]
|
||||||
{included into the main namespace}[http://github.com/sinatra/sinatra/blob/master/lib/sinatra/main.rb#L25].
|
{included into the main namespace}[http://github.com/sinatra/sinatra/blob/master/lib/sinatra/main.rb#L25].
|
||||||
|
|
||||||
|
=== Sinatraをミドルウェアとして利用する
|
||||||
|
|
||||||
|
Sinatraは他のRackミドルウェアを利用することができるだけでなく、
|
||||||
|
全てのSinatraアプリケーションは、それ自体ミドルウェアとして別のRackエンドポイントの前に追加することが可能です。
|
||||||
|
|
||||||
|
このエンドポイントには、別のSinatraアプリケーションまたは他のRackベースのアプリケーション(Rails/Ramaze/Camping/...)が用いられるでしょう。
|
||||||
|
|
||||||
|
require 'sinatra/base'
|
||||||
|
|
||||||
|
class LoginScreen < Sinatra::Base
|
||||||
|
enable :session
|
||||||
|
|
||||||
|
get('/login') { haml :login }
|
||||||
|
|
||||||
|
post('/login') do
|
||||||
|
if params[:name] = 'admin' and params[:password] = 'admin'
|
||||||
|
session['user_name'] = params[:name]
|
||||||
|
else
|
||||||
|
redirect '/login'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class MyApp < Sinatra::Base
|
||||||
|
# middleware will run before filters
|
||||||
|
use LoginScreen
|
||||||
|
|
||||||
|
before do
|
||||||
|
unless session['user_name']
|
||||||
|
halt "Access denied, please <a href='/login'>login</a>."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
get('/') { "Hello #{session['user_name']}." }
|
||||||
|
end
|
||||||
|
|
||||||
|
== スコープとバインディング
|
||||||
|
|
||||||
|
現在のスコープはどのメソッドや変数が利用可能かを決定します。
|
||||||
|
|
||||||
|
=== アプリケーション/クラスのスコープ
|
||||||
|
|
||||||
|
全てのSinatraアプリケーションはSinatra::Baseのサブクラスに相当します。
|
||||||
|
もしトップレベルDSLを利用しているならば(<tt>require 'sinatra'</tt>)このクラスはSinatra::Applicationであり、
|
||||||
|
そうでなければ、あなたが明示的に作成したサブクラスです。
|
||||||
|
クラスレベルでは`get`や`before`のようなメソッドを持っています。
|
||||||
|
しかし`request`オブジェクトや`session`には、全てのリクエストのために1つのアプリケーションクラスが存在するためアクセスできません。
|
||||||
|
|
||||||
|
`set`によって作られたオプションはクラスレベルのメソッドです:
|
||||||
|
|
||||||
|
class MyApp < Sinatra::Base
|
||||||
|
# Hey, I'm in the application scope!
|
||||||
|
set :foo, 42
|
||||||
|
foo # => 42
|
||||||
|
|
||||||
|
get '/foo' do
|
||||||
|
# Hey, I'm no longer in the application scope!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
次の場所ではアプリケーションスコープバインディングを持ちます:
|
||||||
|
|
||||||
|
* アプリケーションのクラス本体
|
||||||
|
* 拡張によって定義されたメソッド
|
||||||
|
* `helpers`に渡されたブロック
|
||||||
|
* `set`の値として使われるProcまたはブロック
|
||||||
|
|
||||||
|
このスコープオブジェクト(クラス)は次のように利用できます:
|
||||||
|
|
||||||
|
* configureブロックに渡されたオブジェクト経由(<tt>configure { |c| ... }</tt>)
|
||||||
|
* リクエストスコープの中での`settings`
|
||||||
|
|
||||||
|
=== リクエスト/インスタンスのスコープ
|
||||||
|
|
||||||
|
やってくるリクエストごとに、あなたのアプリケーションクラスの新しいインスタンスが作成され、全てのハンドラブロックがそのスコープで実行されます。
|
||||||
|
このスコープの内側からは`request`や`session`オブジェクトにアクセスすることができ、`erb`や`haml`のような表示メソッドを呼び出すことができます。
|
||||||
|
リクエストスコープの内側からは、`settings`ヘルパによってアプリケーションスコープにアクセスすることができます。
|
||||||
|
|
||||||
|
class MyApp << Sinatra::Base
|
||||||
|
# Hey, I'm in the application scope!
|
||||||
|
get '/define_route/:name' do
|
||||||
|
# Request scope for '/define_route/:name'
|
||||||
|
@value = 42
|
||||||
|
|
||||||
|
settings.get("/#{params[:name]}") do
|
||||||
|
# Request scope for "/#{params[:name]}"
|
||||||
|
@value # => nil (not the same request)
|
||||||
|
end
|
||||||
|
|
||||||
|
"Route defined!"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
次の場所ではリクエストスコープバインディングを持ちます:
|
||||||
|
|
||||||
|
* get/head/post/put/delete ブロック
|
||||||
|
* before/after フィルタ
|
||||||
|
* helper メソッド
|
||||||
|
* テンプレート/ビュー
|
||||||
|
|
||||||
|
=== デリゲートスコープ
|
||||||
|
|
||||||
|
デリゲートスコープは、単にクラススコープにメソッドを転送します。
|
||||||
|
しかしながら、クラスのバインディングを持っていないため、クラススコープと全く同じふるまいをするわけではありません:
|
||||||
|
委譲すると明示的に示されたメソッドのみが利用可能であり、またクラススコープと変数/状態を共有することはできません(注: 異なった`self`を持っています)。
|
||||||
|
<tt>Sinatra::Delegator.delegate :method_name</tt>を呼び出すことによってデリゲートするメソッドを明示的に追加することができます。
|
||||||
|
|
||||||
|
次の場所ではデリゲートスコープを持ちます:
|
||||||
|
|
||||||
|
* もし<tt>require "sinatra"</tt>しているならば、トップレベルバインディング
|
||||||
|
* `Sinatra::Delegator` mixinでextendされたオブジェクト
|
||||||
|
|
||||||
|
コードをご覧ください: ここでは
|
||||||
|
{Sinatra::Delegator mixin}[http://github.com/sinatra/sinatra/blob/ceac46f0bc129a6e994a06100aa854f606fe5992/lib/sinatra/base.rb#L1128]
|
||||||
|
は{main 名前空間にincludeされています}[http://github.com/sinatra/sinatra/blob/ceac46f0bc129a6e994a06100aa854f606fe5992/lib/sinatra/main.rb#L28].
|
||||||
|
|
||||||
== コマンドライン
|
== コマンドライン
|
||||||
|
|
||||||
Sinatraアプリケーションは直接実行できます。
|
Sinatraアプリケーションは直接実行できます。
|
||||||
|
|
||||||
ruby myapp.rb [-h] [-x] [-e ENVIRONMENT] [-p PORT] [-s HANDLER]
|
ruby myapp.rb [-h] [-x] [-e ENVIRONMENT] [-p PORT] [-o HOST] [-s HANDLER]
|
||||||
|
|
||||||
オプション:
|
オプション:
|
||||||
|
|
||||||
-h # ヘルプ
|
-h # ヘルプ
|
||||||
-p # ポート指定(デフォルトは4567)
|
-p # ポート指定(デフォルトは4567)
|
||||||
|
-o # ホスト指定(デフォルトは0.0.0.0)
|
||||||
-e # 環境を指定 (デフォルトはdevelopment)
|
-e # 環境を指定 (デフォルトはdevelopment)
|
||||||
-s # rackserver/handlerを指定 (デフォルトはthin)
|
-s # rackserver/handlerを指定 (デフォルトはthin)
|
||||||
-x # mutex lockを付ける (デフォルトはoff)
|
-x # mutex lockを付ける (デフォルトはoff)
|
||||||
|
|
Loading…
Add table
Reference in a new issue