How does Sinatra define and invoke the get method?

最后都变了- 提交于 2019-12-01 16:46:55

It is spread out in a few places, but if you look in lib/sinatra/main.rb, you can see this line at the bottom: include Sinatra::Delegator

If we go into lib/sinatra/base.rb we see this chunk of code around like 1470.

  # Sinatra delegation mixin. Mixing this module into an object causes all
  # methods to be delegated to the Sinatra::Application class. Used primarily
  # at the top-level.
  module Delegator #:nodoc:
    def self.delegate(*methods)
      methods.each do |method_name|
        define_method(method_name) do |*args, &block|
          return super(*args, &block) if respond_to? method_name
          Delegator.target.send(method_name, *args, &block)
        end
        private method_name
      end
    end

    delegate :get, :patch, :put, :post, :delete, :head, :options, :template, :layout,
             :before, :after, :error, :not_found, :configure, :set, :mime_type,
             :enable, :disable, :use, :development?, :test?, :production?,
             :helpers, :settings
    class << self
      attr_accessor :target
    end

    self.target = Application
  end

This code does what the comment says: if it is included, it delegates all calls to the list of delegated methods to Sinatra::Application class, which is a subclass of Sinatra::Base, which is where the get method is defined. When you write something like this:

require "sinatra"

get "foo" do
  "Hello World"
end

Sinatra will end up calling the get method on Sinatra::Base due to the delegation it set up earlier.

I haven't looked at the source of Sinatra, but the gist of it should be something like

>> module Test
..   extend self
..   class << self
..     def get; "hi";end
..     end
..   end #=> nil
>> include Test #=> Object
>> get #=> "hi"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!