Module: Roda::RodaPlugins::NotAllowed::RequestClassMethods

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

Instance Method Summary collapse

Instance Method Details

#def_verb_method(mod, verb) ⇒ Object

Define a method named verb in the given module which will return a 405 response if the method is called with any arguments and the arguments terminally match but the request method does not.

If called without any arguments, check to see if the call is inside a terminal match, and in that case record the request method used.



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

def def_verb_method(mod, verb)
  mod.class_eval(<<-END, __FILE__, __LINE__+1)
    def #{verb}(*args, &block)
      if args.empty?
        @_is_verbs << "#{verb.to_s.upcase}" if @_is_verbs
        always(&block) if #{verb == :get ? :is_get : verb}?
      else
        args << ::Roda::RodaPlugins::Base::RequestMethods::TERM
        if_match(args) do |*args|
          if #{verb == :get ? :is_get : verb}?
            block_result(yield(*args))
            throw :halt, response.finish
          end
          response.status = 405
          response['Allow'] = '#{verb.to_s.upcase}'
          nil
        end
      end
    end
  END
end