Class: Puppet::Pops::Lookup::Context

Inherits:
Object
  • Object
show all
Includes:
Types::PuppetObject
Defined in:
lib/puppet/pops/lookup/context.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Types::PuppetObject

#_ptype

Constructor Details

#initialize(environment_name, module_name, lookup_invocation = Invocation.current) ⇒ Context

Returns a new instance of Context.



38
39
40
41
42
43
# File 'lib/puppet/pops/lookup/context.rb', line 38

def initialize(environment_name, module_name, lookup_invocation = Invocation.current)
  @lookup_invocation = lookup_invocation
  @environment_name = environment_name
  @module_name = module_name
  @cache = {}
end

Instance Attribute Details

#environment_nameObject (readonly)



35
36
37
# File 'lib/puppet/pops/lookup/context.rb', line 35

def environment_name
  @environment_name
end

#module_nameObject (readonly)



36
37
38
# File 'lib/puppet/pops/lookup/context.rb', line 36

def module_name
  @module_name
end

Class Method Details

._ptypeObject



6
7
8
# File 'lib/puppet/pops/lookup/context.rb', line 6

def self._ptype
  @type
end

.register_ptype(loader, ir) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/puppet/pops/lookup/context.rb', line 10

def self.register_ptype(loader, ir)
  tf = Types::TypeFactory
  @type = Pcore::create_object_type(loader, ir, self, 'Puppet::LookupContext', 'Any',
    {
      'environment_name' => Types::PStringType::NON_EMPTY,
      'module_name' => {
        Types::KEY_TYPE => tf.optional(Types::PStringType::NON_EMPTY),
        Types::KEY_VALUE => nil
      }
    },
    {
      'not_found' => tf.callable([0, 0], tf.undef),
      'explain' => tf.callable([0, 0, tf.callable(0,0)], tf.undef),
      'cache' => tf.callable([tf.scalar, tf.any], tf.undef),
      'cache_all' => tf.callable([tf.hash_kv(tf.scalar, tf.any)], tf.undef),
      'cached_value' => tf.callable([tf.scalar], tf.any),
      'cached_entries' => tf.variant(
        tf.callable([0, 0, tf.callable(1,1)], tf.undef),
        tf.callable([0, 0, tf.callable(2,2)], tf.undef),
        tf.callable([0, 0], tf.iterable(tf.tuple([tf.scalar, tf.any])))
      )
    }
  ).resolve(Types::TypeParser.singleton, loader)
end

Instance Method Details

#cache(key, value) ⇒ Object



45
46
47
48
# File 'lib/puppet/pops/lookup/context.rb', line 45

def cache(key, value)
  @cache[key] = value
  nil
end

#cache_all(hash) ⇒ Object



50
51
52
53
# File 'lib/puppet/pops/lookup/context.rb', line 50

def cache_all(hash)
  @cache.merge!(hash)
  nil
end

#cached_entries(&block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/puppet/pops/lookup/context.rb', line 59

def cached_entries(&block)
  @cache
  if block_given?
    enumerator = @cache.each_pair
    @cache.size.times do
      if block.arity == 2
        yield(*enumerator.next)
      else
        yield(enumerator.next)
      end
    end
    nil
  else
    Types::Iterable.on(@cache)
  end
end

#cached_value(key) ⇒ Object



55
56
57
# File 'lib/puppet/pops/lookup/context.rb', line 55

def cached_value(key)
  @cache[key]
end

#explain(&block) ⇒ Object



76
77
78
79
# File 'lib/puppet/pops/lookup/context.rb', line 76

def explain(&block)
  @lookup_invocation.report_text(&block) unless @lookup_invocation.nil?
  nil
end

#not_foundObject



81
82
83
# File 'lib/puppet/pops/lookup/context.rb', line 81

def not_found
  throw :no_such_key
end