Ruby 在ActiveResource中设置站点/用户字段

Ruby 在ActiveResource中设置站点/用户字段,ruby,activeresource,highrise,Ruby,Activeresource,Highrise,我正在构建一个sinatra应用程序,它将使用Highrise CRM gem访问Highrise数据。此gem基于ActiveResource类。我想为每个请求设置站点、用户字段。我遵循了这里发布的建议-。我添加了代码(如下所示),然后得到一个错误。任何人都可以帮助理解此错误以及如何修复它 class ActiveResource::Base class << self %w(site user).each do |attr|

我正在构建一个sinatra应用程序,它将使用Highrise CRM gem访问Highrise数据。此gem基于ActiveResource类。我想为每个请求设置站点、用户字段。我遵循了这里发布的建议-。我添加了代码(如下所示),然后得到一个错误。任何人都可以帮助理解此错误以及如何修复它

class ActiveResource::Base
  class << self
    %w(site user).each do |attr|               

      define_method(attr) do
        Thread.current["active_resource.#{attr}"]
      end

      define_method("#{attr}=", val) do
        Thread.current["active_resource.#{attr}"] = val
      end
    end
  end
end
class-ActiveResource::Base

类在使用
define_方法
定义方法时,可以指定其参数,将其作为参数传递给块,而不是
define_方法
本身。所以您可以这样定义setter方法:

define_method("#{attr}=") do |val|
  Thread.current["active_resource.#{attr}"] = val
end

我一直在玩很多游戏,在运行时动态设置
site
选项,这是我找到的唯一不会导致竞争条件的解决方案

类运行程序
def自我更新(站点)
new(ActiveResource::Base)do
self.site=site
self.element_name='runner'
#你的方法在这里
完.新
结束
结束

我尝试了你所说的,但我得到了一个错误-“NoMethodError-未定义的方法'path':“我正在尝试为此匿名类中的对象设置窗体,但匿名类不使用ActiveModel,也不使用窗体。我得到:
类名不能为空。给定匿名类时,需要提供名称参数
。你找到解决这个问题的方法了吗?谢谢。这不是我的用例,我只从内部代码中使用它来获取和发布自定义数据,我甚至不遵循REST规范,所以我不知道它在standart工作流中如何工作。但我也不喜欢这种变通方法。
NoMethodError - undefined method `path' for "https://test.abcd.com":String:
  c:/Ruby192/lib/ruby/gems/1.9.1/gems/activeresource3.0.11/lib/active_resource/base.rb:562:in `prefix'
  c:/Ruby192/lib/ruby/gems/1.9.1/gems/activeresource3.0.11/lib/active_resource/base.rb:667:in `collection_path'
  c:/Ruby192/lib/ruby/gems/1.9.1/gems/activeresource3.0.11/lib/active_resource/base.rb:856:in `find_every' 
  c:/Ruby192/lib/ruby/gems/1.9.1/gems/activeresource-3.0.11/lib/active_resource/base.rb:777:in `find' application.rb:78:in `block in <main>'
  c:/Ruby192/lib/ruby/gems/1.9.1/gems/sinatra-1.3.1/lib/sinatra/base.rb:1212:in `call'
  c:/Ruby192/lib/ruby/gems/1.9.1/gems/sinatra-1.3.1/lib/sinatra/base.rb:1212:in `block in compile!'
  c:/Ruby192/lib/ruby/gems/1.9.1/gems/sinatra-1.3.1/lib/sinatra/base.rb:772:in `[]'
  c:/Ruby192/lib/ruby/gems/1.9.1/gems/sinatra-1.3.1/lib/sinatra/base.rb:772:in `block (3 levels) in route!'
  c:/Ruby192/lib/ruby/gems/1.9.1/gems/sinatra-1.3.1/lib/sinatra/base.rb:788:in `route_eval'
  c:/Ruby192/lib/ruby/gems/1.9.1/gems/sinatra-1.3.1/lib/sinatra/base.rb:772:in `block (2 levels) in route!'
  c:/Ruby192/lib/ruby/gems/1.9.1/gems/sinatra-1.3.1/lib/sinatra/base.rb:821:in `block in process_route'
define_method("#{attr}=") do |val|
  Thread.current["active_resource.#{attr}"] = val
end