Class: Puppet::Pops::Binder::Binder

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/pops/binder/binder.rb

Defined Under Namespace

Classes: LayerProcessor

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(layered_bindings, parent_binder = nil) ⇒ Binder

Returns a new instance of Binder.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/puppet/pops/binder/binder.rb', line 36

def initialize(layered_bindings, parent_binder=nil)
  @parent = parent_binder
  @id_index = Hash.new() { |k, v| [] }

  @key_factory = 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_keyObject (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



29
30
31
# File 'lib/puppet/pops/binder/binder.rb', line 29

def anonymous_key
  @anonymous_key
end

#binder_precedenceObject (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



33
34
35
# File 'lib/puppet/pops/binder/binder.rb', line 33

def binder_precedence
  @binder_precedence
end

#id_indexObject (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.



18
19
20
# File 'lib/puppet/pops/binder/binder.rb', line 18

def id_index
  @id_index
end

#injector_entriesObject (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.



15
16
17
# File 'lib/puppet/pops/binder/binder.rb', line 15

def injector_entries
  @injector_entries
end

#key_factoryObject (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.



21
22
23
# File 'lib/puppet/pops/binder/binder.rb', line 21

def key_factory
  @key_factory
end

#parentObject (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



25
26
27
# File 'lib/puppet/pops/binder/binder.rb', line 25

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.



142
143
144
145
146
# File 'lib/puppet/pops/binder/binder.rb', line 142

def self.format_binding(b)
  type_name = b.type.to_s
  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.



149
150
151
152
# File 'lib/puppet/pops/binder/binder.rb', line 149

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.



155
156
157
158
159
# File 'lib/puppet/pops/binder/binder.rb', line 155

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?(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.



162
163
164
165
166
# File 'lib/puppet/pops/binder/binder.rb', line 162

def self.get_named_layer(b)
  return '<unknown>' if b.nil?
  return b.name if b.is_a?(Bindings::NamedLayer)
  get_named_layer(b.eContainer)
end

Instance Method Details

#add_id_to_index(binding) ⇒ Object



111
112
113
114
# File 'lib/puppet/pops/binder/binder.rb', line 111

def add_id_to_index(binding)
  return unless binding.is_a?(Bindings::Multibinding) && !(id = binding.id).nil?
  @id_index[id] = @id_index[id] << binding
end

#lookup(key) ⇒ Object



134
135
136
137
138
139
# File 'lib/puppet/pops/binder/binder.rb', line 134

def lookup(key)
  if x = injector_entries[key]
    return x
  end
  @parent ?  @parent.lookup(key) :  nil
end

#lookup_in_parent(key) ⇒ Object



130
131
132
# File 'lib/puppet/pops/binder/binder.rb', line 130

def lookup_in_parent(key)
  @parent.nil? ? nil : @parent.lookup(key)
end

#next_anonymous_keyObject

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.



105
106
107
108
109
# File 'lib/puppet/pops/binder/binder.rb', line 105

def next_anonymous_key
  tmp = @anonymous_key
  @anonymous_key += 1
  tmp
end

#promote_matching_bindings(to_binder, from_binder, multibind_id) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/puppet/pops/binder/binder.rb', line 116

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 ] = 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