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

Why You Should Never Use an ORM

Why You Should Never Use an ORM

My presentation at RailsConf 2011 on thinking about your interface, your data and your code.

John Nunemaker

May 17, 2011
Tweet

More Decks by John Nunemaker

Other Decks in Programming

Transcript

  1. class ContentPresenter include BasePresenter def initialize(site, date, options={}) @site, @date

    = site, date @options = options end def page # ... def per_page # ... def total # ... def path # ... def prev_path # ... def next_path # ... def next_page_path # ... def prev_page_path # ... def paginated # ... def content # ...
  2. def path # ... def prev_path # ... def next_path

    # ... def next_page_path # ... def prev_page_path # ... def paginated # ... def content # ... def as_json(options = nil) { 'date' => @date.to_s, 'prev_path' => prev_path, 'next_path' => next_path, 'content' => content, 'page' => page, 'per_page' => per_page, 'total' => total, 'prev_page_path' => prev_page_path, 'next_page_path' => next_page_path, } end end
  3. Content.paginate({ :conditions => { :site_id => site.id, :date => date,

    }, :page => params['page'], :per_page => 15, })
  4. { '_id' => 'site_id:2011-03-28', '/' => {'v' => 200, 't'

    => 'Home'}, '/about/' => {'v' => 50, 't' => 'About'}, '/foo/' => {'v' => 23, 't' => 'Foo!'}, }
  5. class Site States = { 'enabled' => 1, 'disabled' =>

    2, } end Site.create({ :state => Site::States['enabled'], })
  6. class Stylesheet class RSmazCompressor def self.compress(str) RSmaz.compress(str) end def self.decompress(str)

    RSmaz.decompress(str) end end class ZlibCompressor def self.compress(str) Zlib::Deflate.deflate(str) end def self.decompress(str) Zlib::Deflate.inflate(str) end end
  7. Compressors = { 1 => RSmazCompressor, 2 => ZlibCompressor, }

    key :compressor_id, Integer key :contents, String validates_inclusion_of :compressor_id, :within => Compressors.keys def contents value = read_attribute(:contents) compressor.decompress(value) end def compressor Compressors[compressor_id] end end
  8. SiteMode.create({ :id => 1, :name => 'live' }) SiteMode.all #

    find by id or string id pp SiteMode.find(2) pp SiteMode.find('2')
  9. class Hit def site @site ||= begin query = {'_id'

    => site_id} options = {:fields => ['tz']} collection.find_one(query, options) end end end
  10. “ Albert Einstein It takes a touch of genius —and

    a lot of courage—to move in the opposite direction.