Sinatra Pattern 20130415
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Sinatra Pattern 20130415

on

  • 2,114 views

 

Statistics

Views

Total Views
2,114
Views on SlideShare
1,994
Embed Views
120

Actions

Likes
15
Downloads
13
Comments
0

6 Embeds 120

http://studio3104.hatenablog.com 89
https://twitter.com 17
http://www.slideee.com 6
http://feedly.com 5
https://kcw.kddi.ne.jp 2
http://digg.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Sinatra Pattern 20130415 Presentation Transcript

  • 1. ちっちゃくはじめて おっきく育てる sinataraアプリの作り方 SEO Naotoshi (@sonots)
  • 2. 自己紹介 describe Sonots do its(:company) { should == :DeNA } it_should_behave_like “DeNA employee” end shared_examples_for “DeNA employee” do it { should write(:perl) } #=> fail end
  • 3. 最近やってること Haikanko ※ このロゴはフィクションであり(ry
  • 4. 誰かが言ってた sinatraってアプリが小さ い時はいいけど、大きく なるとお辛いんでしょう?
  • 5. そんなこと ないよ
  • 6. ちっちゃくはじめておっきく 育てる 俺の最強 Sinatra パターン をお伝えします
  • 7. ところで ruby 2.0 で何の問題もな く動いているので、ruby 2.0関連の話は何もなし...
  • 8. さっそく コード中心で><
  • 9. 基礎 ∼ クラシックスタイル require 'sinatra'   get '/hello' do 'Hello World!' end   get '/hello/:name' do "Hello #{params[:name]}!" end $ ruby my_app.rb Gemfile Gemfile.lock my_app.rb 起動 ディレクトリ構造 my_app.rb
  • 10. モジュラースタイル require 'sinatra/base'  class MyApp < Siantra::Base get '/hello' do 'Hello World!' end   get '/hello/:name' do "Hello #{params[:name]}!" end end $ rackup Gemfile Gemfile.lock config.ru my_app.rb 起動 ディレクトリ構造 # config.ru require './my_app' run MyApp
  • 11. Controller 足したい
  • 12. 1. Sinatra.register を使う # my_app.rb require 'sinatra/base' require_relative 'posts_controller' class MyApp < Sinatra::Base   register PostsController   get '/hello' do     'Hello World!'   end      get '/hello/:name' do     "Hello #{params[:name]}!"   end end Gemfile Gemfile.lock config.ru my_app.rb posts_controller.rb ディレクトリ # posts_controller.rb module PostsController   def self.registered(app)     app.get '/posts' do     end     app.get '/posts/:id' do       "Post #{params[:id]}"     end   end end
  • 13. app.get とかキモい
  • 14. 2. use (Rack Middleware) # my_app.rb class MyApp < Sinatra::Base   get '/hello' do     'Hello World!'   end      get '/hello/:name' do     "Hello #{params[:name]}!"   end end Gemfile Gemfile.lock config.ru my_app.rb posts_controller.rb ディレクトリ # posts_controller.rb class PostsController < Sinatra::Base   get '/posts' do   end   get '/posts/:id' do     "Post #{params[:id]}"   end end # config.ru require 'sinatra/base' require './my_app' require './posts_controller' use PostsController run MyApp
  • 15. まぁまぁ
  • 16. 本命: URLMap を使う class HelloController < Sinatra::Base   get '/hello' do     'Hello World!'   end      get '/hello/:name' do     "Hello #{params[:name]}!"   end end Gemfile Gemfile.lock config.ru hello_controller.rb posts_controller.rb ディレクトリ class PostsController < Sinatra::Base   get '/' do   end   get '/:id' do     "Post #{params[:id]}"   end end require 'sinatra/base' require './hello_controller' require './posts_controller' ROUTES = {   '/' => HelloController,   '/posts' => PostsController, } run Rack::URLMap.new(ROUTES) config.ru
  • 17. View 足したい
  • 18. View ∼ Sinatraにあるよ class HelloController < Sinatra::Base   set :views, File.expand_path('../views', File.dirname(__FILE__))      get '/hello' do     'Hello World!'   end   get '/hello/:name' do     @name = params[:name]     erb :hello   end end Gemfile Gemfile.lock config.ru controllers/ views/ ディレクトリ <html>   <b>Hello <%= @name %>!</b> </html> views/hello.erb
  • 19. Model 足したい
  • 20. Model ∼ ActiveRecordとか source 'https://rubygems.org' gem 'sinatra', require: 'sinatra/base' gem 'activesupport' gem 'activerecord' gem 'sinatra-activerecord',:require => 'sinatra/activerecord' gem 'sqlite3' gem 'rake' require 'active_record' ActiveRecord::Base.establish_connection(   :adapter => 'sqlite3',   :host => 'localhost',   :username => 'USERNAME', #or root   :password => 'PASSWORD',   :database => 'db/YOURDATABASE' ) class Post < ActiveRecord::Base end models/post.rb Gemfile Gemfile Gemfile.lock config.ru controllers/ views/ models/ ディレクトリ
  • 21. MVC った ^^
  • 22. けど
  • 23. Worker 欲しくなった
  • 24. Worker + gem 'foreman’ ディレクトリ web: bundle exec rackup web/config.ru worker: bundle exec ruby worker/notification.rb worker/notification.rb Gemfile Gemfile Gemfile.lock config.ru web/ controllers/ views/ models/ worker/ require_relative '../models/post.rb' class Notification   def run     while true       sleep 1       puts "hey! #{Post.all.first}"     end   end end Notification.new.run Procfile $ foreman start 起動
  • 25. まとめ • 最初はちっちゃくリーンスタートアップ • おっきくなっても破綻しない!(Rails っぽくし ていけば) • いつ Sinatra やるの?今でしょ! • コード https://github.com/sonots/sinatra-practice