我想测试一个模块包含在一个类中.我正在尝试在RSpec中定义一个新类:
describe Statusable do
let(:test_class) do
class ModelIncludingStatusable < ActiveRecord::Base
include Statusable
statuses published: "????????????", draft: "????????"
end
end
describe '#statuses' do
it 'sets STATUSES for a model' do
test_class::STATUSES.should == ["????????????", "????????"]
end
end
end
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
TypeError:
[ActiveModel::Validations::InclusionValidator] is not a class/module
Run Code Online (Sandbox Code Playgroud)
这可能是因为Statusable
我有:
validates_inclusion_of :status, :in => statuses,
:message => "{{value}} ?????? ???? ????? ??: #{statuses.join ','}"
Run Code Online (Sandbox Code Playgroud)
但如果我发表评论,我会得到:
TypeError:
["????????????", "????????"] is not a class/module
Run Code Online (Sandbox Code Playgroud)
也许新的类定义不是最好的选择,那我该怎么办?即使不是,我如何在RSpec中定义一个类?我该如何解决这个错误?
我正在尝试使用Capistrano 2.15.5将Rails 3.2应用程序部署到Ubuntu 13.04.成功后cap deploy:setup
,Capistrano失败cap deploy:cold
了:
failed: "sh -c 'ls /srv/www/application/shared/assets/manifest*'"
我正在部署预编译资产,其中manifest.yml存在于public/assets中.
失败任务的完整输出:
triggering after callbacks for `deploy:update_code'
* 2013-11-24 23:06:43 executing `deploy:assets:precompile'
triggering before callbacks for `deploy:assets:precompile'
* 2013-11-24 23:06:43 executing `deploy:assets:update_asset_mtimes'
* executing "[ -e /srv/www/application/shared/assets/manifest* ] && cat /srv/www/application/shared/assets/manifest* || echo"
servers: ["1.1.1.1"]
[1.1.1.1] executing command
command finished in 443ms
* executing "cd -- /srv/www/application/releases/20131124190639 && RAILS_ENV=production RAILS_GROUPS=assets #<Capistrano::Configuration::Namespaces::Namespace:0x007f66b2e9a898> assets:precompile"
servers: ["1.1.1.1"]
[1.1.1.1] executing command
command finished in 439ms …
Run Code Online (Sandbox Code Playgroud) 使用哈希值替换所有键的最佳方法是什么?我提出了:
Hash[hash.map {|k,v| [v,k]}]
Run Code Online (Sandbox Code Playgroud)
有更好的解决方案吗?
传递方法来减少或注入而不是像这样的块的最佳方法是什么:
def super_process(list, item)
list ||= []
list << another_method(item) + just_another_method
end
arr = ['1', '2', '3']
arr.reduce(&method(:super_process))
Run Code Online (Sandbox Code Playgroud)
处理list
(它的默认值)时遇到问题.它被分配给第arr
一次迭代的第一个元素,但是在下一次迭代时,它被分配给第一个迭代的结果.我知道我可以写:
arr.reduce {|list, item| list << another_method(list, item) }
Run Code Online (Sandbox Code Playgroud)
但对我来说,这似乎很长,也很无聊.
我已经从git来源安装了gem:
gem 'padrino', git: 'git@github.com:padrino/padrino-framework.git'
Run Code Online (Sandbox Code Playgroud)
我如何找到它在机器上的位置?
我想在 PostgreSQL 的 JSONB 列中存储对象数组。我正在使用 Rails 5.2。我使用自定义序列化程序来确保分配给 JSONB 字段的值是数组而不是哈希。当我向该字段分配类似内容时遇到错误[{a: 1}]
。这是代码:
模型:
class Printing
serialize :card_faces, CardFacesSerializer
end
Run Code Online (Sandbox Code Playgroud)
序列化器:
class CardFacesSerializer
include JSONBArraySerializer
def allowed_attributes
%i[name image]
end
end
Run Code Online (Sandbox Code Playgroud)
序列化器关注点:
module JSONBArraySerializer
extend ActiveSupport::Concern
def initialize(data)
return [] if data.blank?
if data.is_a?(String)
json = Oj.load(data, symbol_keys: true)
end
raise ArgumentError, "#{json} must be [{},{}], not {}" if json.is_a?(Hash)
# Will only set the properties that are allowed
json.map do |hash|
hash.slice(self.allowed_attributes)
end
end
class_methods do
def …
Run Code Online (Sandbox Code Playgroud) 我试图迭代一个数组并有条件地增加一个计数器.我使用索引来比较其他数组的元素:
elements.each_with_index.with_object(0) do |(element, index), diff|
diff += 1 unless other[index] == element
end
Run Code Online (Sandbox Code Playgroud)
diff
即使无条件地更改它,我也无法改变价值.这可以通过以下方式解决inject
:
elements.each_with_index.inject(0) do |diff, (element, index)|
diff += 1 unless other[index] == element
diff
end
Run Code Online (Sandbox Code Playgroud)
但我想知道是否.each_with_index.with_object(0)
是一个有效的结构,如何使用它?
我试图重新定义一个类中定义的每个新方法并为其添加功能:
class Test
def self.method_added(name)
old_method = instance_method(name)
define_method(name) do |*args, &block|
@@tests += 1
old_method.call(*args, &block)
end
end
end
Run Code Online (Sandbox Code Playgroud)
这导致:
stack level too deep (SystemStackError)
Run Code Online (Sandbox Code Playgroud)
在线:
def self.method_added(name)
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
ruby ×7
activerecord ×1
capistrano ×1
gem ×1
jsonb ×1
postgresql ×1
rspec ×1
rspec-rails ×1
rspec2 ×1
rubygems ×1