Class: Puppet::Pops::Lookup::EnvironmentContext

Inherits:
Adaptable::Adapter show all
Defined in:
lib/puppet/pops/lookup/context.rb

Overview

The EnvironmentContext is adapted to the current environment

Defined Under Namespace

Classes: FileData

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Adaptable::Adapter

adapt, adapt_new, associate_adapter, clear, get, instance_var_name, self_attr_name, type_name

Constructor Details

#initialize(environment) ⇒ EnvironmentContext

Returns a new instance of EnvironmentContext.



30
31
32
33
# File 'lib/puppet/pops/lookup/context.rb', line 30

def initialize(environment)
  @environment_name = environment.name
  @file_data_cache = {}
end

Instance Attribute Details

#environment_nameObject (readonly)

Returns the value of attribute environment_name.



24
25
26
# File 'lib/puppet/pops/lookup/context.rb', line 24

def environment_name
  @environment_name
end

Class Method Details

.create_adapter(environment) ⇒ Object



26
27
28
# File 'lib/puppet/pops/lookup/context.rb', line 26

def self.create_adapter(environment)
  new(environment)
end

Instance Method Details

#cached_file_data(path) {|content| ... } ⇒ Object

Loads the contents of the file given by path. The content is then yielded to the provided block in case a block is given, and the returned value from that block is cached and returned by this method. If no block is given, the content is stored instead.

The cache is retained as long as the inode, mtime, and size of the file remains unchanged.

Parameters:

  • path (String)

    path to the file to be read

Yield Parameters:

  • content (String)

    the content that was read from the file

Yield Returns:

  • (Object)

    some result based on the content

Returns:

  • (Object)

    the content, or if a block was given, the return value of the block



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/puppet/pops/lookup/context.rb', line 46

def cached_file_data(path)
  file_data = @file_data_cache[path]
  stat = Puppet::FileSystem.stat(path)
  unless file_data && file_data.valid?(stat)
    Puppet.debug("File at '#{path}' was changed, reloading") if file_data
    content = Puppet::FileSystem.read(path, :encoding => 'utf-8')
    file_data = FileData.new(path, stat.ino, stat.mtime, stat.size, block_given? ? yield(content) : content)
    @file_data_cache[path] = file_data
  end
  file_data.data
end