Class: Derailleur::DefaultRackHandler

Inherits:
RackHandler show all
Defined in:
lib/derailleur/core/handler.rb

Overview

The default rack handler is a handler that does several default things:

  • if the object respond to to_rack_output (like, another handler) it will set the handler’s env to the current one (the handler must also respond to :env= and :ctx=) and call its to_rack_output method

  • if it’s a Proc, it calls the proc passing the env and ctx as block parameters

  • if it’s a subclass of Handler, it instantiates it (without object, and with the same env and ctx)

Instance Attribute Summary

Attributes included from Handler::Rack

#headers, #page, #status

Attributes inherited from Handler

#ctx, #env, #object

Instance Method Summary collapse

Methods inherited from RackHandler

#initialize

Methods included from Handler::Rack

#initialize_rack

Methods inherited from Handler

#initialize

Constructor Details

This class inherits a constructor from Derailleur::RackHandler

Instance Method Details

#do_forward_outputObject



61
62
63
64
65
# File 'lib/derailleur/core/handler.rb', line 61

def do_forward_output
  object.env = env
  object.ctx = ctx
  object.to_rack_output 
end

#do_handler_instantiation_outputObject



71
72
73
# File 'lib/derailleur/core/handler.rb', line 71

def do_handler_instantiation_output
  object.new(nil, env, ctx).to_rack_output
end

#do_non_forward_outputObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/derailleur/core/handler.rb', line 75

def do_non_forward_output
  ret = case object
  when Proc
    do_proc_output
  when Class
    if object.ancestors.include? RackHandler
      do_handler_instantiation_output
    else
      raise InvalidHandler, "invalid handler: #{object}"
    end
  else
    raise InvalidHandler, "invalid handler: #{object}"
  end
  ret
end

#do_proc_outputObject



67
68
69
# File 'lib/derailleur/core/handler.rb', line 67

def do_proc_output
  object.call(env, ctx)
end

#to_rack_outputObject



53
54
55
56
57
58
59
# File 'lib/derailleur/core/handler.rb', line 53

def to_rack_output
  if object.respond_to? :to_rack_output
    do_forward_output
  else
    do_non_forward_output
  end
end