Class: Puppet::Pops::Adaptable::Adapter Private
- Defined in:
- lib/puppet/pops/adaptable.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Base class for an Adapter.
A typical adapter just defines some accessors.
A more advanced adapter may need to setup the adapter based on the object it is adapting.
Direct Known Subclasses
Bindings::NamedBindingsAdapter, DataProviders::DataAdapter, Puppet::Pops::Adapters::DocumentationAdapter, Puppet::Pops::Adapters::LoaderAdapter, Puppet::Pops::Adapters::SourcePosAdapter
Constant Summary collapse
- DOUBLE_COLON =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'::'
- USCORE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'_'
Class Method Summary collapse
-
.adapt(o, &block) {|adapter, o| ... } ⇒ Adapter<self>
private
Returns an existing adapter for the given object, or creates a new adapter if the object has not been adapted, or the adapter has been cleared.
-
.adapt_new(o, &block) {|adapter, o| ... } ⇒ Adapter<self>
private
Creates a new adapter, associates it with the given object and returns the adapter.
-
.associate_adapter(adapter, o) ⇒ adapter
private
Associates the given adapter with the given target object.
-
.clear(o) ⇒ Adapter?
private
Clears the adapter set in the given object o.
-
.create_adapter(o) ⇒ Adapter<self>
private
This base version creates an instance of the class (i.e. an instance of the concrete subclass of Adapter).
-
.get(o) ⇒ Adapter<self>?
private
Returns an existing adapter for the given object, or nil, if the object is not adapted.
-
.instance_var_name(name) ⇒ String
private
Returns a suitable instance variable name given a class name.
-
.self_attr_name ⇒ String
private
Returns a suitable instance variable name for the name of this instance.
Class Method Details
.adapt(o) ⇒ Adapter<self> .adapt(o, {|adapter| block}) ⇒ Adapter<self> .adapt(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.
Returns an existing adapter for the given object, or creates a new adapter if the object has not been adapted, or the adapter has been cleared.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/puppet/pops/adaptable.rb', line 94 def self.adapt(o, &block) attr_name = self_attr_name adapter = if o.instance_variable_defined?(attr_name) && value = o.instance_variable_get(attr_name) value else associate_adapter(create_adapter(o), o) end if block_given? case block.arity when 1 block.call(adapter) else block.call(adapter, o) end end adapter end |
.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.
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/puppet/pops/adaptable.rb', line 129 def self.adapt_new(o, &block) adapter = associate_adapter(create_adapter(o), o) if block_given? case block.arity when 1 block.call(adapter) else block.call(adapter, o) end end adapter end |
.associate_adapter(adapter, o) ⇒ adapter
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.
Associates the given adapter with the given target object
172 173 174 175 176 |
# File 'lib/puppet/pops/adaptable.rb', line 172 def self.associate_adapter(adapter, o) attr_name = :"@#{instance_var_name(adapter.class.name)}" o.instance_variable_set(attr_name, adapter) adapter end |
.clear(o) ⇒ Adapter?
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.
Clears the adapter set in the given object o. Returns any set adapter or nil.
147 148 149 150 151 152 153 154 |
# File 'lib/puppet/pops/adaptable.rb', line 147 def self.clear(o) attr_name = self_attr_name if o.instance_variable_defined?(attr_name) o.send(:remove_instance_variable, attr_name) else nil end end |
.create_adapter(o) ⇒ 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.
This base version creates an instance of the class (i.e. an instance of the concrete subclass of Adapter). A Specialization may want to create an adapter instance specialized for the given target object.
163 164 165 |
# File 'lib/puppet/pops/adaptable.rb', line 163 def self.create_adapter(o) new end |
.get(o) ⇒ 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.
Returns an existing adapter for the given object, or nil, if the object is not adapted.
69 70 71 72 73 74 75 76 |
# File 'lib/puppet/pops/adaptable.rb', line 69 def self.get(o) attr_name = self_attr_name if o.instance_variable_defined?(attr_name) o.instance_variable_get(attr_name) else nil end end |
.instance_var_name(name) ⇒ String
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.
Returns a suitable instance variable name given a class name. The returned string is the fully qualified name of a class with ‘::’ replaced by ‘_’ since ‘::’ is not allowed in an instance variable name.
188 189 190 |
# File 'lib/puppet/pops/adaptable.rb', line 188 def self.instance_var_name(name) name.split(DOUBLE_COLON).join(USCORE) end |
.self_attr_name ⇒ String
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.
Returns a suitable instance variable name for the name of this instance. The name is created by calling Adapter#instance_var_name and then cached.
197 198 199 |
# File 'lib/puppet/pops/adaptable.rb', line 197 def self.self_attr_name @attr_name_sym ||= :"@#{instance_var_name(self.name)}" end |