Method: Chef::Mixin::Properties::ClassMethods#property

Defined in:
lib/chef/mixin/properties.rb

#property(name, type = NOT_PASSED, **options) ⇒ Object Also known as: attribute

Create a property on this resource class.

If a superclass has this property, or if this property has already been defined by this resource, this will override the previous value.

Examples:

Bare property

property :x

With just a type

property :x, String

With just options

property :x, default: 'hi'

With type and options

property :x, String, default: 'hi'

Parameters:

  • name (Symbol)

    The name of the property.

  • type (Object, Array<Object>) (defaults to: NOT_PASSED)

    The type(s) of this property. If present, this is prepended to the is validation option.

  • options (Hash<Symbol,Object>)

    Validation options. @option options [Object,Array] :is An object, or list of

    objects, that must match the value using Ruby's `===` operator
    (`options[:is].any? { |v| v === value }`).
    

    @option options [Object,Array] :equal_to An object, or list

    of objects, that must be equal to the value using Ruby's `==`
    operator (`options[:is].any? { |v| v == value }`)
    

    @option options [Regexp,Array<Regexp>] :regex An object, or

    list of objects, that must match the value with `regex.match(value)`.
    

    @option options [Class,Array<Class>] :kind_of A class, or

    list of classes, that the value must be an instance of.
    

    @option options [Hash<String,Proc>] :callbacks A hash of

    messages -> procs, all of which match the value. The proc must
    return a truthy or falsey value (true means it matches).
    

    @option options [Symbol,Array<Symbol>] :respond_to A method

    name, or list of method names, the value must respond to.
    

    @option options [Symbol,Array<Symbol>] :cannot_be A property,

    or a list of properties, that the value cannot have (such as `:nil` or
    `:empty`). The method with a questionmark at the end is called on the
    value (e.g. `value.empty?`). If the value does not have this method,
    it is considered valid (i.e. if you don't respond to `empty?` we
    assume you are not empty).
    

    @option options [Proc] :coerce A proc which will be called to

    transform the user input to canonical form. The value is passed in,
    and the transformed value returned as output. Lazy values will *not*
    be passed to this method until after they are evaluated. Called in the
    context of the resource (meaning you can access other properties).
    

    @option options [Boolean] :required true if this property

    must be present; `false` otherwise. This is checked after the resource
    is fully initialized.
    

    @option options [Boolean] :name_property true if this

    property defaults to the same value as `name`. Equivalent to
    `default: lazy { name }`, except that #property_is_set? will
    return `true` if the property is set *or* if `name` is set.
    

    @option options [Boolean] :name_attribute Same as name_property. @option options [Object] :default The value this property

    will return if the user does not set one. If this is `lazy`, it will
    be run in the context of the instance (and able to access other
    properties).
    

    @option options [String] :description A description of the property. @option options [String] :introduced The release that introduced this property @option options [Boolean] :desired_state true if this property is

    part of desired state. Defaults to `true`.
    

    @option options [Boolean] :identity true if this property

    is part of object identity. Defaults to `false`.
    

    @option options [Boolean] :sensitive true if this property could

    contain sensitive information and whose value should be redacted
    in any resource reporting output. Defaults to `false`.
    


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/chef/mixin/properties.rb', line 100

def property(name, type = NOT_PASSED, **options)
  name = name.to_sym

  options = options.inject({}) { |memo, (key, value)| memo[key.to_sym] = value; memo }

  options[:instance_variable_name] = :"@#{name}" unless options.key?(:instance_variable_name)
  options[:name] = name
  options[:declared_in] = self

  if type == NOT_PASSED
    # If a type is not passed, the property derives from the
    # superclass property (if any)
    if properties.key?(name)
      property = properties[name].derive(**options)
    else
      property = property_type(**options)
    end

  # If a Property is specified, derive a new one from that.
  elsif type.is_a?(Property) || (type.is_a?(Class) && type <= Property)
    property = type.derive(**options)

  # If a primitive type was passed, combine it with "is"
  else
    if options[:is]
      options[:is] = ([ type ] + [ options[:is] ]).flatten(1)
    else
      options[:is] = type
    end
    property = property_type(**options)
  end

  local_properties = properties(false)
  local_properties[name] = property

  property.emit_dsl
end