Class: Puppet::Pops::Lookup::LookupKey Private

Inherits:
Object
  • Object
show all
Includes:
SubLookup
Defined in:
lib/puppet/pops/lookup/lookup_key.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

LOOKUP_OPTIONS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

LookupKey.new('lookup_options')

Constants included from SubLookup

SubLookup::SPECIAL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SubLookup

#split_key, #sub_lookup

Constructor Details

#initialize(key) ⇒ LookupKey

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of LookupKey.


11
12
13
14
15
16
17
18
19
20
# File 'lib/puppet/pops/lookup/lookup_key.rb', line 11

def initialize(key)
  segments = split_key(key) { |problem| Puppet::DataBinding::LookupError.new(_("%{problem} in key: '%{key}'") % { problem: problem, key: key }) }
  root_key = segments.shift.freeze
  qual_index = root_key.index(DOUBLE_COLON)

  @key = key
  @module_name = qual_index.nil? ? nil : root_key[0..qual_index-1].freeze
  @root_key = root_key
  @segments = segments.empty? ? nil : segments.freeze
end

Instance Attribute Details

#module_nameObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


9
10
11
# File 'lib/puppet/pops/lookup/lookup_key.rb', line 9

def module_name
  @module_name
end

#root_keyObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


9
10
11
# File 'lib/puppet/pops/lookup/lookup_key.rb', line 9

def root_key
  @root_key
end

#segmentsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


9
10
11
# File 'lib/puppet/pops/lookup/lookup_key.rb', line 9

def segments
  @segments
end

Instance Method Details

#dig(lookup_invocation, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


22
23
24
# File 'lib/puppet/pops/lookup/lookup_key.rb', line 22

def dig(lookup_invocation, value)
  @segments.nil? ? value : sub_lookup(@key, lookup_invocation, @segments, value)
end

#eql?(v) ⇒ Boolean Also known as: ==

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

83
84
85
# File 'lib/puppet/pops/lookup/lookup_key.rb', line 83

def eql?(v)
  v.is_a?(LookupKey) && @key == v.to_s
end

#hashObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


88
89
90
# File 'lib/puppet/pops/lookup/lookup_key.rb', line 88

def hash
  @key.hash
end

#prune(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prunes a found root value with respect to subkeys in this key. The given value is returned untouched if this key has no subkeys. Otherwise an attempt is made to create a Hash or Array that contains only the path to the appointed value and that value.

If subkeys exists and no value is found, then this method will return `nil`, an empty `Array` or an empty `Hash` to enable further merges to be applied. The returned type depends on the given value.

Parameters:

  • value (Object)

    the value to prune

Returns:

  • the possibly pruned value


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/puppet/pops/lookup/lookup_key.rb', line 35

def prune(value)
  if @segments.nil?
    value
  else
    pruned = @segments.reduce(value) do |memo, segment|
      memo.is_a?(Hash) || memo.is_a?(Array) && segment.is_a?(Integer) ? memo[segment] : nil
    end
    if pruned.nil?
      case value
      when Hash
        EMPTY_HASH
      when Array
        EMPTY_ARRAY
      else
        nil
      end
    else
      undig(pruned)
    end
  end
end

#to_aObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


74
75
76
77
78
79
80
81
# File 'lib/puppet/pops/lookup/lookup_key.rb', line 74

def to_a
  unless instance_variable_defined?(:@all_segments)
    a = [@root_key]
    a += @segments unless @segments.nil?
    @all_segments = a.freeze
  end
  @all_segments
end

#to_sObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


92
93
94
# File 'lib/puppet/pops/lookup/lookup_key.rb', line 92

def to_s
  @key
end

#undig(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a structure that can be dug into using the subkeys of this key in order to find the given value. If this key has no subkeys, the value is returned.

Parameters:

  • value (Object)

    the value to wrap in a structure in case this value has subkeys

Returns:

  • (Object)

    the possibly wrapped value


62
63
64
65
66
67
68
69
70
71
72
# File 'lib/puppet/pops/lookup/lookup_key.rb', line 62

def undig(value)
  @segments.nil? ? value : segments.reverse.reduce(value) do |memo, segment|
    if segment.is_a?(Integer)
      x = []
      x[segment] = memo
    else
      x = { segment => memo }
    end
    x
  end
end