Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Reindent some chunks of code

  • Loading branch information...
commit 58457397fc874a296e35e3e59603b18439be6cb7 1 parent ccbf202
Patricio Mac Adden authored

Showing 1 changed file with 167 additions and 158 deletions. Show diff stats Hide diff stats

  1. +167 158 lib/sinatra/base.rb
325 lib/sinatra/base.rb
@@ -756,6 +756,7 @@ def find_template(views, name, engine)
756 756 end
757 757
758 758 private
  759 +
759 760 # logic shared between builder and nokogiri
760 761 def render_ruby(engine, template, options = {}, locals = {}, &block)
761 762 options, template = template, nil if template.is_a?(Hash)
@@ -929,189 +930,190 @@ def forward
929 930 nil
930 931 end
931 932
932   - private
933   - # Run filters defined on the class and all superclasses.
934   - def filter!(type, base = settings)
935   - filter! type, base.superclass if base.superclass.respond_to?(:filters)
936   - base.filters[type].each { |args| process_route(*args) }
937   - end
  933 + private
938 934
939   - # Run routes defined on the class and all superclasses.
940   - def route!(base = settings, pass_block = nil)
941   - if routes = base.routes[@request.request_method]
942   - routes.each do |pattern, keys, conditions, block|
943   - pass_block = process_route(pattern, keys, conditions) do |*args|
944   - route_eval { block[*args] }
  935 + # Run filters defined on the class and all superclasses.
  936 + def filter!(type, base = settings)
  937 + filter! type, base.superclass if base.superclass.respond_to?(:filters)
  938 + base.filters[type].each { |args| process_route(*args) }
  939 + end
  940 +
  941 + # Run routes defined on the class and all superclasses.
  942 + def route!(base = settings, pass_block = nil)
  943 + if routes = base.routes[@request.request_method]
  944 + routes.each do |pattern, keys, conditions, block|
  945 + pass_block = process_route(pattern, keys, conditions) do |*args|
  946 + route_eval { block[*args] }
  947 + end
945 948 end
946 949 end
947   - end
948 950
949   - # Run routes defined in superclass.
950   - if base.superclass.respond_to?(:routes)
951   - return route!(base.superclass, pass_block)
952   - end
  951 + # Run routes defined in superclass.
  952 + if base.superclass.respond_to?(:routes)
  953 + return route!(base.superclass, pass_block)
  954 + end
953 955
954   - route_eval(&pass_block) if pass_block
955   - route_missing
956   - end
  956 + route_eval(&pass_block) if pass_block
  957 + route_missing
  958 + end
957 959
958   - # Run a route block and throw :halt with the result.
959   - def route_eval
960   - throw :halt, yield
961   - end
  960 + # Run a route block and throw :halt with the result.
  961 + def route_eval
  962 + throw :halt, yield
  963 + end
962 964
963   - # If the current request matches pattern and conditions, fill params
964   - # with keys and call the given block.
965   - # Revert params afterwards.
966   - #
967   - # Returns pass block.
968   - def process_route(pattern, keys, conditions, block = nil, values = [])
969   - route = @request.path_info
970   - route = '/' if route.empty? and not settings.empty_path_info?
971   - return unless match = pattern.match(route)
972   - values += match.captures.to_a.map { |v| force_encoding URI.decode(v) if v }
  965 + # If the current request matches pattern and conditions, fill params
  966 + # with keys and call the given block.
  967 + # Revert params afterwards.
  968 + #
  969 + # Returns pass block.
  970 + def process_route(pattern, keys, conditions, block = nil, values = [])
  971 + route = @request.path_info
  972 + route = '/' if route.empty? and not settings.empty_path_info?
  973 + return unless match = pattern.match(route)
  974 + values += match.captures.to_a.map { |v| force_encoding URI.decode(v) if v }
973 975
974   - if values.any?
975   - original, @params = params, params.merge('splat' => [], 'captures' => values)
976   - keys.zip(values) { |k,v| Array === @params[k] ? @params[k] << v : @params[k] = v if v }
977   - end
  976 + if values.any?
  977 + original, @params = params, params.merge('splat' => [], 'captures' => values)
  978 + keys.zip(values) { |k,v| Array === @params[k] ? @params[k] << v : @params[k] = v if v }
  979 + end
