Class: Puppet::Pops::Binder::Binder
- Defined in:
- lib/puppet/pops/binder/binder.rb
Overview
The Binder is responsible for processing layered bindings that can be used to setup an Injector.
An instance should be created and a call to #define_layers should be made which will process the layered bindings (handle overrides, abstract entries etc.). The constructed hash with ‘key => InjectorEntry` mappings is obtained as #injector_entries, and is used to initialize an Injector.
Defined Under Namespace
Classes: LayerProcessor
Instance Attribute Summary collapse
-
#anonymous_key ⇒ Object
readonly
private
The next anonymous key to use.
-
#binder_precedence ⇒ Object
readonly
private
This binder’s precedence.
- #id_index ⇒ Object readonly private
- #injector_entries ⇒ Object readonly private
- #key_factory ⇒ Object readonly private
-
#parent ⇒ Object
readonly
private
A parent Binder or nil.
Class Method Summary collapse
- .format_binding(b) ⇒ Object private
- .format_contribution_source(b) ⇒ Object private
- .get_named_binding_layer_and_name(b) ⇒ Object private
- .get_named_layer(b) ⇒ Object private
Instance Method Summary collapse
- #add_id_to_index(binding) ⇒ Object
-
#initialize(layered_bindings, parent_binder = nil) ⇒ Binder
constructor
A new instance of Binder.
- #lookup(key) ⇒ Object
- #lookup_in_parent(key) ⇒ Object
- #next_anonymous_key ⇒ Object private
- #promote_matching_bindings(to_binder, from_binder, multibind_id) ⇒ Object
Constructor Details
#initialize(layered_bindings, parent_binder = nil) ⇒ Binder
Returns a new instance of Binder.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/puppet/pops/binder/binder.rb', line 34 def initialize(layered_bindings, parent_binder=nil) @parent = parent_binder @id_index = Hash.new() { |k, v| [] } @key_factory = Puppet::Pops::Binder::KeyFactory.new() # Resulting hash of all key -> binding @injector_entries = {} if @parent.nil? @anonymous_key = 0 @binder_precedence = 0 else # First anonymous key is the parent's next (non incremented key). (The parent can not change, it is # the final, free key). @anonymous_key = @parent.anonymous_key @binder_precedence = @parent.binder_precedence + 1 end define_layers(layered_bindings) end |
Instance Attribute Details
#anonymous_key ⇒ Object (readonly)
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.
The next anonymous key to use
27 28 29 |
# File 'lib/puppet/pops/binder/binder.rb', line 27 def anonymous_key @anonymous_key end |
#binder_precedence ⇒ Object (readonly)
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 binder’s precedence
31 32 33 |
# File 'lib/puppet/pops/binder/binder.rb', line 31 def binder_precedence @binder_precedence end |
#id_index ⇒ Object (readonly)
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.
16 17 18 |
# File 'lib/puppet/pops/binder/binder.rb', line 16 def id_index @id_index end |
#injector_entries ⇒ Object (readonly)
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.
13 14 15 |
# File 'lib/puppet/pops/binder/binder.rb', line 13 def injector_entries @injector_entries end |
#key_factory ⇒ Object (readonly)
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.
19 20 21 |
# File 'lib/puppet/pops/binder/binder.rb', line 19 def key_factory @key_factory end |
#parent ⇒ Object (readonly)
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.
A parent Binder or nil
23 24 25 |
# File 'lib/puppet/pops/binder/binder.rb', line 23 def parent @parent end |
Class Method Details
.format_binding(b) ⇒ Object
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.
140 141 142 143 144 |
# File 'lib/puppet/pops/binder/binder.rb', line 140 def self.format_binding(b) type_name = Puppet::Pops::Types::TypeCalculator.new().string(b.type) layer_name, bindings_name = get_named_binding_layer_and_name(b) "binding: '#{type_name}/#{b.name}' in: '#{bindings_name}' in layer: '#{layer_name}'" end |
.format_contribution_source(b) ⇒ Object
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.
147 148 149 150 |
# File 'lib/puppet/pops/binder/binder.rb', line 147 def self.format_contribution_source(b) layer_name, bindings_name = get_named_binding_layer_and_name(b) "(layer: #{layer_name}, bindings: #{bindings_name})" end |
.get_named_binding_layer_and_name(b) ⇒ Object
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.
153 154 155 156 157 |
# File 'lib/puppet/pops/binder/binder.rb', line 153 def self.get_named_binding_layer_and_name(b) return ['<unknown>', '<unknown>'] if b.nil? return [get_named_layer(b), b.name] if b.is_a?(Puppet::Pops::Binder::Bindings::NamedBindings) get_named_binding_layer_and_name(b.eContainer) end |
.get_named_layer(b) ⇒ Object
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.
160 161 162 163 164 |
# File 'lib/puppet/pops/binder/binder.rb', line 160 def self.get_named_layer(b) return '<unknown>' if b.nil? return b.name if b.is_a?(Puppet::Pops::Binder::Bindings::NamedLayer) get_named_layer(b.eContainer) end |
Instance Method Details
#add_id_to_index(binding) ⇒ Object
109 110 111 112 |
# File 'lib/puppet/pops/binder/binder.rb', line 109 def add_id_to_index(binding) return unless binding.is_a?(Puppet::Pops::Binder::Bindings::Multibinding) && !(id = binding.id).nil? @id_index[id] = @id_index[id] << binding end |
#lookup(key) ⇒ Object
132 133 134 135 136 137 |
# File 'lib/puppet/pops/binder/binder.rb', line 132 def lookup(key) if x = injector_entries[key] return x end @parent ? @parent.lookup(key) : nil end |
#lookup_in_parent(key) ⇒ Object
128 129 130 |
# File 'lib/puppet/pops/binder/binder.rb', line 128 def lookup_in_parent(key) @parent.nil? ? nil : @parent.lookup(key) end |
#next_anonymous_key ⇒ Object
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.
103 104 105 106 107 |
# File 'lib/puppet/pops/binder/binder.rb', line 103 def next_anonymous_key tmp = @anonymous_key @anonymous_key += 1 tmp end |
#promote_matching_bindings(to_binder, from_binder, multibind_id) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/puppet/pops/binder/binder.rb', line 114 def promote_matching_bindings(to_binder, from_binder, multibind_id) return if from_binder.nil? from_binder.id_index[ multibind_id ].each do |binding| key = key_factory.binding_key(binding) entry = lookup(key) unless entry.precedence == @binder_precedence # it is from a lower layer it must be promoted injector_entries[ key ] = Puppet::Pops::Binder::InjectorEntry.new(binding, binder_precedence) end end # recursive "up the parent chain" to promote all promote_matching_bindings(to_binder, from_binder.parent, multibind_id) end |