Lazy Loading and Object Proxying Shenangians

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    Lazy Loading and Object Proxying Shenangians - Presentation Transcript

    1. Lazy Loading ..and object proxying shenanigans
    2. John Barton www.whoisjohnbarton.com @johnbarton
    3. we’re often* hiring * a month or two ago, right now, a couple of months from now
    4. The Problem:
    5. To handle ever increasing page load through the judicious application of view fragment caching...
    6. ... without resorting to downright ugly code
    7. For Example:
    8. class PostController < ApplicationController def index @posts = Post.all end end
    9. <% @posts.each do |post| %> <%= post.title %> <% end %>
    10. Make it fast:
    11. <% cache 'slow_posts' do %> <% @posts.each do |post| %> <%= post.title %> <% end %> <% end %>
    12. What now?
    13. class PostController < ApplicationController def index @posts = Post.all end end @posts = Post.all is still evaluated ... and it’s expensive
    14. What about? <% cache 'slow_posts' do %> <% Post.all.each do |post| %> <%= post.title %> <% end %> <% end %>
    15. Separation of concerns?
    16. What about? class PostController < ApplicationController def index unless fragment_exist? 'slow_posts' @posts = Post.all end end end
    17. Separation of concerns?
    18. What About? class PostController < ApplicationController def index @posts = lazy_load { Post.all } end end
    19. Still not perfect ... but fuck it ... I’ve got a job to get on with
    20. How?
    21. def lazy_load(&block) LazyLoader.new(&block) end
    22. class LazyLoader instance_methods.each { |m| undef_method m unless m =~ /^__/ } def initialize(&block) @_initializer = block end protected # pass everything to _target def method_missing(method, *args, &block) _target.send method, *args, &block end private # on first call will instantiate itself with _initializer block def _target @_target ||= @_initializer.call end end
    23. Gotchas:
    24. ... or when I realised I wanted to be a Smalltalk programmer
    25. class PostController < ApplicationController def show @post = Post.find(params[:id]) end end <% if @post %> <%= @post.title %> <% end %>
    26. class PostController < ApplicationController def show @post = lazy_load { Post.find(params[:id]) } end end <% cache 'slow_post' do %> <% if @post %> <%= @post.title %> <% end %> <% end %>
    27. NoMethodError: undefined method `title' for nil:NilClass
    28. wtf?
    29. Ruby’s boolean operators don’t ask the objects for their truthyness Word on the street is Smalltalk has that covered* * I don’t know Smalltalk so hopefully I’m not full of shit right here
    30. a = lazy_load { nil } # this is fine nil.nil? # => true a.nil? # => true # this is broken but..... # i can fix it by opening up NilClass a == nil # => true nil == a # => false # but what do i do about these? !!nil # => false !!a # => true if a puts "blah!" end # => "blah!"
    31. Two Solutions:
    32. <% cache 'slow_post' do %> <% unless @post.nil? %> <%= @post.title %> <% end %> <% end %> OR Learn Smalltalk
    33. http://github.com/joho/lazy-loader we’re pretty frickin rad, come work for us
    SlideShare Zeitgeist 2009

    + John BartonJohn Barton Nominate

    custom

    121 views, 0 favs, 0 embeds more stats

    A presentation delivered to the Ruby on Rails Ocean more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 121
      • 121 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 3
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories

    Tags