【问题标题】:Class fields in Ruby (mongoid?)Ruby 中的类字段(mongoid?)
【发布时间】:2013-11-28 11:56:11
【问题描述】:

我找到了以下代码:

  class Store
    include ::Mongoid::Document
    include ::Mongoid::Timestamps

    field :cache_class, type: String

    index({cache_class: 1, key: 1}, {background: true})

    validates :cache_class, presence: true
  end

fieldvalidates 是方法调用吗?如果是这样,谁能解释一下这个 ruby​​ 语法?

谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby class


    【解决方案1】:

    这是一种嵌入式领域特定语言(嵌入式 DSL 或只是 EDSL)。 fieldindexvalidates 是类方法,用于配置未来对象的行为。这种语法不应该看起来像程序的和平。相反,它应该看起来像一种配置语言。

    这些方法通常在包含的类之一中定义(在本例中为::Mongoid::Document::Mongoid::Timestamps)。

    这种模式在 ruby​​ 中很常见。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-29
      • 2017-11-23
      • 1970-01-01
      • 1970-01-01
      • 2012-11-28
      • 2013-03-04
      • 2020-10-02
      相关资源
      最近更新 更多