Set site/user fields in ActiveResource

淺唱寂寞╮ 提交于 2019-12-02 01:01:58

I've been playing a lot with setting site option dynamically during runtime and the only solution I have found which will not lead to race condition.

class Runner
  def self.new(site)
    Class.new(ActiveResource::Base) do
      self.site = site
      self.element_name = 'runner'

      # your methods here
    end.new
  end
end

While defining method with define_method you can specify its arguments passing them as arguments to the block and not to define_method itself. So you can define setter method like that:

define_method("#{attr}=") do |val|
  Thread.current["active_resource.#{attr}"] = val
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!