Module: Roda::RodaPlugins::MultiRoute::ClassMethods

Defined in:
lib/roda/plugins/multi_route.rb

Instance Method Summary collapse

Instance Method Details

#freezeObject

Freeze the namespaced routes and setup the regexp matchers so that there can be no thread safety issues at runtime.



137
138
139
140
141
142
143
144
# File 'lib/roda/plugins/multi_route.rb', line 137

def freeze
  opts[:namespaced_routes].freeze.each do |k,v|
    v.freeze
    self::RodaRequest.named_route_regexp(k)
  end
  self::RodaRequest.instance_variable_get(:@namespaced_route_regexps).freeze
  super
end

#inherited(subclass) ⇒ Object

Copy the named routes into the subclass when inheriting.



147
148
149
150
151
152
# File 'lib/roda/plugins/multi_route.rb', line 147

def inherited(subclass)
  super
  nsr = subclass.opts[:namespaced_routes]
  opts[:namespaced_routes].each{|k, v| nsr[k] = v.dup}
  subclass::RodaRequest.instance_variable_set(:@namespaced_route_regexps, {})
end

#named_route(name, namespace = nil) ⇒ Object

Return the named route with the given name.



163
164
165
# File 'lib/roda/plugins/multi_route.rb', line 163

def named_route(name, namespace=nil)
  opts[:namespaced_routes][namespace][name]
end

#named_routes(namespace = nil) ⇒ Object

The names for the currently stored named routes



155
156
157
158
159
160
# File 'lib/roda/plugins/multi_route.rb', line 155

def named_routes(namespace=nil)
  unless routes = opts[:namespaced_routes][namespace]
    raise RodaError, "unsupported multi_route namespace used: #{namespace.inspect}"
  end
  routes.keys
end

#route(name = nil, namespace = nil, &block) ⇒ Object

If the given route has a name, treat it as a named route and store the route block. Otherwise, this is the main route, so call super.



170
171
172
173
174
175
176
177
178
# File 'lib/roda/plugins/multi_route.rb', line 170

def route(name=nil, namespace=nil, &block)
  if name
    opts[:namespaced_routes][namespace] ||= {}
    opts[:namespaced_routes][namespace][name] = block
    self::RodaRequest.clear_named_route_regexp!(namespace)
  else
    super(&block)
  end
end