Class: Pancake::Router

Inherits:
Usher::Interface::RackInterface
  • Object
show all
Defined in:
lib/pancake/router.rb

Overview

The pancake router is a customized version of the Usher router. Usher is a fast tree based router that can generate routes, have nested routers, and even generate from nested routers.

See Also:

Author:

  • Daniel Neighman

Since:

  • 0.1.2

Direct Known Subclasses

Stack::Router

Defined Under Namespace

Classes: MountedApplication, RackApplicationExpected

Constant Summary collapse

CONFIGURATION_KEY =

Since:

  • 0.1.2

"pancake.request.configuration".freeze
ROUTE_KEY =

Since:

  • 0.1.2

"pancake.request.matching_route".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configurationObject

Since:

  • 0.1.2



47
48
49
# File 'lib/pancake/router.rb', line 47

def configuration
  @configuration
end

#router=(value) ⇒ Object (writeonly)

Since:

  • 0.1.2



41
42
43
# File 'lib/pancake/router.rb', line 41

def router=(value)
  @router = value
end

Instance Method Details

#add(path, opts = {}, &block) ⇒ Object

Adds a route to the router.

See Also:

  • Usher::Interface::RackInterface#add

Since:

  • 0.1.2



117
118
119
120
121
122
123
124
# File 'lib/pancake/router.rb', line 117

def add(path, opts = {}, &block)
  opts = cooerce_options_to_usher(opts)
  route = super(path, opts)
  if block_given?
    route.to(block)
  end
  route
end

#base_url(opts = {}) ⇒ Object

Since:

  • 0.1.2



137
138
139
# File 'lib/pancake/router.rb', line 137

def base_url(opts = {})
  router.generator.generate_base_url(opts)
end

#call(env) ⇒ Object

Since:

  • 0.1.2



141
142
143
144
145
146
147
148
149
# File 'lib/pancake/router.rb', line 141

def call(env)
  orig_config = env[CONFIGURATION_KEY]
  orig_route  = env[ROUTE_KEY]
  env[CONFIGURATION_KEY] = configuration
  super(env)
ensure
  env[CONFIGURATION_KEY] = orig_config
  env[ROUTE_KEY]         = orig_route
end

#mount(mounted_app, path, options = {}) ⇒ Object

Mounts an application in the router as a sub application in the url space. This will route directly to the sub application and skip any middlewares etc defined on a stack

Since:

  • 0.1.2



95
96
97
98
99
# File 'lib/pancake/router.rb', line 95

def mount(mounted_app, path, options = {})
  mounted_app = MountedApplication.new(mounted_app, path, options)
  self.class.mounted_applications << mounted_app
  mounted_app
end

#mount_applications!Object

Since:

  • 0.1.2



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/pancake/router.rb', line 101

def mount_applications!
  # need to set them as mounted here before we actually to mount them.
  # if we just mounted them, the inheritance of the routes would mean that only the first would be mounted on this class
  # and the rest would be mounted on the child class
  apps = self.class.mounted_applications.select do |a|
    a.mounted? ? false : (a.mounted = true)
  end

  apps.each do |app|
    route = add(app.mount_path, app.options)
    app.mount!(route)
  end
end

#url(name_or_path, options = {}) ⇒ Object

Generate a url

Since:

  • 0.1.2



127
128
129
130
131
132
133
134
135
# File 'lib/pancake/router.rb', line 127

def url(name_or_path, options = {})
  if Hash === name_or_path
    name = nil
    options = name_or_path
  else
    name = name_or_path
  end
  generate(name, options)
end