Module: Poise::Helpers::Subresources::Child::ClassMethods

Included in:
Poise::Helpers::Subresources::Child
Defined in:
lib/poise/helpers/subresources/child.rb

Overview

Since:

  • 1.0.0

Instance Method Summary collapse

Instance Method Details

#parent_attribute(name, type: Chef::Resource, optional: false, auto: true, default: nil)

This method returns an undefined value.

Create a new kind of parent link.

Parameters:

  • name (Symbol)

    Name of the relationship. This becomes a method name on the resource instance.

  • type (Class) (defaults to: Chef::Resource)

    Class of the parent.

  • optional (Boolean) (defaults to: false)

    If the parent is optional.

  • auto (Boolean) (defaults to: true)

    If the parent is auto-detected.

Since:

  • 2.0.0



240
241
242
243
244
245
246
# File 'lib/poise/helpers/subresources/child.rb', line 240

def parent_attribute(name, type: Chef::Resource, optional: false, auto: true, default: nil)
  name = :"parent_#{name}"
  (@parent_attributes ||= {})[name] = type
  define_method(name) do |*args|
    _parent(name, type, optional, auto, default, *args)
  end
end

#parent_attributesHash<Symbol, Class>

Return the name of all parent relationships on this class.

Returns:

  • (Hash<Symbol, Class>)

Since:

  • 2.0.0



252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/poise/helpers/subresources/child.rb', line 252

def parent_attributes
  {}.tap do |attrs|
    # Grab superclass's attributes if possible.
    attrs.update(Poise::Utils.ancestor_send(self, :parent_attributes, default: {}))
    # Local default parent.
    attrs[:parent] = parent_type
    # Extra locally defined parents.
    attrs.update(@parent_attributes) if @parent_attributes
    # Remove anything with the type set to true.
    attrs.reject! {|name, type| type == true }
  end
end

#parent_autoBoolean #parent_auto(val) ⇒ Boolean

Overloads:

  • #parent_autoBoolean

    Get the auto-detect mode for the default parent link on this resource.

    Returns:

    • (Boolean)
  • #parent_auto(val) ⇒ Boolean

    Set the auto-detect mode for the default parent link on this resource.

    Parameters:

    • val (Boolean)

      Mode to set.

    Returns:

    • (Boolean)

Since:

  • 1.0.0



200
201
202
203
204
205
206
207
208
209
# File 'lib/poise/helpers/subresources/child.rb', line 200

def parent_auto(val=nil)
  unless val.nil?
    @parent_auto = val
  end
  if @parent_auto.nil?
    Poise::Utils.ancestor_send(self, :parent_auto, default: true)
  else
    @parent_auto
  end
end

#parent_defaultObject, Chef::DelayedEvaluator #parent_default(val) ⇒ Object, Chef::DelayedEvaluator

Overloads:

  • #parent_defaultObject, Chef::DelayedEvaluator

    Get the default value for the default parent link on this resource.

    Returns:

    • (Object, Chef::DelayedEvaluator)

    Since:

    • 2.3.0

  • #parent_default(val) ⇒ Object, Chef::DelayedEvaluator

    Set the default value for the default parent link on this resource.

    Parameters:

    • val (Object, Chef::DelayedEvaluator)

      Default value to set.

    Returns:

    • (Object, Chef::DelayedEvaluator)

    Since:

    • 2.3.0

Since:

  • 1.0.0



220
221
222
223
224
225
226
227
228
229
# File 'lib/poise/helpers/subresources/child.rb', line 220

def parent_default(*args)
  unless args.empty?
    @parent_default = args.first
  end
  if defined?(@parent_default)
    @parent_default
  else
    Poise::Utils.ancestor_send(self, :parent_default)
  end
end

#parent_optionalBoolean #parent_optional(val) ⇒ Boolean

Overloads:

  • #parent_optionalBoolean

    Get the optional mode for the default parent link on this resource.

    Returns:

    • (Boolean)
  • #parent_optional(val) ⇒ Boolean

    Set the optional mode for the default parent link on this resource.

    Parameters:

    • val (Boolean)

      Mode to set.

    Returns:

    • (Boolean)

Since:

  • 1.0.0



182
183
184
185
186
187
188
189
190
191
# File 'lib/poise/helpers/subresources/child.rb', line 182

def parent_optional(val=nil)
  unless val.nil?
    @parent_optional = val
  end
  if @parent_optional.nil?
    Poise::Utils.ancestor_send(self, :parent_optional, default: false)
  else
    @parent_optional
  end
end

#parent_typeClass, Symbol #parent_type(type) ⇒ Class, Symbol

Overloads:

  • #parent_typeClass, Symbol

    Get the class of the default parent link on this resource.

    Returns:

    • (Class, Symbol)
  • #parent_type(type) ⇒ Class, Symbol

    Set the class of the default parent link on this resource.

    Parameters:

    • type (Class, Symbol)

      Class to set.

    Returns:

    • (Class, Symbol)

Since:

  • 1.0.0



164
165
166
167
168
169
170
171
172
173
# File 'lib/poise/helpers/subresources/child.rb', line 164

def parent_type(type=nil)
  if type
    raise Poise::Error.new("Parent type must be a class, symbol, or true, got #{type.inspect}") unless type.is_a?(Class) || type.is_a?(Symbol) || type == true
    # Setting to true shouldn't actually do anything if a type was already set.
    @parent_type = type unless type == true && !@parent_type.nil?
  end
  # First ancestor_send looks for a non-true && non-default value,
  # second one is to check for default vs true if no real value is found.
  @parent_type || Poise::Utils.ancestor_send(self, :parent_type, ignore: [Chef::Resource, true]) || Poise::Utils.ancestor_send(self, :parent_type, default: Chef::Resource)
end