lib/roda/plugins/_optimized_matching.rb
# Optimize the r.on method handling of a single string, String, Integer,
# or regexp argument.  Inline the related matching code to avoid the
# need to modify @captures.
Optimize the r.on method handling of a single string, String, Integer,
or regexp argument.  Inline the related matching code to avoid the
need to modify @captures.
Optimize the r . on method handling of a single string String Integer or regexp argument . Inline the related matching code to avoid the need to modify @captures .
def on(*args, &block)
          case args.length
          when 1
            case matcher = args[0]
            when String
              always{yield} if _match_string(matcher)
            when Class
              if matcher == String
                rp = @remaining_path
                if rp.getbyte(0) == 47
                  if last = rp.index('/', 1)
                    @remaining_path = rp[last, rp.length]
                    always{yield rp[1, last-1]}
                  elsif (len = rp.length) > 1
                    @remaining_path = ""
                    always{yield rp[1, len]}
                  end
                end
              elsif matcher == Integer
                if matchdata = /\A\/(\d+)(?=\/|\z)/.match(@remaining_path)
                  @remaining_path = matchdata.post_match
                  always{yield(matchdata[1].to_i)}
                end
              else
                path = @remaining_path
                captures = @captures.clear
                meth = :"_match_class_#{matcher}"
                if respond_to?(meth, true)
                  if send(meth, &block)
                    block_result(yield(*captures))
                    throw :halt, response.finish
                  else
                    @remaining_path = path
                    false
                  end
                else
                  unsupported_matcher(matcher)
                end
              end
            when Regexp
              if matchdata = self.class.cached_matcher(matcher){matcher}.match(@remaining_path)
                @remaining_path = matchdata.post_match
                always{yield(*matchdata.captures)}
              end
            when true
              always(&block)
            when false, nil
            else
              path = @remaining_path
              captures = @captures.clear
              matched = case matcher
              when Array
                _match_array(matcher)
              when Hash
                _match_hash(matcher)
              when Symbol
                _match_symbol(matcher)
              when Proc
                matcher.call
              else
                unsupported_matcher(matcher)
              end
              if matched
                block_result(yield(*captures))
                throw :halt, response.finish
              else
                @remaining_path = path
                false
              end
            end
          when 0
            always(&block)
          else
            if_match(args, &block)
          end
        end
def on ( * args , & block ) case args . length when 1 case matcher = args [ 0 ] when String always { yield } if _match_string ( matcher ) when Class if matcher == String rp = @remaining_path if rp . getbyte ( 0 ) == 47 if last = rp . index ( '/' , 1 ) @remaining_path = rp [ last , rp . length ] always { yield rp [ 1 , last - 1 ] } elsif ( len = rp . length ) > 1 @remaining_path = "" always { yield rp [ 1 , len ] } end end elsif matcher == Integer if matchdata = / \A \/ ( \d +)(?= \/ | \z ) / . match ( @remaining_path ) @remaining_path = matchdata . post_match always { yield ( matchdata [ 1 ] . to_i ) } end else path = @remaining_path captures = @captures . clear meth = :" _match_class_ #{ matcher } " if respond_to? ( meth , true ) if send ( meth , & block ) block_result ( yield ( * captures ) ) throw :halt , response . finish else @remaining_path = path false end else unsupported_matcher ( matcher ) end end when Regexp if matchdata = self . class . cached_matcher ( matcher ) { matcher } . match ( @remaining_path ) @remaining_path = matchdata . post_match always { yield ( * matchdata . captures ) } end when true always ( & block ) when false , nil else path = @remaining_path captures = @captures . clear matched = case matcher when Array _match_array ( matcher ) when Hash _match_hash ( matcher ) when Symbol _match_symbol ( matcher ) when Proc matcher . call else unsupported_matcher ( matcher ) end if matched block_result ( yield ( * captures ) ) throw :halt , response . finish else @remaining_path = path false end end when 0 always ( & block ) else if_match ( args , & block ) end end
Optimize the r.on method handling of a single string, String, Integer,
or regexp argument.
Optimize the r . on method handling of a single string String Integer or regexp argument .
# Allow calling private methods, as match methods are generally private # nothing
args
block