class Puppet::Pops::Lookup::LookupKey
@api private
Constants
- LOOKUP_OPTIONS
Attributes
Public Class Methods
# File lib/puppet/pops/lookup/lookup_key.rb 12 def initialize(key) 13 segments = split_key(key) { |problem| Puppet::DataBinding::LookupError.new(_("%{problem} in key: '%{key}'") % { problem: problem, key: key }) } 14 root_key = segments.shift.freeze 15 qual_index = root_key.index(DOUBLE_COLON) 16 17 @key = key 18 @module_name = qual_index.nil? ? nil : root_key[0..qual_index-1].freeze 19 @root_key = root_key 20 @segments = segments.empty? ? nil : segments.freeze 21 end
Public Instance Methods
# File lib/puppet/pops/lookup/lookup_key.rb 23 def dig(lookup_invocation, value) 24 @segments.nil? ? value : sub_lookup(@key, lookup_invocation, @segments, value) 25 end
# File lib/puppet/pops/lookup/lookup_key.rb 84 def eql?(v) 85 v.is_a?(LookupKey) && @key == v.to_s 86 end
# File lib/puppet/pops/lookup/lookup_key.rb 89 def hash 90 @key.hash 91 end
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.
@param value [Object] the value to prune @return the possibly pruned value
# File lib/puppet/pops/lookup/lookup_key.rb 36 def prune(value) 37 if @segments.nil? 38 value 39 else 40 pruned = @segments.reduce(value) do |memo, segment| 41 memo.is_a?(Hash) || memo.is_a?(Array) && segment.is_a?(Integer) ? memo[segment] : nil 42 end 43 if pruned.nil? 44 case value 45 when Hash 46 EMPTY_HASH 47 when Array 48 EMPTY_ARRAY 49 else 50 nil 51 end 52 else 53 undig(pruned) 54 end 55 end 56 end
# File lib/puppet/pops/lookup/lookup_key.rb 75 def to_a 76 unless instance_variable_defined?(:@all_segments) 77 a = [@root_key] 78 a += @segments unless @segments.nil? 79 @all_segments = a.freeze 80 end 81 @all_segments 82 end
# File lib/puppet/pops/lookup/lookup_key.rb 93 def to_s 94 @key 95 end
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.
@param value [Object] the value to wrap in a structure in case this value has subkeys @return [Object] the possibly wrapped value
# File lib/puppet/pops/lookup/lookup_key.rb 63 def undig(value) 64 @segments.nil? ? value : segments.reverse.reduce(value) do |memo, segment| 65 if segment.is_a?(Integer) 66 x = [] 67 x[segment] = memo 68 else 69 x = { segment => memo } 70 end 71 x 72 end 73 end