Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ActionDispatch ってなんだろう?

ActionDispatch ってなんだろう?

広島岡山Ruby交流会 01 での発表資料。 http://hirosimaokayamarb.doorkeeper.jp/events/8993

Railsのソースコードリーディングをしているので、その成果を発表です。
http://blog.eiel.info/blog/2014/03/30/action-dispatch/

Tomohiko Himura

March 29, 2014
Tweet

More Decks by Tomohiko Himura

Other Decks in Programming

Transcript

  1. ActionDispatch
 ͬͯͳΜͩΖ͏ʁ 2014-03-29 ޿ౡɾԬࢁRubyަྲྀձ01

  2. ࣗݾ঺հ wͻΉΒͱ΋ͻ͜!FJFMI w(JU)VCFJFM w3BJMT J04ͳͲ w)JSPTIJNBSC ͍͢͝޿ౡ-5ۦಈͳͲӡӦ

  3. ຖ೔3BJMTͷιʔείʔυΛಡΜͰΔΒ͍͠
 ਫ༵೔Λআ͘

  4. ஫ҙࣄ߲ wຊ಺༰͸ݸਓతʹιʔείʔυΛಡΜͰɺ ࣗ෼ͳΓͷཧղͰ͋Γɺ͢΂ͯਖ਼֬Ͱ͋ Δͱ͸ݶΓ·ͤΜɻ wSBJMTEPDFJFMJOGPͷ੒ՌͷͻͱͭͰ͢ɻ

  5. ΞδΣϯμ wجૅ஌ࣝ w3BJMTΞϓϦέʔγϣϯ w"DUJPO%JTQBUDI w·ͱΊ

  6. جૅ஌ࣝ

  7. "DUJPO%JTQBUDI

  8. Ruby on Rails • Railties • ActionPack • ActionDispatch •

    ActionController • AbstractController • ActiveModel • ActiveRecord • ActionView • ActiveSupport • ActionMailer
  9. 3BJMTͷΞʔΩςΫνϟ

  10. 3VCZPO3BJMT wΦʔϓϯιʔε8FCϑϨʔϜϫʔΫ w΢ΣϒαʔϏε࡞੒ͷͨΊͷϥΠϒϥϦ w.7$ϑϨʔϜϫʔΫ

  11. 8FCαʔϏε

  12. 3BDL w3VCZͷͨΊ΢ΣϒαʔόΠϯλʔϑΣΠε

  13. 3BDL w3BDLΞϓϦέʔγϣϯ w3BDLϛυϧ΢ΣΞ

  14. 3BDLΞϓϦέʔγϣϯ

  15. 3BDLΞϓϦέʔγϣϯ wDBMMϝιουΛ΋ͭΦϒδΣΫτ w )BTIΛ౉͢ͱϦΫΤετ w<'JYOVN )BTI &OVNFSBCMF>͕ฦΔϨεϙϯε status header body

  16. 3BDLϛυϧ΢ΣΞ

  17. 3BDLϛυϧ΢ΣΞ w3BDLΞϓϦέʔγϣϯͷ৚݅Λຬͨ͢ wίϯετϥΫλͰ3BDLΞϓϦέʔγϣ Λ͏͚ͱΔ

  18. ϛυϧ΢ΣΞͷಛ௃ w3BDLΞϓϦέʔγϣϯΛ౉͢͜ͱͰ3BDL ΞϓϦέʔγϣϯͱͯ͠࢖͑Δ w౉ͨ͠3BDLΞϓϦέʔγϣϯͷલͱޙ ʹॲཧΛ௥ՃͰ͖Δ

  19. .7$ w.PEFM "DUJWF3FDPSE"DUJWF.PEFM  w7JFX "DUJPO7JFX  w$POUSPMMFS "DUJPO$POUSPMMFS "DUJPO.BJMFS

    "CTUSBDU$POUSPMMFS
  20. .7$

  21. "DUJPO%JTQBUDI͕͍ͳ͍

  22. 3BJMTΞϓϦέʔγϣϯ

  23. Ͱ͔͍ͬ
 3BDLΞϓϦέʔγϣϯ

  24. None
  25. None
  26. None
  27. େ͖ͳ໾໨͸ ϧʔςΟϯά

  28. "DUJPO%JTQBDI wϧʔςΟϯάͷͨΊͷ%4- wϧʔςΟϯάʹར༻͢Δ৘ใΛͭ͘Δ 3BDLϛυϧ΢ΣΞ wϧʔςΟϯάʹར༻͢ΔͨΊͷ w3FRVFTUΦϒδΣΫτ w3FTQPOTFΦϒδΣΫτ

  29. ࣮ࡍʹݟͯΈΔ $ rails -v Rails 4.1.0.rc2 $ rails new hoge

    $ cd hoge # gem ‘pry-rails’ ͯ͠Δ͜ͱʹ͠ͱ͖·͢ $ rails c
  30. 3BJMT"QQMJDBUJPO > Rails.application.class => Hoge::Application > Rails.application.class.ancestors => [Hoge::Application, Rails::Application,

    Rails::Engine, Rails::Railtie, Rails::Initializable, Object, ActiveSupport::Dependencies::Loadable, PP::ObjectMixin, JSON::Ext::Generator::GeneratorMethods::Object, Kernel, BasicObject]
  31. 3BDLΞϓϦέʔγϣϯ > show-method Rails::Application#call def call(env) env["ORIGINAL_FULLPATH"] = build_original_fullpath(env) env["ORIGINAL_SCRIPT_NAME"]

    = env["SCRIPT_NAME"] super(env) end > show-method Rails::Engine#call def call(env) env.merge!(env_config) if env['SCRIPT_NAME'] env.merge! "ROUTES_#{routes.object_id}_SCRIPT_NAME" => env['SCRIPT_NAME' end app.call(env) end
  32. 3BJMTBQQMJDBUJPOBQQ > Rails.application.app @app= #<ActionDispatch::Static:0x007fe97db880d0 @app= #<Rack::Lock:0x007fe97db88148 @app= #<ActiveSupport::Cache::Strategy::LocalCache::Midd 0x007fe97e35cc70

    @app= #<Rack::Runtime:0x007fe97db88198 @app= #<Rack::MethodOverride:0x007fe97db881c0
  33. ϛυϧ΢ΣΞͷ૚

  34. > Rails.application.middleware => #<ActionDispatch::MiddlewareStack:0x007fe97bcbaf68 @middlewares= [Rack::Sendfile, ActionDispatch::Static, Rack::Lock, #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x007fe97e35cc70>, Rack::Runtime,

    Rack::MethodOverride, ActionDispatch::RequestId, Rails::Rack::Logger, ActionDispatch::ShowExceptions, ActionDispatch::DebugExceptions, ActionDispatch::RemoteIp, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::Migration::CheckPending, ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, ActionDispatch::ParamsParser, Rack::Head, Rack::ConditionalGet, Rack::ETag]>
  35. ίΞͷ3BDLΞϓϦέʔγϣϯ

  36. > Rails.application.endpoint => #<ActionDispatch::Routing::RouteSet:0x007fe97dad1830> ! 
 > Rails.application.routes => #<ActionDispatch::Routing::RouteSet:0x007fe97dad1830>

  37. 3PVUF4FUʹ͍ͭͯ͸·ͨޙͰ

  38. "DUJPO%JTQBUDI

  39. "DUJPO%JTQBDIͷߏ੒ཁૉ w)551 w3BDL3FRVFTUͱ͔3BDL3FTQPOTFͷ֦ு wKPVSOFZ w3PVUJOHͷίΞɻ3BJMTΑΓ wNJEEMFXBSF w3BDLϛυϧ΢ΣΞ܈ w3FRVFTU w)551ͱͳͥΘ͔ΕͯΜͩΖ͏ʜ w3PVUJOH

    w5FTUJOH wςετ࣌ʹศརͳBTTFBSUJPO͕࣮૷͞ΕͯΔ
  40. "DUJPO%JTQBUDI)551 w"DUJPO%JTQBUDI)551'JMUFS1BSBNFUFST wϩάͰύεϫʔυӅ͢΍ͭ w"DUJPO%JTQBUDI)551)FBEFST w$(*@7"3*#"-&4ʹ)551@Λ͚ͭͭͭFOWʹΞΫηε w"DUJPO%JTQBUDI)5511BSBNFUFST wQBSBNTΛఏڙͨ͠Γ w"DUJPO%JTQBUDI)55163- wVSM@GPSΛఏڙͨ͠Γ

  41. "DUJPO%JTQBUDI+PVSOFZ wDPOpHSPVUFTSC wbIPHFIPHF`Έ͍ͨͳͷΛύʔεͨ͠Γ w/'"ͱ͔%'"ͱ͔ͭͬͯ͘ϧʔςΟϯά ͷܾఆΞϧΰϦζϜͱ͔ w͝ΊΜɺઆ໌Ͱ͖Δ΄ͲΘ͔ΒΜ

  42. "DUJPO%JTQBUDI3PVUJOH w"DUJPO%JTQBUDI3PVUJOH3PVUFT*OTQFDUPS wSBLFSPVUFTͷग़ྗͭͬͨ͘Γ wϚον͢Δϧʔτ͕ͳ͍ͱ͖ͷը໘ͭͬͨ͘Γ w"DUJPO%JTQBUDI3PVUJOH.BQQFS wDPOpHSPVUFTSCͰ࢖͏໋ྩ w3PVUF4FUBEE@SPVUFΛ࠷ऴతʹݺͿ

  43. "DUJPO%JTQBUDI3PVUJOH w"DUJPO%JTQBUDI3PVUJOH1PMZNPSQIJD3PVUFT wVSM@GPSʹΦϒδΣΫτ౉ͨ͠ͱ͖ͷॲཧ w"DUJPO%JTQBUDI3PVUJOH3FEJSFDUJPO wDPOpHSPVUFTSCͰ࢖͑ΔϦμΠϨΫτ͢ΔSBDL ΞϓϦέʔγϣϯ

  44. "DUJPO%JTQBUDI3PVUJOH w"DUJPO%JTQBUDI3PVUJOH3PVUF4FU wίΞ wϧʔςΟϯά͕Ϛον͢ΔͱίϯτϩʔϥʹॲཧΛ ౉͢໾໨ wDPOpHSPVUFTSCͷESBXϝιουΛఏڙ wDPOpHSPVUFTSCʹΑͬͯߏஙͨ͠IFMQFSूΊͨ ϞδϡʔϧΛ༻ҙͨ͠Γ

  45. "DUJPOEJTQBUDI3PVUJOH w"DUJPO%JTQBUDI3PVUJOH6SM'PS wVSM@GPSͷৼΓ෼͚ wύϥϝʔλͰQPMZNPQJD@VSM΁ճͨ͠Γ

  46. ͔ͤͬ͘ͳͷͰ "DUJPO%JTQBUDI ͚ͩͰ΢ΣϒΞϓϦ

  47. require 'action_dispatch'! ! header = {'Content-Type' => 'text/html'}! ! routes

    = ActionDispatch::Routing::RouteSet.new! routes.draw do! get 'hello', to: -> env { [200, header, ['hello']] }! get 'goodbye', to: -> env { [200, header, ['goobye']] }! root to: -> env { [200, header, ['Hiroshima Okayama .rb']]}! end! ! Rack::Handler.default.run routes, Port: 3000
  48. ·ͱΊ w"UJPO%JTQBUDI͸$POUSPMMFS΁ڮ౉͠Λ͢Δ wϧʔςΟϯάΛఏڙ͢Δ wؔ࿈ϑΝΠϧDPOpHSPVUFTSC w"DUJPO%JTQBUDI3PVUJOH3PVUF4FU΁ͷૢ࡞ w"DUJPO%JTQBUDI3PVUJOH.BQQFS໋͕ྩΛఆٛ wϧʔςΟϯάͷ࢓૊ΈΛࢧ͑Δͷ͸+PVSOFZ