Module: Roda::RodaPlugins::NamedRoutes::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#freezeObject

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



115
116
117
118
# File 'lib/roda/plugins/named_routes.rb', line 115

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

#inherited(subclass) ⇒ Object

Copy the named routes into the subclass when inheriting.



121
122
123
124
125
# File 'lib/roda/plugins/named_routes.rb', line 121

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

#named_route(name, namespace = nil) ⇒ Object

Return the named route with the given name.



136
137
138
# File 'lib/roda/plugins/named_routes.rb', line 136

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



128
129
130
131
132
133
# File 'lib/roda/plugins/named_routes.rb', line 128

def named_routes(namespace=nil)
  unless routes = opts[:namespaced_routes][namespace]
    raise RodaError, "unsupported named_routes 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.



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/roda/plugins/named_routes.rb', line 143

def route(name=nil, namespace=nil, &block)
  if name
    routes = opts[:namespaced_routes][namespace] ||= {}
    if block
      routes[name] = define_roda_method(routes[name] || "named_routes_#{namespace}_#{name}", 1, &convert_route_block(block))
    elsif meth = routes.delete(name)
      remove_method(meth)
    end
  else
    super(&block)
  end
end