在 ActiveResource 中设置站点/用户字段 [英] Set site/user fields in ActiveResource

查看:40
本文介绍了在 ActiveResource 中设置站点/用户字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个将使用 Highrise CRM gem 访问 Highrise 数据的 sinatra 应用程序.这个 gem 基于 ActiveResource 类.我想为每个请求设置站点、用户字段.我遵循了这里发布的建议 - 在每个用户的基础上设置 Active Resource HTTP 身份验证是否线程安全?.我添加了代码(如下所示),但出现错误.任何人都可以帮助理解此错误以及如何修复它.

I am building a sinatra app that will use Highrise CRM gem to access Highrise data. This gem is based on ActiveResource class. I want to set site, user fields for every request. I followed suggestion posted here - Is it thread safe to set Active Resource HTTP authentication on a per-user basis?. I add code (shown below) and I get an error. Can anyone help understand this error and how to fix it.

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

和错误:

c:/dev/hgadget/application.rb:18:in `block in singletonclass':
undefined local variable or method `val' for #<Class:ActiveResource::Base> (NameError)
    from c:/dev/hgadget/application.rb:12:in `each'
    from c:/dev/hgadget/application.rb:12:in `singletonclass'
    from c:/dev/hgadget/application.rb:11:in `<class:Base>'
    from c:/dev/hgadget/application.rb:9:in `<top (required)>'
    from <internal:lib/rubygems/custom_require>:29:in `require'
    from <internal:lib/rubygems/custom_require>:29:in `require'
    from application_test.rb:1:in `<main>'

------------------------更新-----------------------------

------------------------update-----------------------------

我尝试了您的建议,现在出现此错误.

I tried your suggestion, I now get this error.

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'

推荐答案

我一直在玩很多在运行时动态设置 site 选项的方法,也是我发现的唯一不会导致竞争条件.

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

这篇关于在 ActiveResource 中设置站点/用户字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