Class: Hiera::Filecache

### Home Classes Methods

### In Files

- lib/hiera/filecache.rb

### Parent

Object

### Methods

- ::new
- #read
- #read_file

### Class/Module Index

Quicksearch

- Hiera
- Hiera::Backend
- Hiera::Backend::Json_backend
- Hiera::Backend::Yaml_backend
- Hiera::Config
- Hiera::Console_logger
- Hiera::Error
- Hiera::FallbackLogger
- Hiera::Filecache
- Hiera::Interpolate
- Hiera::InterpolationLoop
- Hiera::InvalidConfigurationError
- Hiera::Noop_logger
- Hiera::Puppet_logger
- Hiera::RecursiveGuard
- Hiera::Util

No matching classes.

# Hiera::Filecache

### Public Class Methods

new() click to toggle source

    # File lib/hiera/filecache.rb, line 3
    def initialize
      @cache = {}
    end

### Public Instance Methods

read(path, expected_type = Object, default=nil, &block) click to toggle source

Reads a file, optionally parse it in some way check the output type and set a default

Simply invoking it this way will return the file contents

    data = read("/some/file")

But as most cases of file reading in hiera involves some kind of parsing through a serializer there's some help for those cases:

    data = read("/some/file", Hash, {}) do |data|
       JSON.parse(data)
    end

In this case it will read the file, parse it using JSON then check that the end result is a Hash, if it's not a hash or if reading/parsing fails it will return {} instead

Prior to calling this method you should be sure the file exist

    # File lib/hiera/filecache.rb, line 27
    def read(path, expected_type = Object, default=nil, &block)
      read_file(path, expected_type, &block)
    rescue TypeError => detail
      Hiera.debug("#{detail.message}, setting defaults")
      @cache[path][:data] = default
    rescue => detail
      error = "Reading data from #{path} failed: #{detail.class}: #{detail}"
      if default.nil?
        raise detail
      else
        Hiera.debug(error)
        @cache[path][:data] = default
      end
    end

read_file(path, expected_type = Object) click to toggle source

Read a file when it changes. If a file is re-read and has not changed since the last time then the last, processed, contents will be returned.

The processed data can also be checked against an expected type. If the type does not match a TypeError is raised.

No error handling is done inside this method. Any failed reads or errors in processing will be propagated to the caller

    # File lib/hiera/filecache.rb, line 50
    def read_file(path, expected_type = Object)
      if stale?(path)
        data = File.read(path)
        @cache[path][:data] = block_given? ? yield(data) : data

        if !@cache[path][:data].is_a?(expected_type)
          raise TypeError, "Data retrieved from #{path} is #{data.class} not #{expected_type}"
        end
      end

      @cache[path][:data]
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.