Ruby off Rails (japanese)

  • View
    7.566

  • Download
    7

Embed Size (px)

DESCRIPTION

Web applications development with Ruby (but without Rails)

Text of Ruby off Rails (japanese)

  • 1. Ruby off Rails by Stoyan Zhekov Kyoto, 17-May-2008

2. 3. Coupling and Cohesion ORM - Sequel () Ruby Enter Rack - The Middle Man Thin, Ramaze, Tamanegi , , 08/05/19 3 4. Coupling and Cohesion (1) Cohesion - ()http://en.wikipedia.org/wiki/Cohesion_(computer_science) Coupling - ()http://en.wikipedia.org/wiki/Coupling_(computer_science) 08/05/194 5. Coupling and Cohesion (2) High cohesion Low (loosely) coupling 08/05/195 6. Coupling and Cohesion (3)Google vs Amazon08/05/19 6 7. Coupling and Cohesion (5)VS 08/05/19 7 8. Rails 08/05/19 8 9. Ruby on Rails Convention over configuration ? DRY ? Generators Plugins Libraries 08/05/19 9 10. Rails?08/05/1910 11. : ORM Request: CGI env, : URL Rendering (views): rendering libraries Sessions: persist objects or data 08/05/1911 12. Database ORM Active Record ?= ActiveRecord ActiveRecord http://datamapper.org/why.html Data Mapper DB Sequel 08/05/1912 13. ORM Sequel (?) Pure Ruby Thread safe Connection pooling DB queriesDSL ObjectDBORM Transactions with rollback08/05/1913 14. ORM Sequel (Core) require 'sequel' DB = Sequel.open 'sqlite:///blog.db' DB = Sequel.open 'postgres://cico:12345@localhost:5432/mydb' DB = Sequel.open(quot;postgres://postgres:postgres@localhost/my_dbquot;,:max_connections => 10, :logger => Logger.new('log/db.log')) 08/05/19 14 15. ORM Sequel (Core, 2) DB 50..100).order(:name, :department) paginated = dataset.paginate(1, 10) # first page, 10 rows per page paginated.page_count #=> number of pages in dataset paginated.current_page #=> 108/05/1915 16. ORM Sequel (Model)class Post < Sequel::Model(:my_posts)set_primary_key [:category, :title]belongs_to :author has_many :comments has_and_belongs_to_many :tagsafter_create do set(:created_at => Time.now) end end08/05/19 16 17. ORM Sequel (Model, 2) class Person < Sequel::Modelhas_many :posts, :eager=>[:tags] set_schema doprimary_key :idtext :nametext :emailforeign_key :team_id, :table => :teamsendend08/05/1917 18. Evolution 08/05/19 18 19. Ruby Web EvolutionWebrick Mongrel Event Thin EbbMachine 08/05/1919 20. Thin Ragel and Event Machine Unix sockets - nginx Rack-based Rails, Ramaze etc. Rackup () 08/05/19 20 21. Thin + Nginxthin start --servers 3 --socket /tmp/thinthin start --servers 3 --port 3000# nginx.confupstream backend {fair;server unix:/tmp/thin.0.sock;server unix:/tmp/thin.1.sock;server unix:/tmp/thin.2.sock;}08/05/19 21 22. Merb RamazeSinatra... How to connect them? CGIWebrickMongrelThin 08/05/19 22 23. - RackMerb RamazeSinatra... Rack The Middle ManCGIWebrickMongrelThin 08/05/19 23 24. Rack? 08/05/1924 25. Rack? http://rack.rubyforge.org/By Christian Neukirchen 08/05/1925 26. Rack HTTPRuby API() Request -> Response callenv: a status, i.e. 200 the headers, i.e. {Content-Type => text/html} an object that responds to each: some string 08/05/19 26 27. Rack! class RackAppdef call(env) [ 200, {quot;Content-Typequot; => quot;text/plainquot;}, quot;Hi!quot;]end endapp = RackApp.new 08/05/1927 28. Free Hugshttp://www.freehugscampaign.org/ 08/05/1928 29. Hugs (1) p 'Hug!'08/05/19 29 30. ruby hugs.rb %w(rubygems rack).each { |dep| require dep } class HugsApp def call(env)[ 200, {quot;Content-Typequot; => quot;text/plainquot;}, quot;Hug!quot;] endend Rack::Handler::Mongrel.run(HugsApp.new, :Port => 3000) 08/05/1930 31. Rack::Builderrequire 'hugs'app = Rack::Builder.new { use Rack::Static, :urls => [quot;/cssquot;, quot;/imagesquot;], :root => quot;publicquot; use Rack::CommonLogger use Rack::ShowExceptions map quot;/quot; do run lambda { [200, {quot;Content-Typequot; => quot;text/plainquot;}, [quot;Hi!quot;]] }end map quot;/hugquot; do run HugsApp.newend} Rack::Handler::Thin.run app, :Port => 300008/05/1931 32. Rack Building Blocks request = Rack::Request.new(env) request.get? response = Rack::Response.new('Hi!') response.set_cookie('sess-id', 'abcde') Rack::URLMap Rack::Session Rack::Auth::Basic Rack::File 08/05/19 32 33. Rack Middleware use Rack::... ?class RackMiddlewareExample def initialize app @app = app enddef call env @app.call(env) endend08/05/1933 34. (ORM): Sequel Request: Rack::Request(env) : Rack 'map' and 'run' Rendering/views: Tenjin, Amrita2 Sessions: Rack::Session 08/05/1934 35. Does it scale?08/05/1935 36. rackup hugs.ru require 'hugs'app = Rack::Builder.new {use Rack::Static, :urls => [quot;/cssquot;, quot;/imagesquot;], :root => quot;publicquot; map quot;/quot; do run lambda { [200, {quot;Content-Typequot; => quot;text/plainquot;}, [quot;Hi!quot;]] }end map quot;/hugquot; dorun HugsApp.newend}Rack::Handler::Thin.run app, :Port => 3000 08/05/19 36 37. Hugs rackup -s webrick -p 3000 hugs.ru thin start --servers 3 -p 3000 -R hugs.ru SCGI FastCGI Litespeed ....08/05/19 37 38. Rack-based Invisible thin-based in 35 LOC http://github.com/macournoyer/invisible/ Coset REST on Rack Halcyon JSON Server Framework Merb Sinatra Ramaze Rails !? 08/05/1938 39. RailsRack RailsRack: raw req -> rack env -> Rack::Request ->CGIWrapper ->CgiRequest Ezra Zygmuntowicz (merb)'s repo on github http://github.com/ezmobius/rails raw req -> rack env -> ActionController::RackRequest ./script/rackup -s thin -c 5 -e production 08/05/1939 40. Ramaze : : http://ramaze.net/ : gem install ramaze git clone http://github.com/manveru/ramaze.git : Michael Fellinger and Co. 08/05/1940 41. Ramaze? (low [loosely] coupling) Rack-based (~25) facebook, rapaste - #ramaze All bugs are fixed within 48 hours of reporting.08/05/1941 42. Deployment CGI Ebb FastCGI Evented Mongrel LiteSpeed Swiftiplied Mongrel Mongrel Thin SCGI Rack-based Webrick08/05/19 42 43. Templating engines Amrita2 RedCloth Builder Remarkably Erubis Sass Ezamar Tagz Haml Tenjin Liquid XSLT Markaby08/05/1943 44. Ezamar 08/05/1944 45. ? %w(rubygems ramaze).each {|dep| require dep} class MainController < Ramaze::Controllerdef index; quot;Hiquot; endend class HugsController < Ramaze::Controllermap '/hug'def index; quot;Hug!quot; endend Ramaze.start :adapter => :thin, :port => 3000 08/05/19 45 46. Ramazeclass SmileyController < Ramaze::Controller map '/smile' helper :smiley def indexsmiley(':)')endend 08/05/19 46 47. Ramaze (2)module Ramazemodule Helpermodule SmileyHelper FACES = {':)' => '/images/smile.png',';)' => '/images/twink.png'} REGEXP = Regexp.union(*FACES.keys)def smiley(string) string.gsub(REGEXP) { FACES[$1] } end endendend08/05/1947 48. Stack ORM Sequel Thin + NginX Rack Middleware Ramaze Templates HAML, Tenjin, Amrita2 08/05/19 48 49. Rails SequelORM Rack! DIY framework, DIY server Ramaze! 08/05/1949