lib/nyara/controller.rb in nyara-0.1.pre.0 vs lib/nyara/controller.rb in nyara-0.1.pre.1

- old
+ new

@@ -7,10 +7,15 @@ # http :get, '/' do # send_string 'hello world' # end # def http method, path, &blk + # special treatment: '/' also maps '' + if path == '/' + http method, '', &blk + end + @routes ||= [] @used_ids = {} method = method.to_s.upcase action = Route.new @@ -162,37 +167,81 @@ klass.instance_variable_set iv, value.dup end end end + def self.process_reload request, l + if request.http_method == 'POST' and request.path =~ /\A\/reload:([\w-]+)\z/ + ty = $1 + files = request.param['files'] + case ty + when 'views-modified' + files.each do |f| + if l + l.info "modified: #{f}" + end + View.on_removed f + View.on_modified f + end + when 'views-removed' + files.each do |f| + if l + l.info "removed: #{f}" + end + View.on_removed f + end + when 'app-modified' + files.each do |f| + if l + l.info "modified: #{f}" + end + Reload.load_file f + end + else + return false + end + true + end + end + def self.dispatch request, instance, args if cookie_str = request.header._aref('Cookie') ParamHash.parse_cookie request.cookie, cookie_str end request.flash = Flash.new( request.session = Session.decode(request.cookie) ) + l = Nyara.logger + if instance - if l = Nyara.logger + if l l.info "#{request.http_method} #{request.path} => #{instance.class}" + if %W"POST PUT PATCH".include?(request.http_method) + l.info " params: #{instance.params.inspect}" + end end instance.send *args return elsif request.http_method == 'GET' and Config['public'] path = Config.public_path request.path if File.file?(path) - if l = Nyara.logger - l.info "GET #{path} => public 200" + if l + l.info "GET #{request.path} => public 200" end instance = Controller.new request instance.send_file path return end + elsif Config.development? + if process_reload(request, l) + Ext.request_send_data request, "HTTP/1.1 200 OK\r\n\r\n" + return + end end - if l = Nyara.logger + if l l.info "#{request.http_method} #{request.path} => 404" end Ext.request_send_data request, "HTTP/1.1 404 Not Found\r\nConnection: close\r\nContent-Length: 0\r\n\r\n" Fiber.yield :term_close @@ -205,10 +254,13 @@ if args.last.is_a?(Hash) opts = args.pop end template, meth = self.class.path_templates[id.to_s] + if template.blank? && meth.blank? + raise ArgumentError, "#{id} route not found." + end r = template % args if opts format = opts.delete :format r << ".#{format}" if format @@ -271,12 +323,15 @@ Fiber.yield :term_close end # Shortcut for `redirect url_to *xs` - def redirect_to *xs - redirect url_to(*xs) + def redirect_to identifier, *xs + if identifier !~ /\A\w*#\w++(?:\-\w++)*\z/ + raise ArgumentError, "not action identifier: #{identifier.inspect}, did you mean `redirect`?" + end + redirect url_to(identifier, *xs) end # Stop processing and close connection<br> # Calling `halt` closes the connection at once, you may usually need to set status code and send header before halt. # @@ -477,10 +532,12 @@ unless header['Content-Type'] unless content_type extname = File.extname(file) extname = File.extname(filename) if extname.blank? and filename + extname.gsub!(".","") + content_type = MIME_TYPES[extname] || 'application/octet-stream' end header['Content-Type'] = content_type end @@ -524,10 +581,10 @@ Fiber.yield :sleep # see event.c for the handler end # Render a template as string def partial view_path, locals: nil - view = View.new self, view_path, nil, nil, {} + view = View.new self, view_path, nil, locals, {} view.partial end # One shot render, and terminate the action. #