Method: Puppet::Pops::Adaptable::Adapter.adapt_new

Defined in:
lib/puppet/pops/adaptable.rb

.adapt_new(o) ⇒ Adapter<self> .adapt_new(o, {|adapter| block}) ⇒ Adapter<self> .adapt_new(o, {|adapter, o| block}) ⇒ Adapter<self>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new adapter, associates it with the given object and returns the adapter.

This is used when a fresh adapter is wanted instead of possible returning an existing adapter as in the case of adapt.

Examples:

Using a block to set values

NickNameAdapter.adapt_new(o) { |a| a.nick_name = "Buddy!" }
NickNameAdapter.adapt_new(o) { |a, o| a.nick_name = "Your the best #{o.class.name} I met."}

Parameters:

  • o (Adaptable)

    object to add adapter to

  • block (Proc)

    optional, evaluated in the context of the new adapter

Yield Parameters:

  • adapter (Adapter<self>)

    the created adapter

  • o (Adaptable)

    optional, the given adaptable

Returns:

  • (Adapter<self>)

    an adapter of the same class as the receiver of the call

Raises:

  • (ArgumentError)

    if the given object o is not adaptable



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/puppet/pops/adaptable.rb', line 124

def self.adapt_new(o, &block)
  adapter = associate_adapter(create_adapter(o), o)
  if block_given?
    if block.arity == 1
      block.call(adapter)
    else
      block.call(adapter, o)
    end
  end
  adapter
end