Class: Ruta::Handlers

Inherits:
Object
  • Object
show all
Defined in:
lib/ruta/handler.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, block) ⇒ Handlers

Returns a new instance of Handlers.

See Also:

  • Ruta::Handlers#Handlers#Handlers#define_for


17
18
19
20
# File 'lib/ruta/handler.rb', line 17

def initialize context,block
  @context = context
  instance_exec(&block)
end

Class Method Details

.define_for(context) { ... } ⇒ Object

define handlers for a context

Examples:

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

Parameters:

  • context (Symbol)

    to define handlers for

Yields:

  • block containing handlers for a context



62
63
64
65
# 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

Instance Method Details

#defaultObject

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



35
36
37
38
39
40
41
42
43
44
45
46
# 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) {|params, path| ... } ⇒ Object

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

Parameters:

  • handler_name (Symbol)

    the unique ident of the handler, it should match the id of an element that you want the component to be rendered to

Yields:

  • (params, path)

    a block containing logic for processing any params before passing it to a component to render

Yield Parameters:

  • params ({Symbol => String})

    containing a list of params passed into it from the matching route

  • path (String)

    the non processed url

Yield Returns:

  • (Object)

    a component that will be passed to the renderer to be rendered to the page



11
12
13
14
# File 'lib/ruta/handler.rb', line 11

def handle handler_name,&handler
    @handler_name = handler_name
    @context.handlers[@handler_name] = handler
end

#mount(context) ⇒ Object

wipe the matching element and render a context

Parameters:

  • context (Symbol)

    context to be mounted to matching element of handler



25
26
27
28
29
30
31
# 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