对于 Rails 应用程序:是否存在命名映射到模型对象的空对象的命名约定?
例子:
#app/models/blog.rb
class Blog < ActiveRecord::Base
end
#app/models/null_blog.rb
class NullBlog # Null Objects are POROs, correct?
def title
"No title"
end
end
# so to implement it I can do this:
blogs = ids.map{|id| Blog.find_by(id: id) || NullBlog.new}
# which allows me to do this and it works, even if some of those ids did not find a blog
blogs.each{|blog| blog.title}
是NullBlog
常规的吗?
Avdi Grimm 的这篇文章和 Sandi Metz 的Nothing is Something演讲都没有提到任何空对象命名约定。