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

Sinatra Pattern 20130415

on

  • 292 views

 

Statistics

Views

Total Views
292
Views on SlideShare
286
Embed Views
6

Actions

Likes
2
Downloads
0
Comments
0

1 Embed 6

http://www.slideee.com 6

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 Sinatra Pattern 20130415 Presentation Transcript

  • ちっちゃくはじめて おっきく育てる sinataraアプリの作り方 SEO Naotoshi (@sonots)
  • 自己紹介 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
  • 最近やってること Haikanko ※ このロゴはフィクションであり(ry View slide
  • 誰かが言ってた sinatraってアプリが小さ い時はいいけど、大きく なるとお辛いんでしょう? View slide
  • そんなこと ないよ
  • ちっちゃくはじめておっきく 育てる 俺の最強 Sinatra パターン をお伝えします
  • ところで ruby 2.0 で何の問題もな く動いているので、ruby 2.0関連の話は何もなし...
  • さっそく コード中心で><
  • 基礎 ∼ クラシックスタイル 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
  • モジュラースタイル 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
  • Controller 足したい
  • 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
  • app.get とかキモい
  • 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
  • まぁまぁ
  • 本命: 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
  • View 足したい
  • 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
  • Model 足したい
  • 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/ ディレクトリ
  • MVC った ^^
  • けど
  • Worker 欲しくなった
  • 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 起動
  • まとめ • 最初はちっちゃくリーンスタートアップ • おっきくなっても破綻しない!(Rails っぽくし ていけば) • いつ Sinatra やるの?今でしょ! • コード https://github.com/sonots/sinatra-practice