Class: Merb::Router::Behavior
- Inherits:
-
Object
- Object
- Merb::Router::Behavior
- Defined in:
- lib/merb-slices/router_ext.rb
Instance Method Summary collapse
-
#add_slice(slice_module, options = {}) {|ns| ... } ⇒ Behaviour
Add a Slice in a router namespace.
-
#all_slices(config = {}) {|ns| ... } ⇒ Object
(also: #add_slices)
Add all known slices to the router.
-
#slice(slice_module, options = {}, &block) ⇒ Object
Insert a slice directly into the current router context.
Instance Method Details
#add_slice(slice_module, options = {}) {|ns| ... } ⇒ Behaviour
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.
Add a Slice in a router namespace
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/merb-slices/router_ext.rb', line 40 def add_slice(slice_module, = {}, &block) if Merb::Slices.exists?(slice_module) = { :path => } if .is_a?(String) slice_module = Object.full_const_get(slice_module.to_s) if slice_module.class.in?(String, Symbol) namespace = [:namespace] || slice_module.to_s.snake_case [:path] ||= slice_module[:path_prefix] || [:namespace] || slice_module.identifier [:default_routes] = true unless .key?(:default_routes) [:prepend_routes] = block if block_given? slice_module[:path_prefix] = [:path] Merb.logger.info!("Mounting slice #{slice_module} at /#{[:path]}") # setup routes - capture the slice's routes for easy reference slice_module.routes, slice_module.named_routes = Merb::Router.capture do self.namespace(namespace.to_sym, .except(:default_routes, :prepend_routes, :append_routes)) do |ns| [:prepend_routes].call(ns) if [:prepend_routes].respond_to?(:call) slice_module.setup_router(ns) # setup the routes from the slice itself [:append_routes].call(ns) if [:append_routes].respond_to?(:call) ns.default_routes([:params] || {}) if [:default_routes] end end else Merb.logger.info!("Skipped adding slice #{slice_module} to router...") end self end |
#all_slices(config = {}) {|ns| ... } ⇒ Object Also known as: add_slices
Note:
The block is yielded for each slice individually.
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.
21 22 23 |
# File 'lib/merb-slices/router_ext.rb', line 21 def all_slices(config = {}, &block) Merb::Slices.slice_names.each { |module_name| add_slice(module_name, config[module_name] || {}, &block) } end |
#slice(slice_module, options = {}, &block) ⇒ Object
Insert a slice directly into the current router context.
This will still setup a namespace, but doesn’t set a path prefix.
69 70 71 |
# File 'lib/merb-slices/router_ext.rb', line 69 def slice(slice_module, = {}, &block) add_slice(slice_module, .merge(:path => ''), &block) end |