Module: Roda::RodaPlugins::ClassLevelRouting::InstanceMethods

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

Instance Method Summary collapse

Instance Method Details

#callObject

If the normal routing tree doesn’t handle an action, try each class level route to see if it matches.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/roda/plugins/class_level_routing.rb', line 81

def call
  req = @_request
  rp = req.remaining_path
  result = super

  if result[0] == 404 && (v = result[2]).is_a?(Array) && v.empty?
    # Reset the response so it doesn't inherit the status or any headers from
    # the original response.
    @_response = self.class::RodaResponse.new
    super do |r|
      opts[:class_level_routes].each do |meth, args, blk|
        req.instance_variable_set(:@remaining_path, rp)
        r.send(meth, *args) do |*a|
          instance_exec(*a, &blk)
        end
      end
    end
  else
    result
  end
end