初めてのPadrino

2c246b10eca87568dda9f431c1134cb3?s=47 Takeshi Yabe
December 11, 2014

 初めてのPadrino

日本OSS推進フォーラム主催 第4回アプリケーション部会勉強会 での発表資料

2c246b10eca87568dda9f431c1134cb3?s=128

Takeshi Yabe

December 11, 2014
Tweet

Transcript

  1. 4.
  2. 5.
  3. 6.
  4. 12.
  5. 14.

    ΫϥγοΫελΠϧ 1 require 'sinatra' 2 3 get '/' do 4

    "Hello, sinatra!" 5 end ϞδϡϥʔελΠϧ 1 require 'sinatra/base' 2 3 class App < Sinatra::Base 4 get '/' do 5 "Hello, world!" 6 end 7 end ୯ମͰ࢖͏γϯϓϧͳΞϓϦͳΒΫϥγοΫελΠϧ ଞͷ3BDLΞϓϦͱҰॹʹ࢖͏ͳΒϞδϡϥʔελΠϧ 4JOBUSB
  6. 17.

    NZQSPKFDU ᵓᴷᴷ(FNpMF ᵓᴷᴷ(FNpMFMPDL ᵓᴷᴷ3BLFpMF ᵓᴷᴷBQQSC ᵓᴷᴷDPOpH ᴹᵋᴷᴷEBUBCBTFSC ᵓᴷᴷDPOpHSV ᵓᴷᴷEC ᴹᵋᴷᴷEFWFMPQNFOUEC

    ᵓᴷᴷMPH ᴹᵋᴷᴷEFWFMPQNFOUMPH ᵓᴷᴷNBJMFSSC ᵓᴷᴷQVCMJD ᴹᵓᴷᴷKBWBTDSJQUT ᴹᴹᵋᴷᴷBQQKT ᴹᵋᴷᴷTUZMFTIFFUT ᴹᵋᴷᴷBQQDTT ᵋᴷᴷWJFXT ᵓᴷᴷJOEFYTMJN ᵋᴷᴷMBZPVUTMJN  σΟϨΫτϦߏ଄  %#ͷઃఆ  ϩάͷઃఆ  ϔϧύʔϝιου  )5.-Τεέʔϓ  ηογϣϯ  ςετ؀ڥ  ίϯιʔϧͰͷಈ࡞֬ೝ  3BLFλεΫ  ͳͲͳͲ ָ͍ͨ͠ 4JOBUSB
  7. 18.
  8. 19.

    1BESJOPͷྺ࢙ TJOBUSB@NPSFͷ։ൃ͕ελʔτ QBESJOPͷ։ൃ͕ελʔτ   TJOBUSB@NPSF͕QBESJOP΁ͷҠߦΛද໌ TJOBUSB@NPSF࠷ޙͷίϛοτ ʜ ೔ຊਓ͕։ൃνʔϜೖΓ !OBNVTZBLB

     ϦϦʔε 3VCZͷαϙʔτഇࢭͳͲେ͖Ίͷมߋ ϦϦʔε EZOBNPJEͷαϙʔτɺDBDIF@PCKFDUIFMQFSͷ௥Ճ ϦϦʔε 1BSBNT1SPUFDUJPOͷ௥Ճ ϦϦʔε SPVUFS SFMPBEFS HFOFSBUPSͷվળ ϦϦʔε NPVOUFS͕վળ͞Εɺଞͷ3BDLΞϓϦͷϚ΢ϯτ͕Մೳʹͳͬͨ OFTRVFOB͕ݸਓͰ։ൃ OFTRVFOBͱɺಉ྅ͩͬͨ %"EE:& BDIJVͰ։ൃ ৽͍͠ϧʔλʔ QFOESBHPO Λ։ൃ
  9. 21.

    1BESJOPͷओͳػೳ  δΣωϨʔλؚΉίϚϯυϥΠϯπʔϧ  αϒΞϓϦέʔγϣϯ  ଞͷ3BDLΞϓϦέʔγϣϯͷϚ΢ϯτ  ϧʔςΟϯάͷ֦ு 

    .7$ύλʔϯͷಋೖ  ๛෋ͳϔϧύʔϝιου  ؆୯ͳϝʔϧૹ৴ػೳ  Ωϟογϡػೳ  ࠃࡍԽػೳ  ଞͷϥΠϒϥϦͱ࿈ܞՄೳͳϩΨʔ  ։ൃ࣌ͷϦϩʔμʔ
  10. 24.

    QBESJOPDPSF 4JOBUSBΛ֦ுͨ͠ϕʔεΞϓϦέʔγϣϯ 1BESJOP'SBNFXPSLͷػೳΛશͯ࢖͏͜ͱ͕Ͱ͖Δ # lib/padrino-core/application.rb 1 module Padrino 2 class

    Application < Sinatra::Base 3 register Padrino::ApplicationSetup 4 register Padrino::Routing 5 register Padrino::ParamsProtection SFHJTUFSϝιουʹΑΔ4JOBUSB֦ுͷొ࿥ VTFϝιουʹΑΔ3BDLϛυϧ΢ΣΞͷొ࿥
  11. 27.

    QBESJOPDPSF 1 SampleProject::App.controllers :posts do 2 get :index do 3

    # url(:posts, :index) => "/posts" 4 render 'posts/index' 5 end 6 7 get :show, map: '/posts/:id' do 8 # url(:posts, :show, id: 5) => "/posts/5" 9 render 'posts/new' 10 end 11 end ϧʔςΟϯάͷ֦ுʢҰྫʣ ໊લ෇͖ϧʔτɺςϯϓϨʔτΤϯδϯͷࣗಈݕग़
  12. 30.

    - project - app - mailer - controller - helper

    - model - migration - plugin - component - task - admin - admin_page QBESJOPHFO ৽͍͠1BESJOPϓϩδΣΫτͷ࡞੒࣌΍ طଘͷ1BESJOPϓϩδΣΫτ΁ ؆୯ʹίʔυΛ௥Ճ͢Δ͜ͱ͕Ͱ͖ΔΑ͏ʹͳΔ
  13. 32.

    QBESJOPHFO ϓϩδΣΫτੜ੒ Usage: padrino-gen project [name] [options] Options: -n, [--app=APP]

    -b, [--bundle], [--no-bundle] -r, [--root=ROOT] [--dev], [--no-dev] -i, [--tiny], [--no-tiny] -l, [--lean], [--no-lean] -a, [--adapter=ADAPTER] -p, [--template=TEMPLATE] -g, [--gem], [--no-gem] [--migration-format=MIGRATION_FORMAT] -d, [--orm=ORM] -t, [--test=TEST] -m, [--mock=MOCK] -s, [--script=SCRIPT] -e, [--renderer=RENDERER] -c, [--stylesheet=STYLESHEET] QBESJOPϓϩδΣΫτͰ࢖͏৔߹͸ QBESJOPHQSPKFDU
  14. 34.

    QBESJOPHFO ϓϩδΣΫτੜ੒ QBESJOPHQSPKFDUTBNQMF@QSPKFDU TBNQMF@QSPKFDU ᵓᴷᴷ(FNpMF ᵓᴷᴷ3BLFpMF ᵓᴷᴷBQQ ᴹᵓᴷᴷBQQSC ᴹᵓᴷᴷDPOUSPMMFST ᴹᵓᴷᴷIFMQFST

    ᴹᵋᴷᴷWJFXT ᴹᵋᴷᴷMBZPVUT ᵓᴷᴷCJO ᴹᵋᴷᴷTBNQMF@QSPKFDU ᵓᴷᴷDPOpH ᴹᵓᴷᴷBQQTSC ᴹᵋᴷᴷCPPUSC ᵓᴷᴷDPOpHSV ᵓᴷᴷMPH ᵓᴷᴷQVCMJD ᴹᵓᴷᴷGBWJDPOJDP ᴹᵓᴷᴷJNBHFT ᴹᵓᴷᴷKBWBTDSJQUT ᴹᵋᴷᴷTUZMFTIFFUT ᵋᴷᴷUNQ EJSFDUPSJFT pMFT QBESJOPHQSPKFDUUJOZ@QSPKFDUŠUJOZ UJOZ@QSPKFDU ᵓᴷᴷ(FNpMF ᵓᴷᴷ3BLFpMF ᵓᴷᴷBQQ ᴹᵓᴷᴷBQQSC ᴹᵓᴷᴷDPOUSPMMFSTSC ᴹᵓᴷᴷIFMQFSTSC ᴹᵋᴷᴷNBJMFSTSC ᵓᴷᴷCJO ᴹᵋᴷᴷUJOZ@QSPKFDU ᵓᴷᴷDPOpH ᴹᵓᴷᴷBQQTSC ᴹᵋᴷᴷCPPUSC ᵓᴷᴷDPOpHSV ᵓᴷᴷMPH ᵓᴷᴷQVCMJD ᴹᵓᴷᴷGBWJDPOJDP ᴹᵓᴷᴷJNBHFT ᴹᵓᴷᴷKBWBTDSJQUT ᴹᵋᴷᴷTUZMFTIFFUT ᵋᴷᴷUNQ EJSFDUPSJFT pMFT
  15. 35.

    QBESJOPHFO ίϯϙʔωϯτͷ௥Ճ Usage: padrino-gen component [options] Options: -n, [--app=APP] -r,

    [--root=ROOT] -a, [--adapter=ADAPTER] -d, [--orm=ORM] -t, [--test=TEST] -m, [--mock=MOCK] -s, [--script=SCRIPT] -e, [--renderer=RENDERER] -c, [--stylesheet=STYLESHEET] Description: padrino-gen component add components into a Padrino project Current Selected Components: orm none test none mock none script none renderer slim stylesheet none ඞཁʹͳͬͨͱ͖ʹ ඞཁͳ΋ͷΛ௥ՃͰ͖Δ
  16. 36.

    QBESJOPHFO αϒΞϓϦέʔγϣϯ αϒΞϓϦέʔγϣϯ͸ Ϟσϧ΍ϥΠϒϥϦΛڞ௨Ͱ࢖͑Δ QBESJOPHBENJO QBESJOPHBQQBQJ TBNQMF@QSPKFDU ᵓᴷᴷBQQ ᴹᵓᴷᴷBQQSC ᴹᵓᴷᴷDPOUSPMMFST

    ᴹᵓᴷᴷIFMQFST ᴹᵋᴷᴷWJFXT ᴹᵋᴷᴷMBZPVUT ᵓᴷᴷBENJO ᴹᵓᴷᴷBQQSC ᴹᵓᴷᴷDPOUSPMMFST ᴹᵓᴷᴷIFMQFST ᴹᵋᴷᴷWJFXT ᴹᵋᴷᴷMBZPVUT ᵓᴷᴷBQJ ᴹᵓᴷᴷBQQSC ᴹᵓᴷᴷDPOUSPMMFST ᴹᵓᴷᴷIFMQFST ᴹᵋᴷᴷWJFXT ᴹᵋᴷᴷMBZPVUT ᵋᴷᴷQVCMJD ᵓᴷᴷBENJO ᴹᵓᴷᴷJNBHFT ᴹᵓᴷᴷKBWBTDSJQUT ᴹᵋᴷᴷTUZMFTIFFUT ᵓᴷᴷGBWJDPOJDP ᵓᴷᴷJNBHFT ᵓᴷᴷKBWBTDSJQUT ᵋᴷᴷTUZMFTIFFUT # config/apps.rb 1 Padrino.mount('SampleProject::Admin') 2 .to('/admin') 3 Padrino.mount('SampleProject::Api') 4 .to('/') 5 .host("api.example.org") 6 Padrino.mount('SampleProject::App') 7 .to(‘/') Ϛ΢ϯτͷઃఆͰɺҰகͤ͞Δύε΍ϗετ໊ΛࢦఆͰ͖Δ
  17. 39.

    QBESJOPBENJO # config/apps.rb 1 Padrino.mount('SampleProject::Admin').to('/admin') 2 Padrino.mount('SampleProject::App').to('/') BENJO ᵓᴷᴷBQQSC ᵓᴷᴷDPOUSPMMFST

    ᴹᵓᴷᴷBDDPVOUTSC ᴹᵓᴷᴷCBTFSC ᴹᵋᴷᴷTFTTJPOTSC ᵋᴷᴷWJFXT ᵓᴷᴷBDDPVOUT ᴹᵓᴷᴷ@GPSNTMJN ᴹᵓᴷᴷFEJUTMJN ᴹᵓᴷᴷJOEFYTMJN ᴹᵋᴷᴷOFXTMJN ᵓᴷᴷCBTF ᴹᵋᴷᴷJOEFYTMJN ᵓᴷᴷFSSPST ᴹᵓᴷᴷTMJN ᴹᵓᴷᴷTMJN ᴹᵋᴷᴷTMJN ᵓᴷᴷMBZPVUT ᴹᵓᴷᴷBQQMJDBUJPOTMJN ᴹᵋᴷᴷFSSPSTMJN ᵋᴷᴷTFTTJPOT ᵋᴷᴷOFXTMJN EJSFDUPSJFT pMFT ؅ཧΞϓϦ͸αϒΞϓϦέʔγϣϯͱͯ͠ Ϛ΢ϯτ͢Δ
  18. 42.

    QBESJOPBENJO ؅ཧΞϓϦ༻ͷϔϧύʔϝιου͕͍͔ͭ͘௥Ճ͞ΕΔ 1 module SampleProject 2 class Admin < Padrino::Application

    3 register Padrino::Mailer 4 register Padrino::Helpers 5 register Padrino::Admin::AccessControl 6 7 set :admin_model, 'Account' 8 set :login_page, '/sessions/new' 9 10 enable :sessions 11 disable :store_location 12 13 access_control.roles_for :any do |role| 14 role.protect '/' 15 role.allow '/sessions' 16 end 17 18 access_control.roles_for :admin do |role| 19 role.project_module :posts, '/posts' 20 role.project_module :accounts, '/accounts' 21 end 22 23 # Custom error management 24 error(403) { @title = "Error 403"; render('errors/403', :layout => :error) } 25 error(404) { @title = "Error 404"; render('errors/404', :layout => :error) } 26 error(500) { @title = "Error 500"; render('errors/500', :layout => :error) } 27 end 28 end
  19. 44.

    QBESJOPIFMQFST ϑΥʔϜ΍ϦϯΫɺը૾༻ͷλάͳͲ )5.-λάΛੜ੒͢Δϝιου͕࢖༻ՄೳʹͳΔ MJOL@UP΍JNBHF@UBHͳͲ3BJMTͱಉ͡Α͏ͳϝιου͕ఆٛ͞Ε͍ͯΔ input_tag :text, class: “demo” #=> <input

    type='text' class='demo' /> ςϯϓϨʔτΛϨϯμϦϯά͢ΔͨΊͷϝιου SFOEFS QBSUJBM ͕࢖༻ՄೳʹͳΔ render ‘path/to/any/template' partial 'photo/_item', object: @photo, locals: { foo: 'bar' }
  20. 46.

    QBESJOPDBDIF ςϯϓϨʔτͷϨϯμϦϯά݁Ռ΍ ೚ҙͷΦϒδΣΫτͷ಺༰ΛΩϟογϡͰ͖ΔΑ͏ʹͳΔ Ωϟογϡػߏͷϕʔεʹ͸.POFUB͕ར༻͞Ε͓ͯΓ σϑΥϧτͰ༷ʑͳΩϟογϡετΞ͕ར༻Մೳͱͳ͍ͬͯΔ 1 Padrino.cache = Padrino::Cache.new(:LRUHash) #

    default choice 2 Padrino.cache = Padrino::Cache.new(:File, dir: Padrino.root('tmp', app_name.to_s, 'cache')) 3 Padrino.cache = Padrino::Cache.new(:Memcached, backend: memcached_or_dalli_instance) 4 Padrino.cache = Padrino::Cache.new(:Redis, backend: redis_instance) 5 Padrino.cache = Padrino::Cache.new(:Mongo, backend: mongo_client_instance) 1 module SampleProject 2 class App < Padrino::Application 3 register Padrino::Cache 4 enable :caching 5 6 get '/foo', cache: true do 7 expires 10 8 Time.now.to_s 9 end 10 end 11 end
  21. 48.

    QBESJOPNBJMFST γϯϓϧͳΠϯλʔϑΣʔεͰϝʔϧ഑৴͕ߦ͑ΔΑ͏ʹͳΔ ϑΝΠϧͷఴ෇΍Ϛϧνύʔτ΋Մೳ .BJMϕʔε 1 class App < Sinatra::Base 2

    register Padrino::Mailer 3 4 set :delivery_method, smtp: { 5 address: ‘smtp.gmail.com’, 6 port: 587, 7 domain: 'your.host.name', 8 user_name: '<username>', 9 password: '<password>', 10 authentication: 'plain', 11 enable_starttls_auto: true 12 } 13 14 post :mail do 15 email do 16 from: "foo@example.com", 17 to: "bar@example.com", 18 subject: "Welcome!", 19 body: "This is test mail." 20 end 21 end 22 23 end 1 module Sample 2 class App < Padrino::Application 3 register Padrino::Mailer 4 5 set :delivery_method, :sendmail 6 7 mailer :sample do 8 email :welcome do 9 from "foo@example.com" 10 to "bar@example.com" 11 subject "Welcome!" 12 add_file "/path/to/someotherfile.png" 13 provides :html, :plain 14 render "sample" 15 end 16 end 17 18 post :mail do 19 deliver :sample, :welcome 20 end 21 22 end 23 end
  22. 51.
  23. 53.
  24. 56.

    1BESJOPͷΤίγεςϜ  3BDLϛυϧ΢ΣΞ  SBDLDPOUSJC  4JOBUSBͷػೳ֦ு 4JOBUSB&YUFOUJPO  

    TJOBUSBDPOUSJC  1BESJOPͷػೳ֦ு 1BESJOP༻ͷ4JOBUSB&YUFOUJPO   QBESJOPDPOUSJC  1BESJOPͷϓϥάΠϯػೳ  QBESJOPSFDJQFTʹެࣜͷϨγϐ͕ެ։͞Ε͍ͯΔ
  25. 57.

    1BESJOPͷΤίγεςϜ  3BDLϛυϧ΢ΣΞ  SBDLDPOUSJC  4JOBUSBͷػೳ֦ு 4JOBUSB&YUFOUJPO  

    TJOBUSBDPOUSJC  1BESJOPͷػೳ֦ு 1BESJOP༻ͷ4JOBUSB&YUFOUJPO   QBESJOPDPOUSJC  1BESJOPͷϓϥάΠϯػೳ  QBESJOPSFDJQFTʹެࣜͷϨγϐ͕ެ։͞Ε͍ͯΔ
  26. 62.

    ͦͷଞ  0NOJ"VUI 8BSEFO  KQNPCJMF #SPXTFS  XJMM@QBHJOBUF ,BNJOBSJ

     #FUUFS&SSPST 4JOBUSBͰ࢖͑Δ΋ͷ͸ී௨ʹ࢖͑Δ  QBESJOPQJQFMJOF QBESPOJBTTFUT 1BESJOPʹಛԽͨ͠΋ͷ΋͋Δ
  27. 78.

    1BESJOPͷࠓޙ 1BESJOP΁޲͚ͯ  ϚϧνεϨουͷαϙʔτ  ৽͍͠ϧʔλʔͷಋೖ  1BESJOP"ENJOͷ࡮৽  ϖʔδωʔγϣϯɺݕࡧɾϑΟϧλɺμΠφϛοΫͳ6*ͳͲ

     ϦϑΝΫλϦϯά  ϦϩʔμʔɺϩΨʔ  υΩϡϝϯτͷ੔උ ґଘΛগͳ͘ɺΑΓݎ࿚ͰΑΓߴ଎ͳϑϨʔϜϫʔΫΛ໨ࢦ͢ 3BDL 4JOBUSBͷಈ޲΋஫໨
  28. 80.
  29. 81.

    1BESJOP΁ͷߩݙ  υΩϡϝϯτ  ίʔυͷதɺެࣜυΩϡϝϯτɺ೔ຊޠ  ϓϥάΠϯ  ಈ͔ͳ͍΋ͷ΁ͷύονɺࣗ෼ͷཉ͍͠΋ͷ 

    HFN  3BDLϛυϧ΢ΣΞɺ4JOBUSB&YUFOUJPOɺ1BESJOPઐ༻  ίʔυ  ίϝϯτमਖ਼ɺόάमਖ਼ɺ৽ػೳͷఏҊ
  30. 82.
  31. 83.
  32. 85.
  33. 86.
  34. 87.