Coder Social home page Coder Social logo

lazy-loader's Introduction

Lazy Loader
===========

got some expensive method call in you controller for some data that isn't always used in the view (perhaps it's referred to in a partial cache block)? Don't want to mess up your controller with duplicate conditionals from your views?

use the lazy loader! it's easy and fun!

if in your controller you previously had
  
  def index
    @posts = Post.find_all_by_a_painfully_slow_method
  end

you would now write

  def index
    @posts = lazy_load { Post.find_all_by_a_painfully_slow_method }
  end

and your painfully slow finder methods will only be called if @posts is actually referred to in the view.

This works by created a LazyLoader proxy object that takes the place of your collection (or whatever it is) and will instantiate your object the first time any method is called on it. It then caches your original object and routes all methods calls through. 

You will get a performance hit if you are calling many methods on your object, but the common case for these is returning a collection of ActiveRecord::Base and once you've called each you're done with the proxy.

Hopefully this can help you keep your data code neat and tidy in the face of caching and nasty complex views

See http://gist.github.com/50426 for a short list of the nilClass gotchas

=====================================
Copyright 2008 John Barton and Envato

lazy-loader's People

Contributors

joho avatar ianwhite avatar

Stargazers

Angus H. avatar  avatar jtribe apps avatar ::1 avatar Ryan Allen avatar Tikhon Bernstam avatar  avatar  avatar Tim Riley avatar Bo Jeanes avatar Colin Schlueter avatar Cristi Balan avatar Dr Nic Williams avatar

Watchers

 avatar James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.