Class: FPM::Cookery::Hiera::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/fpm/cookery/hiera/scope.rb

Overview

Wraps a recipe class, adding a [] method so that it can be used as a Hiera scope.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recipe) ⇒ Scope

Returns a new instance of Scope.



12
13
14
# File 'lib/fpm/cookery/hiera/scope.rb', line 12

def initialize(recipe)
  @recipe = recipe
end

Instance Attribute Details

#recipeObject (readonly)

Returns the value of attribute recipe.



10
11
12
# File 'lib/fpm/cookery/hiera/scope.rb', line 10

def recipe
  @recipe
end

Instance Method Details

#[](name) ⇒ Object

Allow Hiera to perform %{scope(“key”)} interpolations using data from the recipe class, FPM::Cookery::Facts, and Facter. Expects name to be a method name or Facter fact name. Returns the result of the lookup. Will be nil if lookup failed to fetch a result.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fpm/cookery/hiera/scope.rb', line 20

def [](name)
  [recipe, FPM::Cookery::Facts].each do |source|
    if source.respond_to?(name)
      return source.send(name)
    end
  end

  # As a backup, try to retrieve it from +Facter+.
  unless (result = Facter[name]).nil?
    result.value
  end
end

#include?(name) ⇒ Boolean

Newer versions of Hiera requires also #include? method for context

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
# File 'lib/fpm/cookery/hiera/scope.rb', line 34

def include?(name)
  [recipe, FPM::Cookery::Facts].each do |source|
    return true if source.respond_to?(name)
  end

  # If not found, check in +Facter+.
  ! Facter[name].nil?
end