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 so that there can be no thread safety issues at runtime.



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

def freeze
  opts[:namespaced_routes].freeze
  opts[:namespaced_routes].each_value(&:freeze)
  super
end

#inherited(subclass) ⇒ Object

Copy the named routes into the subclass when inheriting.



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

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.



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

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



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

def named_routes(namespace=nil)
  unless routes = opts[:namespaced_routes][namespace]
    RodaPlugins.warn "Attempt to access multi_route namespace for which no routes have been defined: #{namespace}"
  end
  routes ? 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.



167
168
169
170
171
172
173
174
175
# File 'lib/roda/plugins/multi_route.rb', line 167

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