【发布时间】:2013-05-15 17:27:35
【问题描述】:
对不起,如果我的问题很愚蠢,但我花了很多时间搜索解决方案,但我没有找到。
我想创建一个没有数据库的ApiOutputsHandler 模型。所以我创建了一个 ActiveModel。
此模型将用于我的 API 的自定义响应,例如错误(但不仅限于)。我已经使用了 send() 方法给这个模型添加了属性,但是觉得它很糟糕......
class ApiOutputsHandler
attr_accessor :status, :type, :message, :api_code, :http_code, :template
ERR_TYPES = {
:permanent_quota_limit => { :type => 'PermanentLimitException', :message => 'Quota limit reached for this action', :api_code => 700, :http_code => 401 }
}
def initialize(data)
data.each do |name, value|
send("#{name}=", value)
end
end
def error()
send('status=','error')
send('template=','api/v1/api_outputs_handler/output')
return self
end
def new
return self
end
end
然后,我像这样实例化我的对象
@output = ApiOutputsHandler.new(ApiOutputsHandler::ERR_TYPES[:permanent_quota_limit])
return @output.error()
我会推荐很多ERR_TYPES(这就是兴趣所在)。
你认为有更好的方法吗?
当我检查创建的对象时,它看起来像这样:
#<ApiOutputsHandler:0x000000036a6cd0 @type="PermanentLimitException", @message="Quota limit reached for this action">
你看到属性前面的 arobase 了吗?为什么我得到的是那个而不是普通的:
#<ApiOutputsHandler:0x000000036a6cd0 type: "PermanentLimitException", message: "Quota limit reached for this action">
感谢您的帮助!
【问题讨论】:
-
您的问题是关于更改对象在控制台中的外观吗?
-
其实有2个问题。 1-您认为有更好的方法吗? 2-是的,我可以更改对象方面以获得与 ActiveRecord 相同的类型吗?谢谢你
标签: ruby-on-rails ruby-on-rails-3 activemodel active-model-serializers