Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
DSL or NoDSL?


José Valim       blog.plataformatec.com   @josevalim
DSL or NoDSL?


   ID                    blog              twitter

José Valim       blog.plataformatec.com   @josevalim
A brief introduction...




José Valim          blog.plataformatec.com   @josevalim
A short story...




José Valim      blog.plataformatec.com   @josevalim
José Valim   blog.plataformatec.com   @josevalim
mail_form




José Valim    blog.plataformatec.com   @josevalim
Contact form recipe




José Valim        blog.plataformatec.com   @josevalim
Contact form recipe

               ✦   Controller (20 LOC)




José Valim            blog.plataformatec.com   @josevalim
Contact form recipe

               ✦ Controller (20 LOC)
               ✦ Model (30 LOC)




José Valim          blog.pla...
Contact form recipe

               ✦ Controller (20 LOC)
               ✦ Model (30 LOC)

               ✦ View (30 LOC)
...
Contact form recipe

               ✦ Controller (20 LOC)
               ✦ Model (30 LOC)

               ✦ View (30 LOC)
...
Contact form recipe

               ✦ Controller (20 LOC)
               ✦ Model (30 LOC)

               ✦ View (30 LOC)
...
Contact form recipe

               ✦ Controller (20 LOC)
               ✦ Model (30 LOC)

               ✦ View (30 LOC)
...
Let’s create a DSL!




José Valim        blog.plataformatec.com   @josevalim
Domain Speci c Language




José Valim       blog.plataformatec.com   @josevalim
class ContactForm < MailForm::Base
         to "jose.valim@plataformatec.com.br"
         from "contact_form@app_name.com"...
from { |c| "#{c.name} <#{c.email}>" }




José Valim               blog.plataformatec.com      @josevalim
to :author_email

             def author_email
               Author.find(self.author_id).email
             end




José...
headers { |c|
               { "X-Spam" => "True" } if c.honey_pot
             }




José Valim                blog.plata...
class ContactForm < MailForm::Base
               to :author_email
               from { |c| "#{c.name} <#{c.email}>" }
  ...
Issues




José Valim   blog.plataformatec.com   @josevalim
Issues


             ✦   Lacks simplicity




José Valim                 blog.plataformatec.com   @josevalim
Issues


             ✦ Lacks simplicity
             ✦ Reduces programmer happiness




José Valim             blog.plata...
NoDSL!


José Valim   blog.plataformatec.com   @josevalim
From: GitHub <noreply@github.com>
         To: jose.valim@plataformatec.com.br
         Message-Id: <4bfe3e812c22fc17d@git...
class ContactForm < MailForm::Base
               attributes :name, :email, :message

               def headers
         ...
It provides a nice DSL




José Valim          blog.plataformatec.com   @josevalim
It relies on a simple contract




José Valim       blog.plataformatec.com   @josevalim
José Valim   blog.plataformatec.com   @josevalim
Rack




José Valim   blog.plataformatec.com   @josevalim
class RackApp
   def self.call(env)
     [200, { "Content-Type" => "text/html"}, ["Hello"]]
   end
 end




José Valim    ...
RackApp = Rack::AppBuilder.new do |env|
               status 200
               headers "Content-Type" => "text/html"
   ...
Rake and Thor




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

             namespace :app do
               task ...
task :process do
               # do some processing
             end

             namespace :app do
               task ...
class Default < Thor
               def process
                 # do some processing
               end
             end
...
class Default < Thor
               def process
                 # do some processing
               end
             end
...
Rspec and Test::Unit




José Valim         blog.plataformatec.com   @josevalim
# Rspec
             describe User do
               it "should be valid" do
                 User.new(@attributes).should...
A nal story...




José Valim      blog.plataformatec.com   @josevalim
class UsersController < ApplicationController
                  def index
                    @users = User.all


        ...
class UsersController < ApplicationController
               restful!
             end




