Module: Cybertrain::Routes

Defined in:
cybertrain/generator/routes_dsl.rb

Overview

The entry point config/routes.rb calls:

Cybertrain::Routes.draw do root “posts#index” resources :posts do |posts| posts.resources :comments, only: [:create, :destroy] posts.member { |m| m.get “preview” } end get “/about”, to: “pages#about” end

Spinel compiles instance_eval(&block) by splicing the block body into the caller with self rebound to the mapper, so receiverless calls work at the top level of the draw block. A block nested inside it is still compiled as a closure of its own, where that rebound self does not exist: a receiverless resources inside resources :posts do ... end runs under CRuby but fails the C build under Spinel (“use of undeclared identifier ‘self’”). Nested blocks therefore take the mapper as their argument. draw do |r| r.root ... end also works (instance_eval passes the receiver as the block argument).

Class Method Summary collapse

Class Method Details

.draw {|mapper| ... } ⇒ nil

Declares the application’s routes; the block’s methods are those of Gen::Mapper. spin run gen runs config/routes.rb and writes gen/routes.rb (re-run it after editing the routes; the development server does).

Yield Parameters:

  • mapper (Gen::Mapper) —

    also the block’s self, at its top level only

Returns:

  • (nil)


398
399
400
401
# File 'cybertrain/generator/routes_dsl.rb', line 398

def self.draw(&block)
  Gen::Mapper.new(@specs).instance_eval(&block)
  nil
end