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


296
297
298
# File 'lib/chef/mixin/properties.rb', line 296

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


369
370
371
372
373
374
375
376
# File 'lib/chef/mixin/properties.rb', line 369

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)

335
336
337
338
339
340
# File 'lib/chef/mixin/properties.rb', line 335

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)

309
310
311
312
313
314
# File 'lib/chef/mixin/properties.rb', line 309

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)

323
324
325
326
327
328
# File 'lib/chef/mixin/properties.rb', line 323

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