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

Design patterns – Belarus Ruby on Rails User Gr...

Design patterns – Belarus Ruby on Rails User Group 23 Feb 2013

Avatar for Sergey Nartimov

Sergey Nartimov

February 23, 2013
Tweet

More Decks by Sergey Nartimov

Other Decks in Programming

Transcript

  1. About • Rails, Rubinius, Elixir contributor • Software engineer at

    Brainspec Sergey Nartimov Brainspec https://github.com/lest twitter: @just_lest
  2. $ wc -l app/models/**/*.rb | sort -nr 5450 total 595

    app/models/order.rb 434 app/models/user.rb 326 app/models/variant.rb 314 app/models/product.rb 279 app/models/invoice.rb 248 app/models/cart.rb
  3. $ wc -l app/models/**/*.rb | sort -nr 5450 total 595

    app/models/order.rb 434 app/models/user.rb 326 app/models/variant.rb 314 app/models/product.rb 279 app/models/invoice.rb 248 app/models/cart.rb https://github.com/drhenner/ror_ecommerce
  4. A pattern is a careful description of a perennial solution

    to a recurring problem within a building context, describing one of the configurations that brings life to a building.
  5. Strategy class Status attr_accessor :publisher def publish publisher.publish(status) end end

    class TwitterPublisher def publish(status) # ... end end class FacebookPublisher def publish(status) # ... end end
  6. Strategy class Status attr_accessor :publisher def publish publisher.publish(status) end end

    class TwitterPublisher def publish(status) # ... end end class FacebookPublisher def publish(status) # ... end end status = Status.new status.publisher = TwitterPublisher.new status.publish
  7. Decorator class Post def publish # ... end end class

    EmailNotifier < SimpleDelegator def publish super && notify! end private def notify! # ... end end
  8. Decorator class Post def publish # ... end end class

    EmailNotifier < SimpleDelegator def publish super && notify! end private def notify! # ... end end post = Post.new notifiable_post = EmailNotifier.new(post) notifiable_post.publish
  9. Command class CreateProducts < ActiveRecord::Migration def up create_table :products do

    |t| t.string :name t.text :description t.timestamps end end def down drop_table :products end end