ruby-on-rails - Rails 中空对象的命名约定?

标签 ruby-on-rails ruby null-object-pattern

对于 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 是否常规?

都不是this article by Avdi Grimm也不是 Nothing is Something Sandi Metz 的演讲提到了任何空对象命名约定。

最佳答案

目前还没有主流公约。可能是因为 Null Object Pattern本身还没有广泛使用。我遇到的对命名指南的唯一引用来自 ThoughBot Style Guide ,它使用它作为命名规则的示例:

Prefer naming classes after domain concepts rather than patterns they implement (e.g. Guest vs NullUser, CachedRequest vs RequestDecorator).

在您的情况下,这将是为“空博客”找到“域语言”的问题。我找到的查找这些术语的最佳方法是简单地与域用户讨论该主题,并记下他们使用的任何名词。 Object Thinking 一书中概述了这种方法.

关于ruby-on-rails - Rails 中空对象的命名约定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33062862/

相关文章:

javascript - Javascript:在请求的对象“未找到”的情况下,使用模式空对象模式,RORO,未定义等作为Get函数的返回参数

ruby-on-rails - ruby on Rails 多元化有帮助吗?

ruby-on-rails - 如何在 rspec 测试中忽略 ApplicationController 中的 rescue_from?

ruby - 生产服务器上的 RVM?

ruby-on-rails - 在 Ruby on Rails 中使用 pdftk 库检查复选框

typescript - 功能范式的空对象模式

ruby-on-rails - RVM删除所有 ruby 和所有对应的 gem 集

javascript - 通过 jQuery 重置表单不清除 ckeditor cktext_area 字段

ruby-on-rails - 仅当 Rails 5 中的值可用时才在方法中使用值

java - 空对象模式