José Valim                    b...
class UsersController < ApplicationController
               restful!

              create do
                before {
  ...
InheritedResources




José Valim        blog.plataformatec.com   @josevalim
class UsersController < InheritedResources::Base
         def create
           # do something before
           flash[:su...
Bene ts




José Valim   blog.plataformatec.com   @josevalim
Bene ts

             ✦   Simple




José Valim                blog.plataformatec.com   @josevalim
Bene ts

             ✦ Simple
             ✦ Less code to maintain




José Valim              blog.plataformatec.com   @...
Bene ts

             ✦ Simple
             ✦ Less code to maintain

             ✦ More time to focus on important

     ...
Bene ts

             ✦ Simple
             ✦ Less code to maintain

             ✦ More time to focus on important

     ...
José Valim   blog.plataformatec.com   @josevalim
ORM Callbacks




José Valim      blog.plataformatec.com   @josevalim
# DSL
 class User < ActiveRecord::Base
   after_save :deliver_notification, :unless => :admin?
 end

 # NoDSL
 class User ...
DSL or NoDSL?


José Valim       blog.plataformatec.com   @josevalim
Thank you!




José Valim    blog.plataformatec.com   @josevalim
Questions? Feedback?
                  Opinions?



José Valim         blog.plataformatec.com   @josevalim
Questions? Feedback?
                  Opinions?


   ID                      blog              twitter

José Valim       ...
Upcoming SlideShare
Loading in …5
×

DSL or NoDSL - Euruko - 29may2010

2,368 views

Published on

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

Published in: Technology, News & Politics
  • Be the first to comment

DSL or NoDSL - Euruko - 29may2010

  1. 1. DSL or NoDSL? José Valim blog.plataformatec.com @josevalim
  2. 2. DSL or NoDSL? ID blog twitter José Valim blog.plataformatec.com @josevalim
  3. 3. A brief introduction... José Valim blog.plataformatec.com @josevalim
  4. 4. A short story... José Valim blog.plataformatec.com @josevalim
  5. 5. José Valim blog.plataformatec.com @josevalim
  6. 6. mail_form José Valim blog.plataformatec.com @josevalim
  7. 7. Contact form recipe José Valim blog.plataformatec.com @josevalim
  8. 8. Contact form recipe ✦ Controller (20 LOC) José Valim blog.plataformatec.com @josevalim
  9. 9. Contact form recipe ✦ Controller (20 LOC) ✦ Model (30 LOC) José Valim blog.plataformatec.com @josevalim
  10. 10. Contact form recipe ✦ Controller (20 LOC) ✦ Model (30 LOC) ✦ View (30 LOC) José Valim blog.plataformatec.com @josevalim
  11. 11. Contact form recipe ✦ Controller (20 LOC) ✦ Model (30 LOC) ✦ View (30 LOC) ✦ Mailer (15 LOC) José Valim blog.plataformatec.com @josevalim
  12. 12. Contact form recipe ✦ Controller (20 LOC) ✦ Model (30 LOC) ✦ View (30 LOC) ✦ Mailer (15 LOC) ✦ View (20 LOC) José Valim blog.plataformatec.com @josevalim
  13. 13. Contact form recipe ✦ Controller (20 LOC) ✦ Model (30 LOC) ✦ View (30 LOC) ✦ Mailer (15 LOC) ✦ View (20 LOC) José Valim blog.plataformatec.com @josevalim
  14. 14. Let’s create a DSL! José Valim blog.plataformatec.com @josevalim
  15. 15. Domain Speci c Language José Valim blog.plataformatec.com @josevalim
  16. 16. 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 José Valim blog.plataformatec.com @josevalim
  17. 17. from { |c| "#{c.name} <#{c.email}>" } José Valim blog.plataformatec.com @josevalim
  18. 18. to :author_email def author_email Author.find(self.author_id).email end José Valim blog.plataformatec.com @josevalim
  19. 19. headers { |c| { "X-Spam" => "True" } if c.honey_pot } José Valim blog.plataformatec.com @josevalim
  20. 20. 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 José Valim blog.plataformatec.com @josevalim
  21. 21. Issues José Valim blog.plataformatec.com @josevalim
  22. 22. Issues ✦ Lacks simplicity José Valim blog.plataformatec.com @josevalim
  23. 23. Issues ✦ Lacks simplicity ✦ Reduces programmer happiness José Valim blog.plataformatec.com @josevalim
  24. 24. NoDSL! José Valim blog.plataformatec.com @josevalim
  25. 25. 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 José Valim blog.plataformatec.com @josevalim
  26. 26. 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 José Valim blog.plataformatec.com @josevalim
  27. 27. It provides a nice DSL José Valim blog.plataformatec.com @josevalim
  28. 28. It relies on a simple contract José Valim blog.plataformatec.com @josevalim
  29. 29. José Valim blog.plataformatec.com @josevalim
  30. 30. Rack José Valim blog.plataformatec.com @josevalim
  31. 31. class RackApp def self.call(env) [200, { "Content-Type" => "text/html"}, ["Hello"]] end end José Valim blog.plataformatec.com @josevalim
  32. 32. RackApp = Rack::AppBuilder.new do |env| status 200 headers "Content-Type" => "text/html" body ["Hello"] after_return { # do something } end José Valim blog.plataformatec.com @josevalim
  33. 33. Rake and Thor José Valim blog.plataformatec.com @josevalim
  34. 34. task :process do # do some processing end namespace :app do task :setup do # do some setup # end end rake app:setup José Valim blog.plataformatec.com @josevalim
  35. 35. 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 José Valim blog.plataformatec.com @josevalim
  36. 36. class Default < Thor def process # do some processing end end class App < Thor def setup # do some setup # end end thor app:setup José Valim blog.plataformatec.com @josevalim
  37. 37. 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 José Valim blog.plataformatec.com @josevalim
  38. 38. Rspec and Test::Unit José Valim blog.plataformatec.com @josevalim
  39. 39. # 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 José Valim blog.plataformatec.com @josevalim
  40. 40. A nal story... José Valim blog.plataformatec.com @josevalim
  41. 41. 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 Scaffold Controller 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 José Valim blog.plataformatec.com @josevalim
  42. 42. class UsersController < ApplicationController restful! end José Valim blog.plataformatec.com @josevalim
  43. 43. 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 José Valim blog.plataformatec.com @josevalim
  44. 44. InheritedResources José Valim blog.plataformatec.com @josevalim
  45. 45. class UsersController < InheritedResources::Base def create # do something before flash[:success] = "This is the message" super # do something after end end José Valim blog.plataformatec.com @josevalim
  46. 46. Bene ts José Valim blog.plataformatec.com @josevalim
  47. 47. Bene ts ✦ Simple José Valim blog.plataformatec.com @josevalim
  48. 48. Bene ts ✦ Simple ✦ Less code to maintain José Valim blog.plataformatec.com @josevalim
  49. 49. Bene ts ✦ Simple ✦ Less code to maintain ✦ More time to focus on important features José Valim blog.plataformatec.com @josevalim
  50. 50. Bene ts ✦ Simple ✦ Less code to maintain ✦ More time to focus on important features ✦ Just Ruby™ José Valim blog.plataformatec.com @josevalim
  51. 51. José Valim blog.plataformatec.com @josevalim
  52. 52. ORM Callbacks José Valim blog.plataformatec.com @josevalim
  53. 53. # 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 José Valim blog.plataformatec.com @josevalim
  54. 54. DSL or NoDSL? José Valim blog.plataformatec.com @josevalim
  55. 55. Thank you! José Valim blog.plataformatec.com @josevalim
  56. 56. Questions? Feedback? Opinions? José Valim blog.plataformatec.com @josevalim
  57. 57. Questions? Feedback? Opinions? ID blog twitter José Valim blog.plataformatec.com @josevalim

×