检查可能缺席的包含类方法

时间:2015-10-06 12:23:55

标签: ruby

我有一个案例,我有一个基于该类是否包含模块的类可用的类方法。例如:

class HomePage
  include Symbiont

  url_matches 'test'
end

class WeightPage
  include Symbiont
end

这里包含Symbiont时,它提供了一个名为url_matches的类方法。但请注意,您不必使用它,如WeightPage所示。因此,虽然Symbiont使该方法可用,但包含Symbiont的类不必使用它。

以下是问题: 我想要做的是检查班级是否实际上正在使用它。

但我无法弄清楚如何。所以,让我们这样说:

@home = HomePage.new
@weight = WeightPage.new

我希望能够做到这样的事情:

@home.respond_to?(:url_matches)    # should return true
@weight.respond_to?(:url_matches)  # should return false

这不起作用。两者都返回假。然后我在课堂上尝试了它:

@home.class.respond_to?("url_matches")
@weight.class.respond_to?("url_matches")

然而,总是会返回true。

我试过了:

@home.class.public_instance_methods.include?(:url_matches)
@weight.class.public_instance_methods.include?(:url_matches)

始终返回false。

@home.class.method_defined?(:url_matches)
@weight.class.method_defined?(:url_matches)

始终返回false。

此时,我不知道该怎么做。

基础代码背景

出于这个问题的目的,上下文中的代码可能有点难以理解,但只是提供它:

这是" Symbiont"包含的模块:

https://github.com/jnyman/symbiont/blob/master/lib/symbiont.rb

特别注意这一部分:

module Symbiont
  def self.included(caller)
    caller.extend Symbiont::Assertion

Symbiont :: Assertion就在这里:

https://github.com/jnyman/symbiont/blob/master/lib/symbiont/assertions.rb

这就是url_matches的来源。因此,当Symbiont包含在类中时,如前所示,这是允许在类上声明url_matches的原因。

3 个答案:

答案 0 :(得分:1)

只需将url_matches添加到这两个类中,即可将Symbiont方法添加到这两个类中。

那说:如果该类响应该方法,但是如果该方法被调用,则您不感兴趣。如果您查看implementation of that method,您会看到该方法设置了@url_match变量。因此,您的问题应该是:@url_match变量是否已设置?

这可以通过使用instance_variable_get

来实现
@home.class.instance_variable_get(:@url_match)
@weight.class.instance_variable_get(:@url_match)

答案 1 :(得分:0)

怎么样?
expect(HomePage).to respond_to(:url_matches)

答案 2 :(得分:0)

我的2美分:我想我会修补backend fail_be { .host = "127.0.0.1"; .port = "9000"; .probe = { .url = "/give-me-a-non-200-please"; .interval = 24h; .timeout = 1s; .window = 1; .threshold = 1; } } sub vcl_recv { # Force the non-healthy backend in case of restart because of a previous # failed backend fetch. This will force serving stalled content using # full grace during 'vcl_hit' (if possible). if (req.restarts == 0) { unset req.http.X-Varnish-Restarted-5xx; } else { if (req.http.X-Varnish-Restarted-5xx) { set req.backend_hint = fail_be; } } # ... } sub vcl_synth { # 503 generated for synchronous client requests when abandoning the # backend request (see 'vcl_backend_fetch') and not executing a POST. if (resp.status == 503 && req.method != "POST" && !req.http.X-Varnish-Restarted-5xx) { set req.http.X-Varnish-Restarted-5xx = "1"; return (restart); } # ... } sub vcl_backend_fetch { if (bereq.retries == 0) { unset bereq.http.X-Varnish-Backend-5xx; } else { if (bereq.http.X-Varnish-Backend-5xx) { # Jump to 'vcl_synth' with a 503 status code. return (abandon); } } # ... } sub vcl_backend_response { if (beresp.status >= 500 && beresp.status < 600) { set bereq.http.X-Varnish-Backend-5xx = "1"; return (retry); } # ... } sub vcl_backend_error { set bereq.http.X-Varnish-Backend-5xx = "1"; return (retry); } 方法,以便在调用时将类变量url_matches设置为@@symbiont_enabled,然后我会添加另一个类方法,看起来像这样:

true
相关问题