DSL or NoDSL - Euruko - may 29, 2010

DSL or NoDSL - Euruko - may 29, 2010

José Valim (@josevalim) from Plataforma talks about DSL or NoDSL at Euruko 2010 in Kraków/Poland.

7c12adb8b5521c060ab4630360a4fa27?s=128

Plataformatec

March 08, 2012
Tweet

Transcript

  1. 11.

    José Valim @josevalim blog.plataformatec.com Contact form recipe ✦ Controller (20

    LOC) ✦ Model (30 LOC) ✦ View (30 LOC) ✦ Mailer (15 LOC)
  2. 12.

    José Valim @josevalim blog.plataformatec.com Contact form recipe ✦ Controller (20

    LOC) ✦ Model (30 LOC) ✦ View (30 LOC) ✦ Mailer (15 LOC) ✦ View (20 LOC)
  3. 13.

    José Valim @josevalim blog.plataformatec.com Contact form recipe ✦ Controller (20

    LOC) ✦ Model (30 LOC) ✦ View (30 LOC) ✦ Mailer (15 LOC) ✦ View (20 LOC)
  4. 16.

    José Valim @josevalim blog.plataformatec.com class ContactForm < MailForm::Base to "jose.valim@plataformatec.com.br"

    from "contact_form@app_name.com" subject "Contact form" attributes :name, :email, :message end ContactForm.new(params[:contact_form]).deliver
  5. 20.

    José Valim @josevalim blog.plataformatec.com class ContactForm < MailForm::Base to :author_email

    from { |c| "#{c.name} <#{c.email}>" } subject "Contact form" headers { |c| { "X-Spam" => "True" } if c.honey_pot } attributes :name, :email, :message def author_email Author.find(self.author_id).email end end
  6. 25.

    José Valim @josevalim blog.plataformatec.com From: GitHub <noreply@github.com> To: jose.valim@plataformatec.com.br Message-Id:

    <4bfe3e812c22fc17d@github.com> Subject: [GitHub] someone commented on a commit Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 MESSAGE BODY
  7. 26.

    José Valim @josevalim blog.plataformatec.com class ContactForm < MailForm::Base attributes :name,

    :email, :message def headers { :to => author_email, :from => "#{name} <#{email}>", :subject => "Contact form" } end def author_email Author.find(self.author_id).email end end
  8. 32.

    José Valim @josevalim blog.plataformatec.com RackApp = Rack::AppBuilder.new do |env| status

    200 headers "Content-Type" => "text/html" body ["Hello"] after_return { # do something } end
  9. 34.

    José Valim @josevalim blog.plataformatec.com task :process do # do some

    processing end namespace :app do task :setup do # do some setup # end end rake app:setup
  10. 35.

    José Valim @josevalim blog.plataformatec.com task :process do # do some

    processing end namespace :app do task :setup do # do some setup Rake::Task[:process].invoke end end rake app:setup
  11. 36.

    José Valim @josevalim blog.plataformatec.com class Default < Thor def process

    # do some processing end end class App < Thor def setup # do some setup # end end thor app:setup
  12. 37.

    José Valim @josevalim blog.plataformatec.com class Default < Thor def process

    # do some processing end end class App < Thor def setup # do some setup Default.new.process end end thor app:setup
  13. 39.

    José Valim @josevalim blog.plataformatec.com # Rspec describe User do it

    "should be valid" do User.new(@attributes).should be_valid end end # Test::Unit class UserTest < Test::Unit::Case def test_user_validity assert User.new(@attributes).valid? end end
  14. 41.

    José Valim @josevalim blog.plataformatec.com class UsersController < ApplicationController def index

    @users = User.all respond_to do |format| format.html # index.html.erb format.xml { render :xml => @users } end end def show @user = User.find(params[:id]) respond_to do |format| format.html # show.html.erb format.xml { render :xml => @user } end end def new @user = User.new respond_to do |format| format.html # new.html.erb format.xml { render :xml => @user } end end def edit @user = User.find(params[:id]) end def create @user = User.new(params[:user]) respond_to do |format| if @user.save format.html { redirect_to(@user, :notice => 'User was successfully created.') } format.xml { render :xml => @user, :status => :created, :location => @user } else format.html { render :action => "new" } format.xml { render :xml => @user.errors, :status => :unprocessable_entity } end end end def update @user = User.find(params[:id]) respond_to do |format| if @user.update_attributes(params[:user]) format.html { redirect_to(@user, :notice => 'User was successfully updated.') } format.xml { head :ok } else format.html { render :action => "edit" } format.xml { render :xml => @user.errors, :status => :unprocessable_entity } end end end def destroy @user = User.find(params[:id]) @user.destroy respond_to do |format| format.html { redirect_to(users_url) } format.xml { head :ok } end end end Scaffold Controller
  15. 43.

    José Valim @josevalim blog.plataformatec.com class UsersController < ApplicationController restful! create

    do before { # do something before } success.flash "This is the message" end create.after do # do something after end end
  16. 45.

    José Valim @josevalim blog.plataformatec.com class UsersController < InheritedResources::Base def create

    # do something before flash[:success] = "This is the message" super # do something after end end
  17. 49.

    José Valim @josevalim blog.plataformatec.com Benefits ✦ Simple ✦ Less code

    to maintain ✦ More time to focus on important features
  18. 50.

    José Valim @josevalim blog.plataformatec.com Benefits ✦ Simple ✦ Less code

    to maintain ✦ More time to focus on important features ✦ Just Ruby™
  19. 53.

    José Valim @josevalim blog.plataformatec.com # DSL class User < ActiveRecord::Base

    after_save :deliver_notification, :unless => :admin? end # NoDSL class User < ActiveRecord::Base def save(*) if super deliver_notification unless admin? end end end