Class: Puppet::Pops::Loader::BaseLoader Private

Inherits:
Loader show all
Defined in:
lib/puppet/pops/loader/base_loader.rb

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.

Constant Summary

Constants inherited from Loader

Loader::LOADABLE_KINDS

Instance Attribute Summary collapse

Attributes inherited from Loader

#loader_name

Instance Method Summary collapse

Methods inherited from Loader

#[], #find, #inspect, #load, #loadables, #private_loader, #to_s

Constructor Details

#initialize(parent_loader, loader_name) ⇒ BaseLoader

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 new instance of BaseLoader.



16
17
18
19
20
21
# File 'lib/puppet/pops/loader/base_loader.rb', line 16

def initialize(parent_loader, loader_name)
  super(loader_name)
  @parent = parent_loader # the higher priority loader to consult
  @named_values = {}      # hash name => NamedEntry
  @last_result = nil      # the value of the last name (optimization)
end

Instance Attribute Details

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

The parent loader



14
15
16
# File 'lib/puppet/pops/loader/base_loader.rb', line 14

def parent
  @parent
end

Instance Method Details

#add_entry(type, name, value, origin) ⇒ 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.



91
92
93
# File 'lib/puppet/pops/loader/base_loader.rb', line 91

def add_entry(type, name, value, origin)
  set_entry(TypedName.new(type, name), value, origin)
end

#discover(type, error_collector = nil, name_authority = Pcore::RUNTIME_NAME_AUTHORITY, &block) ⇒ 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.



23
24
25
26
27
28
29
30
31
# File 'lib/puppet/pops/loader/base_loader.rb', line 23

def discover(type, error_collector = nil, name_authority = Pcore::RUNTIME_NAME_AUTHORITY, &block)
  result = []
  @named_values.each_pair do |key, entry|
    result << key unless entry.nil? || entry.value.nil? || key.type != type || (block_given? && !yield(key))
  end
  result.concat(parent.discover(type, error_collector, name_authority, &block))
  result.uniq!
  result
end

#get_entry(typed_name) ⇒ 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.

This method is final (subclasses should not override it)



64
65
66
# File 'lib/puppet/pops/loader/base_loader.rb', line 64

def get_entry(typed_name)
  @named_values[typed_name]
end

#load_typed(typed_name) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/puppet/pops/loader/base_loader.rb', line 35

def load_typed(typed_name)
  # The check for "last queried name" is an optimization when a module searches. First it checks up its parent
  # chain, then itself, and then delegates to modules it depends on.
  # These modules are typically parented by the same
  # loader as the one initiating the search. It is inefficient to again try to search the same loader for
  # the same name.
  if @last_result.nil? || typed_name != @last_result.typed_name
    @last_result = internal_load(typed_name)
  else
    @last_result
  end
end

#loaded_entry(typed_name, check_dependencies = false) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/puppet/pops/loader/base_loader.rb', line 50

def loaded_entry(typed_name, check_dependencies = false)
  if @named_values.has_key?(typed_name)
    @named_values[typed_name]
  elsif parent
    parent.loaded_entry(typed_name, check_dependencies)
  else
    nil
  end
end

#promote_entry(named_entry) ⇒ 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.

Promotes an already created entry (typically from another loader) to this loader



107
108
109
110
111
# File 'lib/puppet/pops/loader/base_loader.rb', line 107

def promote_entry(named_entry)
  typed_name = named_entry.typed_name
  if entry = @named_values[typed_name] then fail_redefine(entry); end
  @named_values[typed_name] = named_entry
end

#remove_entry(typed_name) ⇒ 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.



97
98
99
100
101
# File 'lib/puppet/pops/loader/base_loader.rb', line 97

def remove_entry(typed_name)
  unless @named_values.delete(typed_name).nil?
    @last_result = nil unless @last_result.nil? || typed_name != @last_result.typed_name
  end
end

#set_entry(typed_name, value, origin = nil) ⇒ 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.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/puppet/pops/loader/base_loader.rb', line 70

def set_entry(typed_name, value, origin = nil)
  # It is never ok to redefine in the very same loader unless redefining a 'not found'
  if entry = @named_values[typed_name]
    fail_redefine(entry) unless entry.value.nil?
  end

  # Check if new entry shadows existing entry and fail
  # (unless special loader allows shadowing)
  if typed_name.type == :type && !allow_shadowing?
    entry = loaded_entry(typed_name)
    if entry
      fail_redefine(entry) unless entry.value.nil? #|| entry.value == value
    end
  end

  @last_result = Loader::NamedEntry.new(typed_name, value, origin)
  @named_values[typed_name] = @last_result
end