Class: Rage::Router::DSL::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/rage/router/dsl.rb

Instance Method Summary collapse

Instance Method Details

#delete(path, to:, constraints: nil) ⇒ Object

Register a new DELETE route.

Examples:

delete "/photos/:id", to: "photos#destroy", constraints: { host: /myhost/ }

Parameters:

  • path (String)

    the path for the route handler

  • to (String)

    the route handler in the format of "controller#action"

  • constraints (Hash) (defaults to: nil)

    a hash of constraints for the route



72
73
74
# File 'lib/rage/router/dsl.rb', line 72

def delete(path, to:, constraints: nil)
  __on("DELETE", path, to, constraints)
end

#get(path, to:, constraints: nil) ⇒ Object

Register a new GET route.

Examples:

get "/photos/:id", to: "photos#show", constraints: { host: /myhost/ }

Parameters:

  • path (String)

    the path for the route handler

  • to (String)

    the route handler in the format of "controller#action"

  • constraints (Hash) (defaults to: nil)

    a hash of constraints for the route



28
29
30
# File 'lib/rage/router/dsl.rb', line 28

def get(path, to:, constraints: nil)
  __on("GET", path, to, constraints)
end

#patch(path, to:, constraints: nil) ⇒ Object

Register a new PATCH route.

Examples:

patch "/photos/:id", to: "photos#update", constraints: { host: /myhost/ }

Parameters:

  • path (String)

    the path for the route handler

  • to (String)

    the route handler in the format of "controller#action"

  • constraints (Hash) (defaults to: nil)

    a hash of constraints for the route



61
62
63
# File 'lib/rage/router/dsl.rb', line 61

def patch(path, to:, constraints: nil)
  __on("PATCH", path, to, constraints)
end

#post(path, to:, constraints: nil) ⇒ Object

Register a new POST route.

Examples:

post "/photos", to: "photos#create", constraints: { host: /myhost/ }

Parameters:

  • path (String)

    the path for the route handler

  • to (String)

    the route handler in the format of "controller#action"

  • constraints (Hash) (defaults to: nil)

    a hash of constraints for the route



39
40
41
# File 'lib/rage/router/dsl.rb', line 39

def post(path, to:, constraints: nil)
  __on("POST", path, to, constraints)
end

#put(path, to:, constraints: nil) ⇒ Object

Register a new PUT route.

Examples:

put "/photos/:id", to: "photos#update", constraints: { host: /myhost/ }

Parameters:

  • path (String)

    the path for the route handler

  • to (String)

    the route handler in the format of "controller#action"

  • constraints (Hash) (defaults to: nil)

    a hash of constraints for the route



50
51
52
# File 'lib/rage/router/dsl.rb', line 50

def put(path, to:, constraints: nil)
  __on("PUT", path, to, constraints)
end

#root(to:) ⇒ Object

Register a new route pointing to '/'.

Examples:

root to: "photos#index"

Parameters:

  • to (String)

    the route handler in the format of "controller#action"



81
82
83
# File 'lib/rage/router/dsl.rb', line 81

def root(to:)
  __on("GET", "/", to, nil)
end

#scope(opts, &block) ⇒ Object

Scopes a set of routes to the given default options.

Examples:

Route /photos to Api::PhotosController

scope module: "api" do
  get "photos", to: "photos#index"
end

Route admin/photos to PhotosController

scope path: "admin" do
  get "photos", to: "photos#index"
end

Nested calls

scope module: "admin" do
  get "photos", to: "photos#index"

  scope path: "api", module: "api" do
    get "photos/:id", to: "photos#show"
  end
end

Parameters:

  • opts (Hash)

    scope options.

Options Hash (opts):

  • :module (String)

    module option

  • :path (String)

    path option

Raises:

  • (ArgumentError)


106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rage/router/dsl.rb', line 106

def scope(opts, &block)
  raise ArgumentError, "only 'module' and 'path' options are accepted" if (opts.keys - %i(module path)).any?

  @path_prefixes << opts[:path].delete_prefix("/").delete_suffix("/") if opts[:path]
  @module_prefixes << opts[:module] if opts[:module]

  instance_eval &block

  @path_prefixes.pop if opts[:path]
  @module_prefixes.pop if opts[:module]
end