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

YEGRB Nov 2015 - Domain Specific Languages

Nathan
November 17, 2015

YEGRB Nov 2015 - Domain Specific Languages

A brief overview of writing domain specific languages in Ruby

Nathan

November 17, 2015
Tweet

More Decks by Nathan

Other Decks in Programming

Transcript

  1. Questions What is a DSL? Can you give me an

    example of a DSL? When is a good time to create a DSL? How do I write a DSL? Are there alternate implementations?
  2. Domain Specific Language A domain-specific language (DSL) is a computer

    language specialized to a particular application domain. This is in contrast to a general-purpose language (GPL), which is broadly applicable across domains, and lacks specialized features for a particular domain.
  3. When should I create a DSL? When… • There is

    a common task between projects • A clear domain exists • Expressive code has high value DSLs should be written infrequently.
  4. How do I write a DSL? Rails.application.routes.draw do root to:

    'welcome#home' resources :posts get '/login', to: 'sessions#new' end
  5. How do I write a DSL? class Mapper def root(options

    = {}) puts "Map a URL of '/' to controller and action in options" end def get(*args, &block) puts "Map a URL given in args to controller and action in args" end def resources(*resources, &block) puts "Map multiple URLs for #{resources.first} to the associated controller and actions" end end
  6. How do I write a DSL? mapper = Mapper.new mapper.instance_eval

    do root to: 'welcome#home' resources :posts get '/login', to: 'sessions#new' end class Mapper def root(options = {}) puts "Map a URL of '/' to controller and action in options" end def get(*args, &block) puts "Map a URL given in args to controller and action in args" end def resources(*resources, &block) puts "Map multiple URLs for #{resources.first} to the associated controller and actions" end end
  7. How do I write a DSL? class Mapper def root(options

    = {}) puts "Map a URL of '/' to controller and action in options" end def get(*args, &block) puts "Map a URL given in args to controller and action in args" end def resources(*resources, &block) puts "Map multiple URLs for #{resources.first} to the associated controller and actions" end end class RouteSet def draw(&block) mapper = Mapper.new(self) mapper.instance_eval(&block) end end
  8. How do I write a DSL? Rails.application.routes.draw do root to:

    'welcome#home' resources :posts get '/login', to: 'sessions#new' end class RouteSet def draw(&block) mapper = Mapper.new(self) mapper.instance_eval(&block) end end class Mapper def root(options = {}) puts "Map a URL of '/' to controller and action in options" end def get(*args, &block) puts "Map a URL given in args to controller and action in args" end def resources(*resources, &block) puts "Map multiple URLs for #{resources.first} to the associated controller and actions" end end
  9. Are there alternate implementations? class MyApplicationRoutes < RailsMapper root to:

    'welcome#home' resources :posts get '/login', to: 'sessions#new' end • instance_exec is similar to instance_eval but takes additional arguments