Ruby,Sinatra,Overriding /从单独的文件扩展类以添加方法

时间:2022-08-03 23:20:22

I'm new to Ruby, I can find answers for various variations on this question but not an answer to my problem itself.

我是Ruby的新手,我可以找到关于这个问题的各种变化的答案,但不是我问题本身的答案。

I am writing an application which takes basic auth details and uses them to authenticate the user with a 3rd party platform as such I need to be able to access the password in order to pass it on but I also want to be able to use the Rack Authentication classes

我正在编写一个应用程序,它接受基本的auth详细信息并使用它们来验证用户使用第三方平台,因此我需要能够访问密码才能将其传递但我还希望能够使用Rack身份验证类

All I want to do is this:

我想做的就是:

require 'rack/auth/abstract/request'
require 'rack/auth/basic'

class AuthRequest < Rack::Auth::Basic::Request < Rack::Auth::AbstractRequest
    def password
      credentials[1]
    end
end

If I use Request < Auth::AbstractRequest the IDE tells me I have undeclare constant, but if I fully qualify it as above it says 'wrong argument type true (expected Class) (TypeError)'

如果我使用Request

I read various articles including this but I'm no less confused, I think the problem stems from the fact I'm trying to extend a subclass from this external file, not it's root class.

我阅读了各种各样的文章,包括这个,但我并没有那么困惑,我认为问题源于我试图从这个外部文件扩展子类的事实,而不是它的根类。

What's the right way to do this?

这样做的正确方法是什么?

Edit - some more information on my requirement: I'm basing this need on this sample from the Sinatra documentation. I want the @auth object for the niceness like provided? and basic? but as I can't simply do a comparison like @auth.credentials == ['admin', 'admin'], I need access to the password, which is not granted in the default implementation

编辑 - 有关我的要求的更多信息:我基于Sinatra文档中的此示例。我想要@auth对象提供的好处?和基本的?但由于我不能像@ auth.credentials == ['admin','admin']那样进行比较,我需要访问密码,而密码在默认实现中未授予

helpers do 

  ...

  def authorized?
    @auth ||=  Rack::Auth::Basic::Request.new(request.env)
    @auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == ['admin', 'admin']
  end

end

1 个解决方案

#1


0  

I don't really understand what you expect the expression class AuthRequest < Rack::Auth::Basic::Request < Rack::Auth::AbstractRequest to behave, maybe some kind of multiple inheritance?

我真的不明白你期望表达式类AuthRequest

The problem here is that, being Rack::Auth::Basic::Request a subclass of Rack::Auth::AbstractRequest, the expression Rack::Auth::Basic::Request < Rack::Auth::AbstractRequest evaluates to true (see Module#<), thus your expression is actually interpreted as class AuthRequest < true, that makes no sense, hence the TypeError.

这里的问题是,Rack :: Auth :: Basic :: Request是Rack :: Auth :: AbstractRequest的子类,表达式Rack :: Auth :: Basic :: Request

If I got right what you are trying to achieve the following code should do:

如果我做对了你想要实现的东西,下面的代码应该做:

class AuthRequest < Rack::Auth::Basic::Request
  def password
    credentials[1]
  end
end

#1


0  

I don't really understand what you expect the expression class AuthRequest < Rack::Auth::Basic::Request < Rack::Auth::AbstractRequest to behave, maybe some kind of multiple inheritance?

我真的不明白你期望表达式类AuthRequest

The problem here is that, being Rack::Auth::Basic::Request a subclass of Rack::Auth::AbstractRequest, the expression Rack::Auth::Basic::Request < Rack::Auth::AbstractRequest evaluates to true (see Module#<), thus your expression is actually interpreted as class AuthRequest < true, that makes no sense, hence the TypeError.

这里的问题是,Rack :: Auth :: Basic :: Request是Rack :: Auth :: AbstractRequest的子类,表达式Rack :: Auth :: Basic :: Request

If I got right what you are trying to achieve the following code should do:

如果我做对了你想要实现的东西,下面的代码应该做:

class AuthRequest < Rack::Auth::Basic::Request
  def password
    credentials[1]
  end
end