Class: Puppet::Pops::Lookup::LookupKey Private
- 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
Instance Attribute Summary collapse
- #module_name ⇒ Object readonly private
- #root_key ⇒ Object readonly private
- #segments ⇒ Object readonly private
Instance Method Summary collapse
- #dig(lookup_invocation, value) ⇒ Object private
- #eql?(v) ⇒ Boolean (also: #==) private
- #hash ⇒ Object private
-
#initialize(key) ⇒ LookupKey
constructor
private
A new instance of LookupKey.
-
#prune(value) ⇒ Object
private
Prunes a found root value with respect to subkeys in this key.
- #to_a ⇒ Object private
- #to_s ⇒ Object private
-
#undig(value) ⇒ Object
private
Create a structure that can be dug into using the subkeys of this key in order to find the given value.
Methods included from SubLookup
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.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/puppet/pops/lookup/lookup_key.rb', line 13 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_name ⇒ Object (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.
11 12 13 |
# File 'lib/puppet/pops/lookup/lookup_key.rb', line 11 def module_name @module_name end |
#root_key ⇒ Object (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.
11 12 13 |
# File 'lib/puppet/pops/lookup/lookup_key.rb', line 11 def root_key @root_key end |
#segments ⇒ Object (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.
11 12 13 |
# File 'lib/puppet/pops/lookup/lookup_key.rb', line 11 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.
24 25 26 |
# File 'lib/puppet/pops/lookup/lookup_key.rb', line 24 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.
85 86 87 |
# File 'lib/puppet/pops/lookup/lookup_key.rb', line 85 def eql?(v) v.is_a?(LookupKey) && @key == v.to_s end |
#hash ⇒ 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.
90 91 92 |
# File 'lib/puppet/pops/lookup/lookup_key.rb', line 90 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.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/puppet/pops/lookup/lookup_key.rb', line 37 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_a ⇒ 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.
76 77 78 79 80 81 82 83 |
# File 'lib/puppet/pops/lookup/lookup_key.rb', line 76 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_s ⇒ 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.
94 95 96 |
# File 'lib/puppet/pops/lookup/lookup_key.rb', line 94 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.
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/puppet/pops/lookup/lookup_key.rb', line 64 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 |