Ruby:根据用户输入动态定义类

发布于 2024-10-19 10:30:20 字数 942 浏览 1 评论 0原文

我正在 Ruby 中创建一个库,允许用户访问外部 API。可以通过 SOAP 或 REST API 访问该 API。我愿意支持两者。

我首先在不同的模块中定义必要的对象。例如:

soap_connecton = Library::Soap::Connection.new(username, password)
response = soap_connection.create Library::Soap::LibraryObject.new(type, data, etc)
puts response.class # Library::Soap::Response

rest_connecton = Library::Rest::Connection.new(username, password)
response = rest_connection.create Library::Rest::LibraryObject.new(type, data, etc)
puts response.class # Library::Rest::Response

我想做的是允许用户指定他们只希望使用其中一个 API,也许是这样的:

Library::Modes.set_mode(Library::Modes::Rest)
rest_connection = Library::Connection.new(username, password)
response = rest_connection.create Library::LibraryObject.new(type, data, etc)
puts response.class # Library::Response

但是,我还没有发现一种动态设置的方法,例如 Library::Connection 基于 Library::Modes.set_mode 的输入。实现此功能的最佳方法是什么?

I'm creating a library in Ruby that allows the user to access an external API. That API can be accessed via either a SOAP or a REST API. I would like to support both.

I've started by defining the necessary objects in different modules. For example:

soap_connecton = Library::Soap::Connection.new(username, password)
response = soap_connection.create Library::Soap::LibraryObject.new(type, data, etc)
puts response.class # Library::Soap::Response

rest_connecton = Library::Rest::Connection.new(username, password)
response = rest_connection.create Library::Rest::LibraryObject.new(type, data, etc)
puts response.class # Library::Rest::Response

What I would like to do is allow the user to specify that they only wish to use one of the APIs, perhaps something like this:

Library::Modes.set_mode(Library::Modes::Rest)
rest_connection = Library::Connection.new(username, password)
response = rest_connection.create Library::LibraryObject.new(type, data, etc)
puts response.class # Library::Response

However, I have not yet discovered a way to dynamically set, for example, Library::Connection based on the input to Library::Modes.set_mode. What would be the best way to implement this functionality?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吲‖鸣 2024-10-26 10:30:20

墨菲定律盛行;将问题发布到 Stack Overflow 后立即找到答案。

这段代码似乎对我有用:

module Library
  class Modes
    Rest = 1
    Soap = 2

    def self.set_mode(mode)
      case mode
      when Rest
        Library.const_set "Connection", Class.new(Library::Rest::Connection)
        Library.const_set "LibraryObject", Class.new(Library::Rest::LibraryObject)
      when Soap
        Library.const_set "Connection", Class.new(Library::Soap::Connection)
        Library.const_set "LibraryObject", Class.new(Library::Soap::LibraryObject)
      else
        throw "#{mode.to_s} is not a valid Library::Mode"
      end
    end
  end
end

快速测试:

Library::Modes.set_mode(Library::Modes::Rest)
puts Library::Connection.class == Library::Rest::Connection.class # true
c = Library::Connection.new(username, password)

Murphy's law prevails; find an answer right after posting the question to Stack Overflow.

This code seems to have worked for me:

module Library
  class Modes
    Rest = 1
    Soap = 2

    def self.set_mode(mode)
      case mode
      when Rest
        Library.const_set "Connection", Class.new(Library::Rest::Connection)
        Library.const_set "LibraryObject", Class.new(Library::Rest::LibraryObject)
      when Soap
        Library.const_set "Connection", Class.new(Library::Soap::Connection)
        Library.const_set "LibraryObject", Class.new(Library::Soap::LibraryObject)
      else
        throw "#{mode.to_s} is not a valid Library::Mode"
      end
    end
  end
end

A quick test:

Library::Modes.set_mode(Library::Modes::Rest)
puts Library::Connection.class == Library::Rest::Connection.class # true
c = Library::Connection.new(username, password)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文