class Noodles::Http::Controller

Attributes

env[R]
request[R]
response[R]

Public Class Methods

action(action, routing_params) click to toggle source
# File lib/noodles/http/controller.rb, line 57
def self.action(action, routing_params)
  proc { |e| self.new(e).dispatch(action, routing_params) }
end
new(env) click to toggle source
# File lib/noodles/http/controller.rb, line 14
def initialize(env)
  @env = env
  @routing_params = {}
  @request = Rack::Request.new(env)
  @response = Rack::Response.new([], 200, {'Content-Type' => 'text/html'})
end
view(view) click to toggle source
# File lib/noodles/http/controller.rb, line 67
def self.view(view)
  @@view = view
end

Public Instance Methods

dispatch(action, routing_params) click to toggle source
# File lib/noodles/http/controller.rb, line 61
def dispatch(action, routing_params)
  @routing_params = routing_params
  self.send(action)
  @response.finish
end
html(template_name) click to toggle source
# File lib/noodles/http/controller.rb, line 26
def html(template_name)
  @response.body = [read_file(template_name, :html)]
end
json(jsonized_response) click to toggle source
# File lib/noodles/http/controller.rb, line 30
def json(jsonized_response)
  @response['Content-Type'] = 'application/json'
  unless jsonized_response.is_a? String
    jsonized_response = jsonized_response.to_json
  end
  @response.body = [jsonized_response]
end
params() click to toggle source
# File lib/noodles/http/controller.rb, line 45
def params
  request.params.merge @routing_params
end
redirect(redirect_path, status=302) click to toggle source
# File lib/noodles/http/controller.rb, line 49
def redirect(redirect_path, status=302)
  @response.body = []
  @response['Location'] = redirect_path
  @response.status = status
end
Also aliased as: redirect_to
redirect_to(redirect_path, status=302)
Alias for: redirect
text(textual_response) click to toggle source
# File lib/noodles/http/controller.rb, line 21
def text(textual_response)
  @response['Content-Type'] = 'text/plain'
  @response.body = [textual_response]
end
view_clazz() click to toggle source
# File lib/noodles/http/controller.rb, line 71
def view_clazz
  if defined? @@view
    @@view
  else
    View
  end
end

Private Instance Methods

controller_name() click to toggle source
# File lib/noodles/http/controller.rb, line 89
def controller_name
  klass = self.class.to_s.gsub /Controller$/, ""
  Noodles.to_underscore klass
end
get_rendering_path(template_name, template_type) click to toggle source
# File lib/noodles/http/controller.rb, line 85
def get_rendering_path(template_name, template_type)
  File.join 'app', 'templates', controller_name, "#{template_name}.#{template_type}"
end
map_instance_variables_to_object() click to toggle source
# File lib/noodles/http/controller.rb, line 98
def map_instance_variables_to_object
  view_clazz.send :include, Noodles::Sessionable
  new_object = view_clazz.new
  mapped_instance_variables.each do |k,v|
    new_object.instance_variable_set k, v
  end
  new_object
end
mapped_instance_variables() click to toggle source
# File lib/noodles/http/controller.rb, line 94
def mapped_instance_variables
  Hash[self.instance_variables.map {|var| [var, self.instance_variable_get(var)]}]
end
read_file(template_name, template_type) click to toggle source
# File lib/noodles/http/controller.rb, line 81
def read_file(template_name, template_type)
  File.read get_rendering_path(template_name, template_type)
end