Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Sinatra slideshare

822 views

Published on

とある組織の勉強会議 #5

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Sinatra slideshare

  1. 1. SInatra?
  2. 2. SInatra?
  3. 3. Sinatra?• Ruby製     Webフレームワーク• MCモデル• お手軽
  4. 4. Agenda• Hello Sinatra• View (Haml)• DB(Active Record)
  5. 5. Hello World!# coding: utf-8require ‘sinatra’get ‘/’ do‘Hello Sinatra!’endsinatra.rbhttp://0.0.0.0:4567/COMMANDruby sinatra.rb
  6. 6. 環境作り gem install sinatra
  7. 7. VIEW / Template• Erb• Haml• Markdown• 鋸など
  8. 8. haml• HTML abstraction markup language• Haml accelerates and simplifiestemplate creation down to veritablehaiku
  9. 9. HAML%html%head%titleHaml Sample%bodyHello Haml!<html><head><title>Haml Sample</title></head><body>Hello Haml!</body></html>
  10. 10. Sinatra with hamlhttp://twitter.github.io/bootstrap/https://github.com/kaakaa/SinatraBenkyou1
  11. 11. #coding: utf-8require sinatrarequire hamlget / doredirect /Sinatra/endget /:name/ do@display_name = params[:name]haml :indexendpost /hello do@name = params[:name]if @name.nil? then@name = "Sinatra"endredirect / + @name + /endsinatra.rb
  12. 12. %html%head%titleHaml Sample%meta{ :charset => "utf-8" }%script{ :src => "/js/jquery-1.9.0.min.js" }%link{ :rel => "stylesheet", :href => "/css/bootstrap.css" }%body%div.input-append%h3INPUT YOUT NAME!%form{ :method => "post", :action => "/hello" }%input{ :type => "text", :name => "name",:class => "span2" }%input{ :type => "submit", :value => "SEND",:class => "btn btn-small" }%h2= "Hello " + @display_name.to_s + " !"index.haml
  13. 13. %html%head%titleHaml Sample%meta{ :charset => "utf-8" }%script{ :src => "/js/jquery-1.9.0.min.js" }%link{ :rel => "stylesheet", :href => "/css/bootstrap.css" }%body%div.input-append%h3INPUT YOUT NAME!%form{ :method => "post", :action => "/hello" }%input{ :type => "text", :name => "name",:class => "span2" }%input{ :type => "submit", :value => "SEND",:class => "btn btn-small" }%h2= "Hello " + @display_name.to_s + " !"index.haml
  14. 14. %form{ :method => "post", :action => "/hello" }%input{ :type => "text", :name => "name",:class => "span2" }%input{ :type => "submit", :value => "SEND",:class => "btn btn-small" }post /hello do@name = params[:name]if @name.nil? then@name = "Sinatra"endredirect / + @name + /end
  15. 15. Active Record Design PatternO/R Mapper1Record = 1Objecthttps://github.com/kaakaa/Sinatra-Benkyou2
  16. 16. history Recordnamedate_time
  17. 17. require active_recordActiveRecord::Base.configurations = YAML.load_file(database.yml)ActiveRecord::Base.establish_connection(development)class History < ActiveRecord::Baseenddevelopment:adapter: sqlite3database: db/benkyo.dbsinatra.rb(partially)database.yml
  18. 18. require active_recordActiveRecord::Base.configurations = YAML.load_file(database.yml)ActiveRecord::Base.establish_connection(development)class History < ActiveRecord::Baseenddevelopment:adapter: sqlite3database: db/benkyo.dbsinatra.rb(partially)database.ymlhistorynamedate_time
  19. 19. require active_recordActiveRecord::Base.configurations = YAML.load_file(database.yml)ActiveRecord::Base.establish_connection(development)class History < ActiveRecord::Baseenddevelopment:adapter: sqlite3database: db/benkyo.dbhistory = History.newhistory.name = @namehistory.date_time = @date_timehistory.savehistory = History.find(‘2013/05/14 00:00:00’)history.destroyHistory.destroy_allsinatra.rb(partially)
  20. 20. get / doredirect /Sinatra/endget /:name/ do@display_name = params[:name]@histories = History.allhaml :indexend%h3HISTORY-@histories.each do |history|%div= history.date_time= history.namesinatra.rb(partially) index.haml(partially)
  21. 21. post /*/hello do@name = params[:name]if @name.nil? then@name = "Sinatra"endday = Time.nowdate_time =day.strftime("%Y/%m/%d %H:%M:%S")history = History.newhistory.name = @namehistory.date_time = @date_timehistory.saveredirect / + @name + /sinatra.rb(partially)
  22. 22. summary• Hello Sinatra• 簡単• View (Haml)• 楽• DB(Active Record)• 楽
  23. 23. summary怠惰
  24. 24. bundlesource "https://rubygems.org"ruby 2.0.0group :development, :test dogem "sqlite3", 1.3.7gem "dm-sqlite-adapter"endgem "activerecord"gem "sinatra",git: https://github.com/juanpastas/sinatra.gitgem "haml"bundle install --path vendor/bundlebundle exec ruby sinatra.rbGemfile
  25. 25. 感想
  26. 26. 感想• Ruby周りは楽• ただ、楽過ぎて怖い• テスト環境ならOK?• 本棚アプリ…

×