Module: Chef::Mixin::Properties

Includes:
ParamsValidate
Included in:
Resource
Defined in:
lib/chef/mixin/properties.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ParamsValidate

#lazy, #set_or_return, #validate

Class Method Details

.included(other) ⇒ Object


288
289
290
# File 'lib/chef/mixin/properties.rb', line 288

def self.included(other)
  other.extend ClassMethods
end

Instance Method Details

#copy_properties_from(other, *includes, exclude: [ :name ]) ⇒ Object

Copy properties from another property object (resource)

By default this copies all properties other than the name property (that is required to create the destination object so it has already been done in advance and this way we do not clobber the name that was set in that constructor). By default it copies everything, optional arguments can be use to only select a subset. Or specific excludes can be set (and the default exclude on the name property can also be overridden). Exclude has priority over include, although the caller is likely better off doing the set arithmetic themselves for explicitness.

action :doit do
  # use it inside a block
  file "/etc/whatever.xyz" do
    copy_properties_from new_resource
  end

  # or directly call it
  r = declare_resource(:file, "etc/whatever.xyz")
  r.copy_properties_from(new_resource, :owner, :group, :mode)
end

Parameters:

  • other (Object)

    the other object (Chef::Resource) which implements the properties API

  • includes (Array<Symbol>)

    splat-args list of symbols of the properties to copy.

  • exclude (Array<Symbol>) (defaults to: [ :name ])

    list of symbols of the properties to exclude.

Returns:

  • the self object the properties were copied to for method chaining


361
362
363
364
365
366
367
368
# File 'lib/chef/mixin/properties.rb', line 361

def copy_properties_from(other, *includes, exclude: [ :name ])
  includes = other.class.properties.keys if includes.empty?
  includes -= exclude
  includes.each do |p|
    send(p, other.send(p)) if other.property_is_set?(p)
  end
  self
end

#property_description(name) ⇒ String

The description of the property

Parameters:

  • name (Symbol)

    The name of the property.

Returns:

  • (String)

    The description of the property.

Raises:

  • (ArgumentError)

327
328
329
330
331
332
# File 'lib/chef/mixin/properties.rb', line 327

def property_description(name)
  property = self.class.properties[name.to_sym]
  raise ArgumentError, "Property #{name} is not defined in class #{self}" unless property

  property.description
end

#property_is_set?(name) ⇒ Boolean

Whether this property has been set (or whether it has a default that has been retrieved).

Parameters:

  • name (Symbol)

    The name of the property.

Returns:

  • (Boolean)

    true if the property has been set.

Raises:

  • (ArgumentError)

301
302
303
304
305
306
# File 'lib/chef/mixin/properties.rb', line 301

def property_is_set?(name)
  property = self.class.properties[name.to_sym]
  raise ArgumentError, "Property #{name} is not defined in class #{self}" unless property

  property.is_set?(self)
end

#reset_property(name) ⇒ Object

Clear this property as if it had never been set. It will thereafter return the default. been retrieved).

Parameters:

  • name (Symbol)

    The name of the property.

Raises:

  • (ArgumentError)

315
316
317
318
319
320
# File 'lib/chef/mixin/properties.rb', line 315

def reset_property(name)
  property = self.class.properties[name.to_sym]
  raise ArgumentError, "Property #{name} is not defined in class #{self}" unless property

  property.reset(self)
end