978 980
979   - catch(:pass) do
980   - conditions.each { |c| throw :pass if c.bind(self).call == false }
981   - block ? block[self, values] : yield(self, values)
  981 + catch(:pass) do
  982 + conditions.each { |c| throw :pass if c.bind(self).call == false }
  983 + block ? block[self, values] : yield(self, values)
  984 + end
  985 + ensure
  986 + @params = original if original
982 987 end
983   - ensure
984   - @params = original if original
985   - end
986 988
987   - # No matching route was found or all routes passed. The default
988   - # implementation is to forward the request downstream when running
989   - # as middleware (@app is non-nil); when no downstream app is set, raise
990   - # a NotFound exception. Subclasses can override this method to perform
991   - # custom route miss logic.
992   - def route_missing
993   - if @app
994   - forward
995   - else
996   - raise NotFound
  989 + # No matching route was found or all routes passed. The default
  990 + # implementation is to forward the request downstream when running
  991 + # as middleware (@app is non-nil); when no downstream app is set, raise
  992 + # a NotFound exception. Subclasses can override this method to perform
  993 + # custom route miss logic.
  994 + def route_missing
  995 + if @app
  996 + forward
  997 + else
  998 + raise NotFound
  999 + end
997 1000 end
998   - end
999 1001
1000   - # Attempt to serve static files from public directory. Throws :halt when
1001   - # a matching file is found, returns nil otherwise.
1002   - def static!
1003   - return if (public_dir = settings.public_folder).nil?
1004   - public_dir = File.expand_path(public_dir)
  1002 + # Attempt to serve static files from public directory. Throws :halt when
  1003 + # a matching file is found, returns nil otherwise.
  1004 + def static!
  1005 + return if (public_dir = settings.public_folder).nil?
  1006 + public_dir = File.expand_path(public_dir)
1005 1007
1006   - path = File.expand_path(public_dir + unescape(request.path_info))
1007   - return unless path.start_with?(public_dir) and File.file?(path)
  1008 + path = File.expand_path(public_dir + unescape(request.path_info))
  1009 + return unless path.start_with?(public_dir) and File.file?(path)
1008 1010
1009   - env['sinatra.static_file'] = path
1010   - cache_control(*settings.static_cache_control) if settings.static_cache_control?
1011   - send_file path, :disposition => nil
1012   - end
1013   -
1014   - # Enable string or symbol key access to the nested params hash.
1015   - def indifferent_params(object)
1016   - case object
1017   - when Hash
1018   - new_hash = indifferent_hash
1019   - object.each { |key, value| new_hash[key] = indifferent_params(value) }
1020   - new_hash
1021   - when Array
1022   - object.map { |item| indifferent_params(item) }
1023   - else
1024   - object
  1011 + env['sinatra.static_file'] = path
  1012 + cache_control(*settings.static_cache_control) if settings.static_cache_control?
  1013 + send_file path, :disposition => nil
1025 1014 end
1026   - end
1027 1015
1028   - # Creates a Hash with indifferent access.
1029   - def indifferent_hash
1030   - Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
1031   - end
  1016 + # Enable string or symbol key access to the nested params hash.
  1017 + def indifferent_params(object)
  1018 + case object
  1019 + when Hash
  1020 + new_hash = indifferent_hash
  1021 + object.each { |key, value| new_hash[key] = indifferent_params(value) }
  1022 + new_hash
  1023 + when Array
  1024 + object.map { |item| indifferent_params(item) }
  1025 + else
  1026 + object
  1027 + end
  1028 + end
1032 1029
1033   - # Run the block with 'throw :halt' support and apply result to the response.
1034   - def invoke
1035   - res = catch(:halt) { yield }
1036   - res = [res] if Fixnum === res or String === res
1037   - if Array === res and Fixnum === res.first
1038   - res = res.dup
1039   - status(res.shift)
1040   - body(res.pop)
1041   - headers(*res)
1042   - elsif res.respond_to? :each
1043   - body res
1044   - end
1045   - nil # avoid double setting the same response tuple twice
1046   - end
  1030 + # Creates a Hash with indifferent access.
  1031 + def indifferent_hash
  1032 + Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
  1033 + end
