Sinatra slideshare

  • 379 views
Uploaded on

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

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

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
379
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
3
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. SInatra?
  • 2. SInatra?
  • 3. Sinatra?• Ruby製     Webフレームワーク• MCモデル• お手軽
  • 4. Agenda• Hello Sinatra• View (Haml)• DB(Active Record)
  • 5. Hello World!# coding: utf-8require ‘sinatra’get ‘/’ do‘Hello Sinatra!’endsinatra.rbhttp://0.0.0.0:4567/COMMANDruby sinatra.rb
  • 6. 環境作り gem install sinatra
  • 7. VIEW / Template• Erb• Haml• Markdown• 鋸など
  • 8. haml• HTML abstraction markup language• Haml accelerates and simplifiestemplate creation down to veritablehaiku
  • 9. HAML%html%head%titleHaml Sample%bodyHello Haml!<html><head><title>Haml Sample</title></head><body>Hello Haml!</body></html>
  • 10. Sinatra with hamlhttp://twitter.github.io/bootstrap/https://github.com/kaakaa/SinatraBenkyou1
  • 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. %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. %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. %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. Active Record Design PatternO/R Mapper1Record = 1Objecthttps://github.com/kaakaa/Sinatra-Benkyou2
  • 16. history Recordnamedate_time
  • 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. 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. 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. 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. 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. summary• Hello Sinatra• 簡単• View (Haml)• 楽• DB(Active Record)• 楽
  • 23. summary怠惰
  • 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. 感想
  • 26. 感想• Ruby周りは楽• ただ、楽過ぎて怖い• テスト環境ならOK?• 本棚アプリ…