Changeset 223
- Timestamp:
- 04/27/07 22:19:57 (2 years ago)
- Files:
-
- trunk/lib/merb.rb (modified) (1 diff)
- trunk/lib/merb/core_ext.rb (modified) (2 diffs)
- trunk/lib/merb/core_ext/merb_class.rb (modified) (2 diffs)
- trunk/lib/merb/core_ext/merb_inflections.rb (added)
- trunk/lib/merb/core_ext/merb_inflector.rb (added)
- trunk/lib/merb/core_ext/merb_string.rb (modified) (1 diff)
- trunk/lib/merb/merb_controller.rb (modified) (2 diffs)
- trunk/lib/merb/merb_dispatcher.rb (modified) (3 diffs)
- trunk/lib/merb/merb_router.rb (modified) (5 diffs)
- trunk/specs/merb/merb_dispatch_spec.rb (modified) (1 diff)
- trunk/specs/merb/merb_router_spec.rb (deleted)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/lib/merb.rb
r208 r223 70 70 end 71 71 72 72 module Mongrel::Const 73 HTTP_COOKIE = 'HTTP_COOKIE'.freeze 74 QUERY_STRING = 'QUERY_STRING'.freeze 75 CONTENT_TYPE_TEXT_HTML_HASH = {'Content-Type' =>'text/html'} 76 APPLICATION_JSON = 'application/json'.freeze 77 TEXT_JSON = 'text/x-json'.freeze 78 UPCASE_CONTENT_TYPE = 'CONTENT_TYPE'.freeze 79 end 73 80 74 81 lib = File.join(__DIR__, 'merb') trunk/lib/merb/core_ext.rb
r193 r223 1 1 corelib = __DIR__+'/merb/core_ext' 2 2 3 %w[ merb_class 3 %w[ merb_inflector 4 merb_class 4 5 merb_kernel 5 6 merb_object … … 10 11 merb_numeric 11 12 merb_symbol 13 12 14 ].each {|fn| require File.join(corelib, fn)} trunk/lib/merb/core_ext/merb_class.rb
r127 r223 76 76 77 77 def shared_attributes 78 @shared_attributes ||= {}78 @shared_attributes ||= EMPTY_INHERITABLE_ATTRIBUTES 79 79 end 80 80 81 81 def write_shared_attribute(key, value) 82 if shared_attributes.equal?(EMPTY_INHERITABLE_ATTRIBUTES) 83 @shared_attributes = {} 84 end 82 85 shared_attributes[key] = value 83 86 end … … 88 91 89 92 def reset_shared_attributes 90 shared_attributes .clear93 shared_attributes = EMPTY_INHERITABLE_ATTRIBUTES 91 94 end 92 95 93 96 private 97 98 # Prevent this constant from being created multiple times 99 EMPTY_INHERITABLE_ATTRIBUTES = {}.freeze unless const_defined?(:EMPTY_INHERITABLE_ATTRIBUTES) 100 94 101 def inherited_with_shared_attributes(child) 95 102 inherited_without_shared_attributes(child) if respond_to?(:inherited_without_shared_attributes) 96 97 new_shared_attributes = shared_attributes.inject({}) do |memo, (key, value)| 98 memo.update(key => (value.dup rescue value)) 103 104 if shared_attributes.equal?(EMPTY_INHERITABLE_ATTRIBUTES) 105 new_shared_attributes = EMPTY_INHERITABLE_ATTRIBUTES 106 else 107 new_shared_attributes = shared_attributes.inject({}) do |memo, (key, value)| 108 memo.update(key => (value.dup rescue value)) 109 end 99 110 end 100 111 trunk/lib/merb/core_ext/merb_string.rb
r125 r223 1 1 class String 2 3 # reloads controller classes on each request if4 # :allow_reloading is set to true in the config5 # file or command line options.6 def import7 if Merb::Server.allow_reloading8 Object.send(:remove_const, self.camel_case.intern) rescue nil9 load(self.snake_case + '.rb')10 else11 require(self.snake_case)12 end13 end14 2 15 3 # "FooBar".snake_case #=> "foo_bar" trunk/lib/merb/merb_controller.rb
r216 r223 24 24 25 25 MULTIPART_REGEXP = /\Amultipart\/form-data.*boundary=\"?([^\";,]+)/n.freeze 26 26 27 27 # parses the http request into params, headers and cookies 28 28 # that you can use in your controller classes. Also handles … … 31 31 def initialize(request, env, args, response) 32 32 @env = MerbHash[env.to_hash] 33 @status, @method, @response, @headers = 200, (env[ 'REQUEST_METHOD']||'GET').downcase.to_sym, response,34 {'Content-Type' =>'text/html'}35 cookies = query_parse(@env[ 'HTTP_COOKIE'], ';,')36 querystring = query_parse(@env[ 'QUERY_STRING'])37 38 if MULTIPART_REGEXP =~ @env[ 'CONTENT_TYPE'] && @method == :post33 @status, @method, @response, @headers = 200, (env[Mongrel::Const::REQUEST_METHOD]||Mongrel::Const::GET).downcase.to_sym, response, 34 Mongrel::Const::CONTENT_TYPE_TEXT_HTML_HASH 35 cookies = query_parse(@env[Mongrel::Const::HTTP_COOKIE], ';,') 36 querystring = query_parse(@env[Mongrel::Const::QUERY_STRING]) 37 38 if MULTIPART_REGEXP =~ @env[Mongrel::Const::UPCASE_CONTENT_TYPE] && @method == :post 39 39 querystring.update(parse_multipart(request, $1)) 40 40 elsif @method == :post 41 if [ 'application/json', 'text/x-json'].include?(@env['CONTENT_TYPE'])41 if [Mongrel::Const::APPLICATION_JSON, Mongrel::Const::TEXT_JSON].include?(@env[Mongrel::Const::UPCASE_CONTENT_TYPE]) 42 42 MERB_LOGGER.info("JSON Request") 43 43 json = JSON.parse(request.read || "") || {} trunk/lib/merb/merb_dispatcher.rb
r210 r223 50 50 path = path.sub(/\/+/, '/').sub(/\?.*$/, '') 51 51 path = path[0..-2] if (path[-1] == ?/) && path.size > 1 52 Merb::RouteMatcher. new.route_request(path)52 Merb::RouteMatcher.route_request(path) 53 53 end 54 54 … … 62 62 end unless $TESTING 63 63 begin 64 controller_name.import 64 unless MERB_ENV == 'production' 65 Object.send(:remove_const, controller_name.camel_case.intern) rescue nil 66 load(controller_name.snake_case + '.rb') 67 end 65 68 return Object.const_get( controller_name.camel_case ).new(req, env, params, res) 66 69 rescue RuntimeError … … 69 72 end 70 73 end 71 74 72 75 end # end class << self 73 76 trunk/lib/merb/merb_router.rb
r216 r223 1 1 module Merb 2 begin 3 require 'active_support' 4 rescue 5 MERB_LOGGER.warn "You must have ActiveSupport installed to use merb restful routing\nNormal routing works fine without" 6 end 2 7 3 # Merb::RouteMatcher is the request routing mapper for the merb framework. 8 4 # You can define placeholder parts of the url with the :symbol notation. … … 29 25 compile_router 30 26 end 31 32 # init @sections for route segment recognition 33 def initialize 34 @sections = Hash.new 35 end 36 37 # all defined routes in their raw form. 38 def routes 39 @@routes 40 end 41 27 42 28 # the final compiled lambda that gets used 43 29 # as the body of the route_request method. … … 64 50 # first route that matches wins. 65 51 def self.compile_router 66 router_lambda = @@routes.inject("lambda{|path| \n case path\n") { |m,r|52 router_lambda = @@routes.inject("lambda{|path| \n sections={}\n case path\n") { |m,r| 67 53 m << compile(r) 68 54 } <<" else\n return {:controller=>'Noroutefound', :action=>'noroute'}\n end\n}" 69 55 @@compiled_statement = router_lambda 70 define_method(:route_request, &eval(router_lambda))56 meta_def(:route_request, &eval(router_lambda)) 71 57 end 72 58 … … 89 75 route[0].sub!(@@section_regexp, "([^\/,?]+)") 90 76 end 91 code << " @sections[:#{name}] = $#{count}\n"77 code << " sections[:#{name}] = $#{count}\n" 92 78 end 93 79 @@compiled_regexen << Regexp.new(route[0]) … … 95 81 condition = " when @@compiled_regexen[#{index}] " 96 82 statement = "#{condition}\n#{code}" 97 statement << " return #{route[1].inspect}.update( @sections)\n"83 statement << " return #{route[1].inspect}.update(sections)\n" 98 84 statement 99 85 end trunk/specs/merb/merb_dispatch_spec.rb
r207 r223 220 220 end 221 221 222 specify "compile_statement should contain router lambda" do 223 Merb::RouteMatcher.compiled_statement.should_be.kind_of?(String) 224 Merb::RouteMatcher.compiled_statement.should =~ /lambda\{\|path/ 225 Merb::RouteMatcher.compiled_statement.should =~ /@@compiled_regexen/ 226 Merb::RouteMatcher.compiled_statement.should =~ /update\(sections\)/ 227 end 228 222 229 specify "should handle request: GET /foo/bar and return Foo#bar" do 223 230 controller, action = request(:get, '/foo/bar')
