Class: Gin::Router::Mount

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

Overview

Class for building temporary groups of routes for a given controller. Used by the Gin::App.mount DSL.

Constant Summary collapse

DEFAULT_ACTION_MAP =
{
  :index   => %w{get /},
  :show    => %w{get /:id},
  :new     => %w{get /new},
  :create  => %w{post /:id},
  :edit    => %w{get /:id/edit},
  :update  => %w{put /:id},
  :destroy => %w{delete /:id}
}
VERBS =
%w{get post put delete head options trace}

Instance Method Summary collapse

Constructor Details

#initialize(ctrl, base_path, &block) ⇒ Mount

Returns a new instance of Mount.



48
49
50
51
52
53
54
55
56
# File 'lib/gin/router.rb', line 48

def initialize ctrl, base_path, &block
  @ctrl      = ctrl
  @routes    = []
  @actions   = []
  @base_path = base_path

  instance_eval(&block) if block_given?
  defaults unless block_given?
end

Instance Method Details

#add(verb, action, *args) ⇒ Object

Create a single route and add it to the Mount instance.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/gin/router.rb', line 86

def add verb, action, *args
  path = args.shift        if String === args[0]
  name = args.shift.to_sym if args[0]

  path ||= action.to_s
  name ||= :"#{action}_#{@ctrl.controller_name}"

  path = File.join(@base_path, path)
  target = [@ctrl, action]

  route = Route.new(verb, path, target, name)
  @routes << route
  @actions << action.to_sym
end

#any(action, path = nil) ⇒ Object

Create routes for all standard verbs and add them to the Mount instance.



78
79
80
# File 'lib/gin/router.rb', line 78

def any action, path=nil
  VERBS.each{|verb| send verb, action, path}
end

#defaults(default_verb = nil) ⇒ Object

Create and add default restful routes if they aren’t taken already.



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/gin/router.rb', line 62

def defaults default_verb=nil
  default_verb = default_verb || 'get'

  (@ctrl.actions - @actions).each do |action|
    verb, path = DEFAULT_ACTION_MAP[action]
    verb, path = [default_verb, "/#{action}"] if verb.nil?

    add(verb, action, path) unless verb.nil? ||
      @routes.any?{|route| route === [verb, path] }
  end
end

#each_route(&block) ⇒ Object

Iterate through through all the routes in the Mount.



105
106
107
# File 'lib/gin/router.rb', line 105

def each_route &block
  @routes.each(&block)
end