Class: Pancake::Router

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

Overview

The pancake router uses a http_router router

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
LAYOUT_KEY =

Since:

  • 0.1.2

"pancake.apply_layout".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configurationObject

Since:

  • 0.1.2



44
45
46
# File 'lib/pancake/router.rb', line 44

def configuration
  @configuration
end

#stackObject

Since:

  • 0.1.2



37
38
39
# File 'lib/pancake/router.rb', line 37

def stack
  @stack
end

Instance Method Details

#base_url(opts = {}) ⇒ Object

Since:

  • 0.1.2



110
111
112
# File 'lib/pancake/router.rb', line 110

def base_url(opts = {})
  url_mount.nil? ? "/" : url_mount.url(opts)
end

#call(env) ⇒ Object

Since:

  • 0.1.2



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/pancake/router.rb', line 114

def call(env)
  apply_layout = env[LAYOUT_KEY]
  env[LAYOUT_KEY] = true if stack && stack.use_layout?

  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
  env[LAYOUT_KEY]        = apply_layout
end

#dupObject

Since:

  • 0.1.2



82
83
84
85
# File 'lib/pancake/router.rb', line 82

def dup
  puts "Called DUP: #{caller.first}"
  clone
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



90
91
92
93
94
# File 'lib/pancake/router.rb', line 90

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



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/pancake/router.rb', line 96

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