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.



141
142
143
144
145
146
147
148
# File 'lib/roda/plugins/multi_route.rb', line 141

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.



151
152
153
154
155
156
# File 'lib/roda/plugins/multi_route.rb', line 151

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.



167
168
169
# File 'lib/roda/plugins/multi_route.rb', line 167

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



159
160
161
162
163
164
# File 'lib/roda/plugins/multi_route.rb', line 159

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.



174
175
176
177
178
179
180
181
182
# File 'lib/roda/plugins/multi_route.rb', line 174

def route(name=nil, namespace=nil, &block)
  if name
    routes = opts[:namespaced_routes][namespace] ||= {}
    routes[name] = define_roda_method(routes[name] || "multi_route_#{namespace}_#{name}", 1, &convert_route_block(block))
    self::RodaRequest.clear_named_route_regexp!(namespace)
  else
    super(&block)
  end
end