Module: Chef::Mixin::Properties::ClassMethods
- Defined in:
- lib/chef/mixin/properties.rb
Instance Method Summary collapse
- #deprecated_property_alias(from, to, message) ⇒ Object
-
#identity_properties(*names) ⇒ Array<Property>
Set the identity of this resource to a particular set of properties.
- #included(other) ⇒ Object
-
#lazy(&block) ⇒ Chef::DelayedEvaluator
Create a lazy value for assignment to a default value.
-
#name_property ⇒ Symbol
Returns the name of the name property.
-
#properties(include_superclass = true) ⇒ Hash<Symbol,Property>
The list of properties defined on this resource.
-
#property(name, type = NOT_PASSED, **options) ⇒ Object
(also: #attribute)
Create a property on this resource class.
-
#property_type(**options) ⇒ Object
Create a reusable property type that can be used in multiple properties in different resources.
-
#sensitive_properties ⇒ Array<Property>
This method returns list of sensitive properties.
-
#state_properties(*names) ⇒ Array<Property>
Get or set the list of desired state properties for this resource.
Instance Method Details
#deprecated_property_alias(from, to, message) ⇒ Object
154 155 156 |
# File 'lib/chef/mixin/properties.rb', line 154 def deprecated_property_alias(from, to, ) Property.emit_deprecated_alias(from, to, , self) end |
#identity_properties(*names) ⇒ Array<Property>
Set the identity of this resource to a particular set of properties.
This drives #identity, which returns data that uniquely refers to a given resource on the given node (in such a way that it can be correlated across Chef runs).
This method is unnecessary when declaring properties with ‘property`; properties can be added to identity during declaration with `identity: true`.
“‘ruby property :x, identity: true # part of identity property :y # not part of identity “`
If no properties are marked as identity, “name” is considered the identity.
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/chef/mixin/properties.rb', line 244 def identity_properties(*names) unless names.empty? names = names.map(&:to_sym) # Add or change properties that are not part of the identity. names.each do |name| property = properties[name] if !property self.property name, instance_variable_name: false, identity: true elsif !property.identity? self.property name, identity: true end end # If identity_properties *excludes* something which is currently part of # the identity, mark it as identity: false. properties.each do |name, property| if property.identity? && !names.include?(name) self.property name, identity: false end end end result = properties.values.select(&:identity?) # if there are no other identity properties set, then the name_property becomes the identity, or # failing that we use the actual name. if result.empty? result = name_property ? [ properties[name_property] ] : [ properties[:name] ] end result end |
#included(other) ⇒ Object
291 292 293 |
# File 'lib/chef/mixin/properties.rb', line 291 def included(other) other.extend ClassMethods end |
#lazy(&block) ⇒ Chef::DelayedEvaluator
Create a lazy value for assignment to a default value.
165 166 167 |
# File 'lib/chef/mixin/properties.rb', line 165 def lazy(&block) DelayedEvaluator.new(&block) end |
#name_property ⇒ Symbol
Returns the name of the name property. Returns nil if there is no name property.
286 287 288 289 |
# File 'lib/chef/mixin/properties.rb', line 286 def name_property p = properties.find { |n, p| p.name_property? } p ? p.first : nil end |
#properties(include_superclass = true) ⇒ Hash<Symbol,Property>
The list of properties defined on this resource.
Everything defined with ‘property` is in this list.
20 21 22 23 24 25 26 27 28 |
# File 'lib/chef/mixin/properties.rb', line 20 def properties(include_superclass = true) if include_superclass result = {} ancestors.reverse_each { |c| result.merge!(c.properties(false)) if c.respond_to?(:properties) } result else @properties ||= {} end end |
#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.
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, **) name = name.to_sym = .inject({}) { |memo, (key, value)| memo[key.to_sym] = value; memo } [:instance_variable_name] = :"@#{name}" unless .key?(:instance_variable_name) [:name] = name [: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(**) else property = property_type(**) 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(**) # If a primitive type was passed, combine it with "is" else if [:is] [:is] = ([ type ] + [ [:is] ]).flatten(1) else [:is] = type end property = property_type(**) end local_properties = properties(false) local_properties[name] = property property.emit_dsl end |
#property_type(**options) ⇒ Object
Create a reusable property type that can be used in multiple properties in different resources.
150 151 152 |
# File 'lib/chef/mixin/properties.rb', line 150 def property_type(**) Property.derive(**) end |
#sensitive_properties ⇒ Array<Property>
This method returns list of sensitive properties
279 280 281 |
# File 'lib/chef/mixin/properties.rb', line 279 def sensitive_properties properties.values.empty? ? [] : properties.values.select(&:sensitive?) end |
#state_properties(*names) ⇒ Array<Property>
Get or set the list of desired state properties for this resource.
State properties are properties that describe the desired state of the system, such as file permissions or ownership. In general, state properties are properties that could be populated by examining the state of the system (e.g., File.stat can tell you the permissions on an existing file). Contrarily, properties that are not “state properties” usually modify the way Chef itself behaves, for example by providing additional options for a package manager to use when installing a package.
This method is unnecessary when declaring properties with ‘property`; properties are added to state_properties by default, and can be turned off with `desired_state: false`.
“‘ruby property :x # part of desired state property :y, desired_state: false # not part of desired state “`
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/chef/mixin/properties.rb', line 195 def state_properties(*names) unless names.empty? names = names.map(&:to_sym).uniq local_properties = properties(false) # Add new properties to the list. names.each do |name| property = properties[name] if !property self.property name, instance_variable_name: false, desired_state: true elsif !property.desired_state? self.property name, desired_state: true end end # If state_attrs *excludes* something which is currently desired state, # mark it as desired_state: false. local_properties.each do |name, property| if property.desired_state? && !names.include?(name) self.property name, desired_state: false end end end properties.values.select(&:desired_state?) end |