Class: Puppet::Plugins::DataProviders::PathBasedDataProvider Abstract
- Includes:
- DataProvider
- Defined in:
- lib/puppet/plugins/data_providers/data_provider.rb
Overview
A data provider that is initialized with a set of paths. When performing lookup, each path is search in the order they appear. If a value is found in more than one location it will be merged according to a given (optional) merge strategy.
Direct Known Subclasses
DataProviders::JsonDataProvider, DataProviders::YamlDataProvider
Instance Attribute Summary collapse
- #name ⇒ Object readonly
Instance Method Summary collapse
-
#initialize(name, paths, parent_data_provider = nil) ⇒ PathBasedDataProvider
constructor
A new instance of PathBasedDataProvider.
-
#load_data(path, data_key, lookup_invocation) ⇒ Hash
Gets the data from the compiler, or initializes it by calling #initialize_data if not present in the compiler.
-
#unchecked_lookup(key, lookup_invocation, merge) ⇒ Object
Performs a lookup by searching all given paths for the given key.
- #validate_data(data, module_name) ⇒ Object
Methods included from DataProvider
#data_key, #lookup, #post_process
Constructor Details
#initialize(name, paths, parent_data_provider = nil) ⇒ PathBasedDataProvider
Returns a new instance of PathBasedDataProvider.
186 187 188 189 190 |
# File 'lib/puppet/plugins/data_providers/data_provider.rb', line 186 def initialize(name, paths, parent_data_provider = nil) @name = name @paths = paths @parent_data_provider = parent_data_provider end |
Instance Attribute Details
#name ⇒ Object (readonly)
179 180 181 |
# File 'lib/puppet/plugins/data_providers/data_provider.rb', line 179 def name @name end |
Instance Method Details
#load_data(path, data_key, lookup_invocation) ⇒ Hash
Gets the data from the compiler, or initializes it by calling #initialize_data if not present in the compiler. This means, that data is initialized once per compilation, and the data is cached for as long as the compiler lives (which is for one catalog production). This makes it possible to return data that is tailored for the request.
If data is obtained using the #initialize_data method it will be sent to the #validate_data for validation
206 207 208 209 210 |
# File 'lib/puppet/plugins/data_providers/data_provider.rb', line 206 def load_data(path, data_key, lookup_invocation) compiler = lookup_invocation.scope.compiler adapter = Puppet::DataProviders::DataAdapter.get(compiler) || Puppet::DataProviders::DataAdapter.adapt(compiler) adapter.data[path] ||= validate_data(initialize_data(path, lookup_invocation), data_key) end |
#unchecked_lookup(key, lookup_invocation, merge) ⇒ Object
Performs a lookup by searching all given paths for the given key. A merge will be performed if the value is found in more than one location and merge is not nil.
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/puppet/plugins/data_providers/data_provider.rb', line 225 def unchecked_lookup(key, lookup_invocation, merge) module_name = @parent_data_provider.nil? ? nil : @parent_data_provider.data_key(key, lookup_invocation) lookup_invocation.with(:data_provider, self) do merge_strategy = Puppet::Pops::MergeStrategy.strategy(merge) lookup_invocation.with(:merge, merge_strategy) do merged_result = merge_strategy.merge_lookup(@paths) do |path| lookup_invocation.with(:path, path) do if path.exists? hash = load_data(path.path, module_name, lookup_invocation) value = hash[key] if value || hash.include?(key) lookup_invocation.report_found(key, post_process(value, lookup_invocation)) else lookup_invocation.report_not_found(key) throw :no_such_key end else lookup_invocation.report_path_not_found throw :no_such_key end end end lookup_invocation.report_result(merged_result) end end end |
#validate_data(data, module_name) ⇒ Object
213 214 215 |
# File 'lib/puppet/plugins/data_providers/data_provider.rb', line 213 def validate_data(data, module_name) @parent_data_provider.nil? ? data : @parent_data_provider.validate_data(data, module_name) end |