如何在自定义类型中测试 ensure 的值?

How to test the value of ensure in a custom type?

我一直在为 Puppet 编写自定义类型,并且我 运行 遇到了 'latest' 和 'present' 我都需要某些参数存在的情况,但是对于 'absent' 我希望这些参数是可选的。

不幸的是,我无法弄清楚如何在 ruby 代码中测试 'ensure' 的值。

Puppet::Type.type(:fubar) do
  ensurable do
    desc 'Has three states: absent, present, and latest.'
    newvalue(:absent) do
      # ...
    end

    newvalue(:latest) do
      # ...
    end

    newvalue(:present) do
      # ...
    end

    def insync?(is)
      # ...
    end

    defaultto :latest
  end

  # ...

  validate do
    # This if condition doesn't work.  The error is still raised.
    if :ensure != :absent
      unless value(:myprop)
        raise ArgumentError, "Property 'myprop' is required."
      end
    end
  end
end

所以,我的问题很简单...我如何测试 'ensure' 的值,以便在 'absent' 时不执行验证?

感谢 Matt Schuchard 和 John Bollinger 的帮助。

问题在于:

if :ensure != :absent

确实在比较两个符号,我需要将 属性 的值与一个符号进行比较:

if self[:ensure] != :absent

我对 Puppet 和 Ruby 都很陌生,所以我没有意识到其中的区别。约翰说得很清楚,马特提供了一个很好的例子。

再次感谢马特和约翰。