1047 1034
1048   - # Dispatch a request with error handling.
1049   - def dispatch!
1050   - invoke do
1051   - static! if settings.static? && (request.get? || request.head?)
1052   - filter! :before
1053   - route!
  1035 + # Run the block with 'throw :halt' support and apply result to the response.
  1036 + def invoke
  1037 + res = catch(:halt) { yield }
  1038 + res = [res] if Fixnum === res or String === res
  1039 + if Array === res and Fixnum === res.first
  1040 + res = res.dup
  1041 + status(res.shift)
  1042 + body(res.pop)
  1043 + headers(*res)
  1044 + elsif res.respond_to? :each
  1045 + body res
  1046 + end
  1047 + nil # avoid double setting the same response tuple twice
1054 1048 end
1055   - rescue ::Exception => boom
1056   - invoke { handle_exception!(boom) }
1057   - ensure
1058   - begin
1059   - filter! :after unless env['sinatra.static_file']
  1049 +
  1050 + # Dispatch a request with error handling.
  1051 + def dispatch!
  1052 + invoke do
  1053 + static! if settings.static? && (request.get? || request.head?)
  1054 + filter! :before
  1055 + route!
  1056 + end
1060 1057 rescue ::Exception => boom
1061   - invoke { handle_exception!(boom) } unless @env['sinatra.error']
  1058 + invoke { handle_exception!(boom) }
  1059 + ensure
  1060 + begin
  1061 + filter! :after unless env['sinatra.static_file']
  1062 + rescue ::Exception => boom
  1063 + invoke { handle_exception!(boom) } unless @env['sinatra.error']
  1064 + end
1062 1065 end
1063   - end
1064 1066
1065   - # Error handling during requests.
1066   - def handle_exception!(boom)
1067   - @env['sinatra.error'] = boom
  1067 + # Error handling during requests.
  1068 + def handle_exception!(boom)
  1069 + @env['sinatra.error'] = boom
1068 1070
1069   - if boom.respond_to? :http_status
1070   - status(boom.http_status)
1071   - elsif settings.use_code? and boom.respond_to? :code and boom.code.between? 400, 599
1072   - status(boom.code)
1073   - else
1074   - status(500)
1075   - end
  1071 + if boom.respond_to? :http_status
  1072 + status(boom.http_status)
  1073 + elsif settings.use_code? and boom.respond_to? :code and boom.code.between? 400, 599
  1074 + status(boom.code)
  1075 + else
  1076 + status(500)
  1077 + end
1076 1078
1077   - status(500) unless status.between? 400, 599
  1079 + status(500) unless status.between? 400, 599
1078 1080
1079   - if server_error?
1080   - dump_errors! boom if settings.dump_errors?
1081   - raise boom if settings.show_exceptions? and settings.show_exceptions != :after_handler
1082   - end
  1081 + if server_error?
  1082 + dump_errors! boom if settings.dump_errors?
  1083 + raise boom if settings.show_exceptions? and settings.show_exceptions != :after_handler
  1084 + end
1083 1085
1084   - if not_found?
1085   - headers['X-Cascade'] = 'pass' if settings.x_cascade?
1086   - body '<h1>Not Found</h1>'
1087   - end
  1086 + if not_found?
  1087 + headers['X-Cascade'] = 'pass' if settings.x_cascade?
  1088 + body '<h1>Not Found</h1>'
  1089 + end
1088 1090
1089   - res = error_block!(boom.class, boom) || error_block!(status, boom)
1090   - return res if res or not server_error?
1091   - raise boom if settings.raise_errors? or settings.show_exceptions?
1092   - error_block! Exception, boom
1093   - end
  1091 + res = error_block!(boom.class, boom) || error_block!(status, boom)
  1092 + return res if res or not server_error?
  1093 + raise boom if settings.raise_errors? or settings.show_exceptions?
  1094 + error_block! Exception, boom
  1095 + end
