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

PATH_NAME_MATCHER =

:nodoc:

%r{\A[-+\w/]+\Z}m
VERBS =
%w{get post put delete head options trace}

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Mount.

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/gin/router.rb', line 41

def initialize ctrl, base_path=nil, &block
  raise ArgumentError,
    "#{ctrl.inspect} must respond to `call'" unless ctrl.respond_to?(:call)

  base_path ||= ctrl.controller_name if Gin::Mountable === ctrl

  if !base_path
    uname = Gin.underscore(ctrl.to_s)
    base_path = File.join("", uname) if uname =~ PATH_NAME_MATCHER
  end

  raise ArgumentError,
    "Could not deduce base path from #{ctrl.inspect}" unless base_path

  @ctrl      = ctrl
  @routes    = []
  @actions   = []
  @base_path = base_path

  if block_given?
    instance_eval(&block)
    return
  end

  if !(Gin::Mountable === @ctrl)
    any :call, "/"
  else
    defaults
  end
end

Instance Method Details

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

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

Raises:

  • (ArgumentError)


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/gin/router.rb', line 101

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

  if Gin::Mountable === @ctrl
    path ||= @ctrl.default_route_for(action)[1]
    name ||= @ctrl.route_name_for(action)
  elsif !(path && name) && !action.is_a?(Array) && !action.is_a?(Hash) &&
  action.to_s =~ PATH_NAME_MATCHER
    path ||= action.to_s
    uname = Gin.underscore(@ctrl.to_s).sub(%r{^.*/},'')
    name = "#{action}_#{uname}" if !name && uname =~ PATH_NAME_MATCHER
  end

  raise ArgumentError, "No path could be determined for target %s %s" %
    [@ctrl.inspect, action.inspect] unless path

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

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

#any(action, path = nil) ⇒ Object

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



93
94
95
# File 'lib/gin/router.rb', line 93

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

#defaultsObject

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

Raises:

  • (TypeError)


76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/gin/router.rb', line 76

def defaults
  raise TypeError,
    "#{@ctrl.inspect} must inherit Gin::Mountable to support defaults" unless
    Gin::Mountable === @ctrl

  (@ctrl.actions - @actions).each do |action|
    verb, path = @ctrl.default_route_for(action)

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

#each_route(&block) ⇒ Object

Iterate through through all the routes in the Mount.



130
131
132
# File 'lib/gin/router.rb', line 130

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