Share
Embed
YEGRB Nov 2015 - Domain Specific Languages
A brief overview of writing domain specific languages in Ruby
Nathan
November 17, 2015
Transcript
13 slides
-
Domain Specific Languages in Ruby Presented by Nathan Feaver, Senior
Developer at AgileStyle -
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? -
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. -
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. -
How do I write a DSL? Rails.application.routes.draw do root to:
'welcome#home' resources :posts get '/login', to: 'sessions#new' end -
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 -
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 -
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 -
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 -
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