1094 1096
1095   - # Find an custom error block for the key(s) specified.
1096   - def error_block!(key, *block_params)
1097   - base = settings
1098   - while base.respond_to?(:errors)
1099   - next base = base.superclass unless args_array = base.errors[key]
1100   - args_array.reverse_each do |args|
1101   - first = args == args_array.first
1102   - args += [block_params]
1103   - resp = process_route(*args)
1104   - return resp unless resp.nil? && !first
  1097 + # Find an custom error block for the key(s) specified.
  1098 + def error_block!(key, *block_params)
  1099 + base = settings
  1100 + while base.respond_to?(:errors)
  1101 + next base = base.superclass unless args_array = base.errors[key]
  1102 + args_array.reverse_each do |args|
  1103 + first = args == args_array.first
  1104 + args += [block_params]
  1105 + resp = process_route(*args)
  1106 + return resp unless resp.nil? && !first
  1107 + end
1105 1108 end
  1109 + return false unless key.respond_to? :superclass and key.superclass < Exception
  1110 + error_block!(key.superclass, *block_params)
1106 1111 end
1107   - return false unless key.respond_to? :superclass and key.superclass < Exception
1108   - error_block!(key.superclass, *block_params)
1109   - end
1110 1112
1111   - def dump_errors!(boom)
1112   - msg = ["#{boom.class} - #{boom.message}:", *boom.backtrace].join("\n\t")
1113   - @env['rack.errors'].puts(msg)
1114   - end
  1113 + def dump_errors!(boom)
  1114 + msg = ["#{boom.class} - #{boom.message}:", *boom.backtrace].join("\n\t")
  1115 + @env['rack.errors'].puts(msg)
  1116 + end
1115 1117
1116 1118 class << self
1117 1119 attr_reader :routes, :filters, :templates, :errors
@@ -1315,7 +1317,8 @@ def public_dir
1315 1317 public_folder
1316 1318 end
1317 1319
1318   - private
  1320 + private
  1321 +
1319 1322 # Dynamically defines a method on settings.
1320 1323 def define_singleton(name, content = Proc.new)
1321 1324 # replace with call to singleton_class once we're 1.9 only
@@ -1361,7 +1364,8 @@ def provides(*types)
1361 1364 end
1362 1365 end
1363 1366
1364   - public
  1367 + public
  1368 +
1365 1369 # Defining a `GET` handler also automatically defines
1366 1370 # a `HEAD` handler.
1367 1371 def get(path, opts = {}, &block)
@@ -1381,7 +1385,8 @@ def patch(path, opts = {}, &bk) route 'PATCH', path, opts, &bk end
1381 1385 def link(path, opts = {}, &bk) route 'LINK', path, opts, &bk end
1382 1386 def unlink(path, opts = {}, &bk) route 'UNLINK', path, opts, &bk end
1383 1387
1384   - private
  1388 + private
  1389 +
1385 1390 def route(verb, path, options = {}, &block)
1386 1391 # Because of self.options.host
1387 1392 host_name(options.delete(:host)) if options.key?(:host)
@@ -1517,7 +1522,8 @@ def safe_ignore(ignore)
1517 1522 end
1518 1523 end
1519 1524
1520   - public
  1525 + public
  1526 +
1521 1527 # Makes the methods defined in the block and in the Modules given
1522 1528 # in `extensions` available to the handlers and templates
1523 1529 def helpers(*extensions, &block)
@@ -1610,7 +1616,8 @@ def call(env)
1610 1616 synchronize { prototype.call(env) }
1611 1617 end
1612 1618
1613   - private
  1619 + private
  1620 +
1614 1621 def setup_default_middleware(builder)
1615 1622 builder.use ExtendedRack
1616 1623 builder.use ShowExceptions if show_exceptions?
@@ -1694,7 +1701,8 @@ def synchronize(&block)
1694 1701 end
1695 1702 end
1696 1703
1697   - public
  1704 + public
  1705 +
1698 1706 CALLERS_TO_IGNORE = [ # :nodoc:
1699 1707 /\/sinatra(\/(base|main|showexceptions))?\.rb$/, # all sinatra code
1700 1708 /lib\/tilt.*\.rb$/, # all tilt code
@@ -1724,7 +1732,8 @@ def caller_locations
1724 1732 cleaned_caller 2
1725 1733 end
1726 1734
1727   - private
  1735 + private
  1736 +
1728 1737 # used for deprecation warnings
1729 1738 def warn(message)
1730 1739 super message + "\n\tfrom #{cleaned_caller.first.join(':')}"

0 comments on commit 5845739

Please sign in to comment.
Something went wrong with that request. Please try again.