Module: Roda::RodaPlugins::OptimizedMatching::RequestMethods

Defined in:
lib/roda/plugins/_optimized_matching.rb

Instance Method Summary collapse

Instance Method Details

#is(*args, &block) ⇒ Object

Optimize the r.is method handling of a single string, String, Integer, regexp, or true, argument.



22
23
24
25
26
27
28
29
30
31
# File 'lib/roda/plugins/_optimized_matching.rb', line 22

def is(*args, &block)
  case args.length
  when 1
    _is1(args, &block)
  when 0
    always(&block) if @remaining_path.empty?
  else
    if_match(args << TERM, &block)
  end
end

#on(*args, &block) ⇒ Object

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.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/roda/plugins/_optimized_matching.rb', line 36

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{1,100})(?=\/|\z)/.match(@remaining_path)) && (value = _match_class_convert_Integer(matchdata[1]))
          @remaining_path = matchdata.post_match
          always{yield(value)}
        end
      else
        path = @remaining_path
        captures = @captures.clear
        meth = :"_match_class_#{matcher}"
        if respond_to?(meth, true)
          # Allow calling private methods, as match methods are generally private
          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
      # nothing
    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