我有一个想法,为我的副项目写一个小的redis包装器。 到目前为止,一切看起来都很棒。 现在,我正在尝试对我的配置进行润色,我对airbrake项目的配置块代码有点启发,reference article here
从一开始,我立即开始编写包装类,我设法及时完成。到目前为止,我需要做的是能够在irb或pry中测试我的配置代码块(config / initializers / redis_cli.rb)。
我不确定为什么我的初始化代码(位于rails项目配置/初始化器下)在这样的调试控制台环境中不起作用。
我在控制台中将我的RedisCli类实例化为=>
RedisCli.new
我说错了
“NoMethodError:未定义的方法host' for nil:NilClass
from /Users/admin/rails_projects/redis_app/lib/redis_cli.rb:44:in
redis_wrapper_options'”
相比之下,它在内部Web服务器,Web砖上运行时效果很好。 看起来我的RedisCli初始化程序没有在irb中正确加载。
这是我的代码:
LIB / redis_cli.rb
class RedisCli
attr_accessor :host,:port,:db,:url,:queue
class << self
attr_accessor :configuration
end
def self.configure
self.configuration ||= Configuration.new
yield(configuration)
end
class Configuration
attr_accessor :host,:port,:db,:queue
def initialize
end
end
def initialize(options={})
options = redis_wrapper_options(options)
@host = options[:host]
@port = options[:port]
@db = options[:db]
@queue = options[:queue]
end
def url
@url ||= [@host,@port].reject(&:blank?).join(":") if @url.blank?
@url
end
def lpush(klass,args)
redis = Redis.new(:url => url)
msg = { 'class' => klass,'args' => [args],'jid' => SecureRandom.hex(12), 'retry' => true, 'enqueued_at' => Time.now.to_f }
redis.lpush(@queue,JSON.dump(msg))
end
def redis_wrapper_options(overrides = {})
{
:host => RedisCli.configuration.host,
:port => RedisCli.configuration.port,
:db => RedisCli.configuration.db,
:queue => RedisCli.configuration.queue
}.merge(overrides || {}).delete_if { |key, value| value.nil? }
end
end
和我的初始化程序:
配置/初始化/ redis_cli.rb
RedisCli.configure do |config|
config.host = 'redis://localhost'
config.port = '6379'
config.db = ''
config.queue = 'queue:default'
end
有人能让我了解这个吗?
答案 0 :(得分:0)
This was happening to me as well on Rails 5.1.4
The only thing that worked for me was to completely exit Terminal.app
, relaunch Terminal.app
, and then re-run rails c
. Rails Console took about 4-5 seconds longer to initialize than it did before I restarted terminal. It appears that there is some sort a caching going on but that is just a hunch.