class Ruta::Handlers

Public Class Methods

define_for(context, &block) click to toggle source

define handlers for a context

@example

Ruta::Handlers.define_for :main do
 handler :header do |params,url|
  some code that process the params and returns a component
 end
 handler :footer do |params,url|
  some code that process the params and returns a component
 end
end

@param [Symbol] context to define handlers for @yield block containing handlers for a context

# File lib/ruta/handler.rb, line 62
def define_for context, &block
   new(Context.collection.fetch(context){|c_n|raise"Tried to define handlers for #{c_n} before it exists"},
   block)
end
new(context,block) click to toggle source

@see Handlers#define_for

# File lib/ruta/handler.rb, line 17
def initialize context,block
  @context = context
  instance_exec(&block)
end

Public Instance Methods

default() click to toggle source

Render the default content for this component as it is defined in the context.

# File lib/ruta/handler.rb, line 35
def default
  handler_name = @handler_name
  proc {
    comp = @context.elements[handler_name][:content]
    if comp.kind_of?(Proc)
      comp.call
    else
      Context.wipe handler_name
      Context.render comp, handler_name
    end
  }
end
handle(handler_name,&handler) click to toggle source

create a handle to be excuted when a matching route is hit

@param [Symbol] handler_name the unique ident of the handler, it should match the id of an element that you want the component to be rendered to @yield [params,path] a block containing logic for processing any params before passing it to a component to render @yieldparam [{Symbol => String}] params containing a list of params passed into it from the matching route @yieldparam [String] path the non processed url @yieldreturn [Object] a component that will be passed to the renderer to be rendered to the page

# File lib/ruta/handler.rb, line 11
def handle handler_name,&handler
    @handler_name = handler_name
    @context.handlers[@handler_name] = handler
end
mount(context) click to toggle source

wipe the matching element and render a context

@param [Symbol] context context to be mounted to matching element of handler

# File lib/ruta/handler.rb, line 25
def mount context
  handler_name = @handler_name
  proc {
    Context.wipe handler_name
    Context.render context, handler_name
  }
end