router_ext.rb

Path: merb-slices/lib/merb-slices/router_ext.rb
Last Update: Tue Jul 28 08:32:52 +0200 2009

Methods

Public Instance methods

Add a Slice in a router namespace

@param slice_module<String, Symbol, Module> A Slice module to mount. @param options<Hash, String> Optional hash, set :path if you want to override what appears on the url.

@yield A new Behavior instance is yielded in the block for nested routes - runs before the slice routes are setup. @yieldparam ns<Behavior> The namespace behavior object.

@return <Behaviour> The current router context.

@note If a slice has no routes at all, the activate hook won‘t be executed.

@note Normally you should specify the slice_module using a String or Symbol

      this ensures that your module can be removed from the router at runtime.

[Source]

    # File merb-slices/lib/merb-slices/router_ext.rb, line 37
37:   def add_slice(slice_module, options = {}, &block)
38:     if Merb::Slices.exists?(slice_module)
39:       options = { :path => options } if options.is_a?(String)
40:       slice_module = Object.full_const_get(slice_module.to_s.camel_case) if slice_module.class.in?(String, Symbol)
41:       namespace = options[:namespace] || slice_module.identifier_sym
42:       options[:path] ||= options[:path_prefix] || slice_module[:path_prefix] || options[:namespace] || slice_module.identifier
43:       options[:prepend_routes] = block if block_given?
44:       slice_module[:path_prefix] = options[:path]
45:       Merb.logger.verbose!("Mounting slice #{slice_module} at /#{options[:path]}")
46:       
47:       # reset the inherited controller prefix - especially for 'slice' entries (see below)
48:       @options[:controller_prefix] = nil if options.delete(:reset_controller_prefix)
49:       
50:       # setup routes - capture the slice's routes for easy reference
51:       self.namespace(namespace, options.except(:default_routes, :prepend_routes, :append_routes, :path_prefix)) do |ns|
52:         Merb::Slices.named_routes[slice_module.identifier_sym] = ns.capture do
53:           options[:prepend_routes].call(ns) if options[:prepend_routes].respond_to?(:call)
54:           slice_module.setup_router(ns)     # setup the routes from the slice itself
55:           options[:append_routes].call(ns)  if options[:append_routes].respond_to?(:call)
56:         end
57:       end
58:     else 
59:       Merb.logger.info!("Skipped adding slice #{slice_module} to router...")
60:     end
61:     self
62:   end
add_slices(config = {}, &block)

Alias for all_slices

Add all known slices to the router

By combining this with Merb::Slices.activate_by_file and Merb::Slices.deactivate one can enable/disable slices at runtime, without restarting your app.

@param config<Hash>

 Optional hash, mapping slice module names to their settings;
 set :path (or use a string) if you want to override what appears on the url.

@yield A new Behavior instance is yielded in the block for nested routes. @yieldparam ns<Behavior> The namespace behavior object.

@example all_slices(‘BlogSlice’ => ‘blog’, ‘ForumSlice’ => { :path => ‘forum’ })

@note The block is yielded for each slice individually.

[Source]

    # File merb-slices/lib/merb-slices/router_ext.rb, line 18
18:   def all_slices(config = {}, &block)
19:     Merb::Slices.slice_names.each { |module_name| add_slice(module_name, config[module_name] || {}, &block) }
20:   end

Insert a slice directly into the current router context.

This will still setup a namespace, but doesn‘t set a path prefix. Only for special cases.

[Source]

    # File merb-slices/lib/merb-slices/router_ext.rb, line 67
67:   def slice(slice_module, options = {}, &block)
68:     options[:path] ||= ""
69:     add_slice(slice_module, options.merge(:reset_controller_prefix => true), &block)
70:   end

[Validate]